1use crate::auth::validate_token;
4use crate::db;
5use crate::models::*;
6use actix_web::{web, HttpResponse, Result as ActixResult};
7use sqlx::PgPool;
8use tmf_apis_core::TmfError;
9use uuid::Uuid;
10
11#[utoipa::path(
13 get,
14 path = "/tmf-api/usageManagement/v4/usage",
15 responses(
16 (status = 200, description = "List of usage records", body = Vec<Usage>),
17 (status = 401, description = "Unauthorized")
18 ),
19 tag = "TMF635"
20)]
21pub async fn get_usages(
22 pool: web::Data<PgPool>,
23 req: actix_web::HttpRequest,
24) -> ActixResult<HttpResponse> {
25 validate_token(&req)?;
26
27 match db::get_usages(pool.get_ref()).await {
28 Ok(usages) => Ok(HttpResponse::Ok().json(usages)),
29 Err(e) => Ok(HttpResponse::InternalServerError().json(serde_json::json!({
30 "error": e.to_string()
31 }))),
32 }
33}
34
35#[utoipa::path(
37 get,
38 path = "/tmf-api/usageManagement/v4/usage/{id}",
39 responses(
40 (status = 200, description = "Usage record found", body = Usage),
41 (status = 404, description = "Usage record not found"),
42 (status = 400, description = "Invalid usage ID"),
43 (status = 401, description = "Unauthorized")
44 ),
45 params(
46 ("id" = String, Path, description = "Usage ID (UUID)")
47 ),
48 tag = "TMF635"
49)]
50pub async fn get_usage_by_id(
51 pool: web::Data<PgPool>,
52 req: actix_web::HttpRequest,
53 path: web::Path<String>,
54) -> ActixResult<HttpResponse> {
55 validate_token(&req)?;
56
57 let id = match Uuid::parse_str(&path.into_inner()) {
58 Ok(uuid) => uuid,
59 Err(_) => {
60 return Ok(HttpResponse::BadRequest().json(serde_json::json!({
61 "error": "Invalid usage ID format. Expected UUID."
62 })));
63 }
64 };
65
66 match db::get_usage_by_id(pool.get_ref(), id).await {
67 Ok(usage) => Ok(HttpResponse::Ok().json(usage)),
68 Err(TmfError::NotFound(msg)) => Ok(HttpResponse::NotFound().json(serde_json::json!({
69 "error": msg
70 }))),
71 Err(e) => Ok(HttpResponse::InternalServerError().json(serde_json::json!({
72 "error": e.to_string()
73 }))),
74 }
75}
76
77#[utoipa::path(
79 post,
80 path = "/tmf-api/usageManagement/v4/usage",
81 request_body = CreateUsageRequest,
82 responses(
83 (status = 201, description = "Usage record created", body = Usage),
84 (status = 400, description = "Invalid request"),
85 (status = 401, description = "Unauthorized")
86 ),
87 tag = "TMF635"
88)]
89pub async fn create_usage(
90 pool: web::Data<PgPool>,
91 req: actix_web::HttpRequest,
92 body: web::Json<CreateUsageRequest>,
93) -> ActixResult<HttpResponse> {
94 validate_token(&req)?;
95
96 match db::create_usage(pool.get_ref(), body.into_inner()).await {
97 Ok(usage) => Ok(HttpResponse::Created().json(usage)),
98 Err(e) => Ok(HttpResponse::InternalServerError().json(serde_json::json!({
99 "error": e.to_string()
100 }))),
101 }
102}