backbone_accounting/presentation/http/
hierarchy_handler.rs1use std::sync::Arc;
16
17use axum::{
18 extract::Path, extract::Query, extract::State, response::IntoResponse, routing::get, Json,
19 Router,
20};
21use serde::Deserialize;
22use uuid::Uuid;
23
24use crate::application::service::hierarchy_service::HierarchyService;
25use crate::domain::repositories::hierarchy_repository::HierarchyTable;
26
27fn err(e: anyhow::Error) -> (axum::http::StatusCode, Json<serde_json::Value>) {
28 (
29 axum::http::StatusCode::INTERNAL_SERVER_ERROR,
30 Json(serde_json::json!({ "success": false, "error": e.to_string() })),
31 )
32}
33
34async fn ancestors(
35 State(svc): State<Arc<HierarchyService>>,
36 table: HierarchyTable,
37 Path(id): Path<Uuid>,
38) -> impl IntoResponse {
39 match svc.ancestors(table, id).await {
40 Ok(chain) => {
41 Json(serde_json::json!({ "success": true, "hierarchy": chain })).into_response()
42 }
43 Err(e) => err(e).into_response(),
44 }
45}
46
47pub fn create_hierarchy_routes(service: Arc<HierarchyService>) -> Router {
49 Router::new()
50 .route(
51 "/accounts/:id/hierarchy",
52 get(|st, id| ancestors(st, HierarchyTable::Account, id)),
53 )
54 .route(
55 "/cost-centers/:id/hierarchy",
56 get(|st, id| ancestors(st, HierarchyTable::CostCenter, id)),
57 )
58 .route(
59 "/fiscal-periods/:id/hierarchy",
60 get(|st, id| ancestors(st, HierarchyTable::FiscalPeriod, id)),
61 )
62 .with_state(service)
63}