раскидал структуру для монорепозитория
This commit is contained in:
132
backend/src/routes/family.rs
Normal file
132
backend/src/routes/family.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use sea_orm::DatabaseConnection;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::models::family::Model as FamilyModel;
|
||||
use crate::services::FamilyService;
|
||||
|
||||
#[derive(Debug, Deserialize, ToSchema)]
|
||||
#[schema(example = json!({"name": "Smith Family"}))]
|
||||
pub struct CreateFamilyRequest {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, ToSchema)]
|
||||
#[schema(example = json!({"name": "Updated Family Name"}))]
|
||||
pub struct UpdateFamilyRequest {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/families",
|
||||
tag = "families",
|
||||
request_body = CreateFamilyRequest,
|
||||
responses(
|
||||
(status = 200, description = "Family created successfully", body = FamilyModel),
|
||||
(status = 500, description = "Internal server error")
|
||||
)
|
||||
)]
|
||||
pub async fn create_family(
|
||||
State(db): State<DatabaseConnection>,
|
||||
Json(payload): Json<CreateFamilyRequest>,
|
||||
) -> Result<Json<FamilyModel>, StatusCode> {
|
||||
FamilyService::create(&db, payload.name)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/families/{id}",
|
||||
tag = "families",
|
||||
params(
|
||||
("id" = i32, Path, description = "Family ID")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Family found", body = FamilyModel),
|
||||
(status = 404, description = "Family not found"),
|
||||
(status = 500, description = "Internal server error")
|
||||
)
|
||||
)]
|
||||
pub async fn get_family(
|
||||
State(db): State<DatabaseConnection>,
|
||||
Path(id): Path<i32>,
|
||||
) -> Result<Json<FamilyModel>, StatusCode> {
|
||||
FamilyService::find_by_id(&db, id)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.map(Json)
|
||||
.ok_or(StatusCode::NOT_FOUND)
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/families",
|
||||
tag = "families",
|
||||
responses(
|
||||
(status = 200, description = "List of all families", body = Vec<FamilyModel>),
|
||||
(status = 500, description = "Internal server error")
|
||||
)
|
||||
)]
|
||||
pub async fn get_all_families(
|
||||
State(db): State<DatabaseConnection>,
|
||||
) -> Result<Json<Vec<FamilyModel>>, StatusCode> {
|
||||
FamilyService::find_all(&db)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/families/{id}",
|
||||
tag = "families",
|
||||
params(
|
||||
("id" = i32, Path, description = "Family ID")
|
||||
),
|
||||
request_body = UpdateFamilyRequest,
|
||||
responses(
|
||||
(status = 200, description = "Family updated successfully", body = FamilyModel),
|
||||
(status = 500, description = "Internal server error")
|
||||
)
|
||||
)]
|
||||
pub async fn update_family(
|
||||
State(db): State<DatabaseConnection>,
|
||||
Path(id): Path<i32>,
|
||||
Json(payload): Json<UpdateFamilyRequest>,
|
||||
) -> Result<Json<FamilyModel>, StatusCode> {
|
||||
FamilyService::update(&db, id, payload.name)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/families/{id}",
|
||||
tag = "families",
|
||||
params(
|
||||
("id" = i32, Path, description = "Family ID")
|
||||
),
|
||||
responses(
|
||||
(status = 204, description = "Family deleted successfully"),
|
||||
(status = 500, description = "Internal server error")
|
||||
)
|
||||
)]
|
||||
pub async fn delete_family(
|
||||
State(db): State<DatabaseConnection>,
|
||||
Path(id): Path<i32>,
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
FamilyService::delete(&db, id)
|
||||
.await
|
||||
.map(|_| StatusCode::NO_CONTENT)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
Reference in New Issue
Block a user