From ad3bb41bbad052a177059a31a904fd8e883d8ff9 Mon Sep 17 00:00:00 2001 From: Nell Date: Sat, 21 Feb 2026 11:06:50 +0100 Subject: [PATCH] Init --- src/network/http/web/api/category.rs | 76 ++++++++++++++++------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/network/http/web/api/category.rs b/src/network/http/web/api/category.rs index 220ca6d..2dd3acc 100644 --- a/src/network/http/web/api/category.rs +++ b/src/network/http/web/api/category.rs @@ -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, Extension(_ctx): Extension, - Query(query): Query -) -> Result>, HTTPError> { + Query(query): Query, +) -> Result>, 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, - Path(id): Path -) -> Result, HTTPError> { + Path(id): Path, +) -> Result, 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, - Json(serializer): Json -) -> Result, HTTPError> { - log::info!("POST /categories/ - Creating category: {:?}", serializer.name); + Json(serializer): Json, +) -> Result, 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, Path(id): Path, - Json(serializer): Json, -) -> Result, HTTPError> { + Json(serializer): Json, +) -> Result, 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, - Path(id): Path + Path(id): Path, ) -> Result { 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) } -} \ No newline at end of file +}