koprogo_api/infrastructure/web/handlers/
pcn_handlers.rs

1use crate::application::dto::PcnReportRequest;
2use crate::infrastructure::web::AppState;
3use actix_web::{get, post, web, HttpResponse, Responder};
4use uuid::Uuid;
5
6/// Generate PCN report for a building (JSON)
7/// POST /api/v1/pcn/report/:building_id
8#[post("/pcn/report/{building_id}")]
9pub async fn generate_pcn_report(
10    app_state: web::Data<AppState>,
11    building_id: web::Path<Uuid>,
12) -> impl Responder {
13    let request = PcnReportRequest {
14        building_id: *building_id,
15        start_date: None,
16        end_date: None,
17    };
18
19    match app_state.pcn_use_cases.generate_report(request).await {
20        Ok(report) => HttpResponse::Ok().json(report),
21        Err(err) => HttpResponse::InternalServerError().body(err),
22    }
23}
24
25/// Export PCN report as PDF
26/// GET /api/v1/pcn/export/pdf/:building_id?name=MyBuilding
27#[get("/pcn/export/pdf/{building_id}")]
28pub async fn export_pcn_pdf(
29    app_state: web::Data<AppState>,
30    building_id: web::Path<Uuid>,
31    query: web::Query<std::collections::HashMap<String, String>>,
32) -> impl Responder {
33    let building_name = query
34        .get("name")
35        .cloned()
36        .unwrap_or_else(|| "Building".to_string());
37
38    let request = PcnReportRequest {
39        building_id: *building_id,
40        start_date: None,
41        end_date: None,
42    };
43
44    match app_state
45        .pcn_use_cases
46        .export_pdf(&building_name, request)
47        .await
48    {
49        Ok(pdf_bytes) => HttpResponse::Ok()
50            .content_type("application/pdf")
51            .append_header((
52                "Content-Disposition",
53                "attachment; filename=\"rapport-pcn.pdf\"",
54            ))
55            .body(pdf_bytes),
56        Err(err) => HttpResponse::InternalServerError().body(err),
57    }
58}
59
60/// Export PCN report as Excel
61/// GET /api/v1/pcn/export/excel/:building_id?name=MyBuilding
62#[get("/pcn/export/excel/{building_id}")]
63pub async fn export_pcn_excel(
64    app_state: web::Data<AppState>,
65    building_id: web::Path<Uuid>,
66    query: web::Query<std::collections::HashMap<String, String>>,
67) -> impl Responder {
68    let building_name = query
69        .get("name")
70        .cloned()
71        .unwrap_or_else(|| "Building".to_string());
72
73    let request = PcnReportRequest {
74        building_id: *building_id,
75        start_date: None,
76        end_date: None,
77    };
78
79    match app_state
80        .pcn_use_cases
81        .export_excel(&building_name, request)
82        .await
83    {
84        Ok(excel_bytes) => HttpResponse::Ok()
85            .content_type("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
86            .append_header((
87                "Content-Disposition",
88                "attachment; filename=\"rapport-pcn.xlsx\"",
89            ))
90            .body(excel_bytes),
91        Err(err) => HttpResponse::InternalServerError().body(err),
92    }
93}