Init
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
use axum::{Extension, Json};
|
||||
use axum::extract::{Path, State, Query};
|
||||
use crate::app::AppState;
|
||||
use crate::interfaces::http::dto::category::{CategoryResponse, CreateCategoryRequest};
|
||||
use crate::models::category;
|
||||
use crate::network::http::RequestContext;
|
||||
use crate::network::http::{AppRouter, HTTPError};
|
||||
use crate::serializers::CategorySerializer;
|
||||
use axum::extract::{Path, Query, State};
|
||||
use axum::http::{Extensions, StatusCode};
|
||||
use axum::routing::{delete, get, post, put};
|
||||
use axum::{Extension, Json};
|
||||
use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
use crate::app::AppState;
|
||||
use crate::models::category;
|
||||
use crate::network::http::{AppRouter, HTTPError};
|
||||
use crate::network::http::RequestContext;
|
||||
use crate::serializers::CategorySerializer;
|
||||
|
||||
pub fn setup_route() -> AppRouter {
|
||||
AppRouter::new()
|
||||
@@ -28,31 +29,35 @@ pub struct CategoryQuery {
|
||||
pub async fn category_list(
|
||||
State(app_state): State<AppState>,
|
||||
Extension(_ctx): Extension<RequestContext>,
|
||||
Query(query): Query<CategoryQuery>
|
||||
) -> Result<Json<Vec<CategorySerializer>>, HTTPError> {
|
||||
Query(query): Query<CategoryQuery>,
|
||||
) -> Result<Json<Vec<CategoryResponse>>, HTTPError> {
|
||||
log::info!("GET /categories/ - Query: server_id={:?}", query.server_id);
|
||||
|
||||
let categories = if let Some(server_id) = query.server_id {
|
||||
app_state.repositories.category
|
||||
app_state
|
||||
.repositories
|
||||
.category
|
||||
.get_by_server_id(server_id)
|
||||
.await?
|
||||
} else {
|
||||
app_state.repositories.category
|
||||
.get_all()
|
||||
.await?
|
||||
app_state.repositories.category.get_all().await?
|
||||
};
|
||||
|
||||
log::info!("GET /categories/ - Found {} categories", categories.len());
|
||||
Ok(Json(categories.into_iter().map(CategorySerializer::from).collect()))
|
||||
Ok(Json(
|
||||
categories.into_iter().map(CategoryResponse::from).collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn category_detail(
|
||||
State(app_state): State<AppState>,
|
||||
Path(id): Path<Uuid>
|
||||
) -> Result<Json<CategorySerializer>, HTTPError> {
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<CategoryResponse>, HTTPError> {
|
||||
log::info!("GET /categories/{id}/ - Fetching category: {}", id);
|
||||
|
||||
let category = app_state.repositories.category
|
||||
let category = app_state
|
||||
.repositories
|
||||
.category
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
@@ -61,27 +66,33 @@ pub async fn category_detail(
|
||||
})?;
|
||||
|
||||
log::info!("GET /categories/{id}/ - Category found: {}", id);
|
||||
Ok(Json(CategorySerializer::from(category)))
|
||||
Ok(Json(CategoryResponse::from(category)))
|
||||
}
|
||||
|
||||
pub async fn category_create(
|
||||
State(app_state): State<AppState>,
|
||||
Json(serializer): Json<CategorySerializer>
|
||||
) -> Result<Json<CategorySerializer>, HTTPError> {
|
||||
log::info!("POST /categories/ - Creating category: {:?}", serializer.name);
|
||||
Json(serializer): Json<CreateCategoryRequest>,
|
||||
) -> Result<Json<CategoryResponse>, HTTPError> {
|
||||
log::info!(
|
||||
"POST /categories/ - Creating category: {:?}",
|
||||
serializer.name
|
||||
);
|
||||
|
||||
let active = serializer.into_active_model();
|
||||
let active: category::ActiveModel = serializer.into();
|
||||
let category: category::Model = active.insert(app_state.db.get_connection()).await?;
|
||||
|
||||
log::info!("POST /categories/ - Category created with id: {}", category.id);
|
||||
Ok(Json(CategorySerializer::from(category)))
|
||||
log::info!(
|
||||
"POST /categories/ - Category created with id: {}",
|
||||
category.id
|
||||
);
|
||||
Ok(Json(CategoryResponse::from(category)))
|
||||
}
|
||||
|
||||
pub async fn category_update(
|
||||
State(app_state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(serializer): Json<CategorySerializer>,
|
||||
) -> Result<Json<CategorySerializer>, HTTPError> {
|
||||
Json(serializer): Json<CreateCategoryRequest>,
|
||||
) -> Result<Json<CategoryResponse>, HTTPError> {
|
||||
log::info!("PUT /categories/{id}/ - Updating category: {}", id);
|
||||
|
||||
let category = category::Entity::find_by_id(id)
|
||||
@@ -94,17 +105,18 @@ pub async fn category_update(
|
||||
|
||||
let active = category.into_active_model();
|
||||
|
||||
let category: category::Model = serializer.apply_to_active_model(active)
|
||||
.update(app_state.db.get_connection())
|
||||
.await?;
|
||||
// let category: category::Model = serializer
|
||||
// .apply_to_active_model(active)
|
||||
// .update(app_state.db.get_connection())
|
||||
// .await?;
|
||||
|
||||
log::info!("PUT /categories/{id}/ - Category updated: {}", id);
|
||||
Ok(Json(CategorySerializer::from(category)))
|
||||
Ok(Json(CategoryResponse::from(category)))
|
||||
}
|
||||
|
||||
pub async fn category_delete(
|
||||
State(app_state): State<AppState>,
|
||||
Path(id): Path<Uuid>
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
log::info!("DELETE /categories/{id}/ - Deleting category: {}", id);
|
||||
|
||||
@@ -119,4 +131,4 @@ pub async fn category_delete(
|
||||
log::info!("DELETE /categories/{id}/ - Category deleted: {}", id);
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user