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