about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/set/unit.rs
blob: 4281e05cb76f1a5b52fdd3fab12bf7f984a921e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use actix_web::{HttpResponse, Responder, Result, post, web};
use serde::Deserialize;
use utoipa::ToSchema;

use crate::{app::App, storage::sql::{insert::Operations, unit::{Unit, UnitId}}};

#[derive(Deserialize, ToSchema)]
struct UnitStub {
    full_name_plural: String,
    full_name_singular: String,
    short_name: String,
    description: Option<String>,
}

/// Register an Unit
#[utoipa::path(
    responses(
        (
            status = 200,
            description = "Product successfully registered in database",
            body = UnitId,
        ),
        (
            status = INTERNAL_SERVER_ERROR,
            description = "Server encountered error",
            body = String,
        )
    ),
    request_body = UnitStub,
)]
#[post("/unit/new")]
pub(crate) async fn register_unit(
    app: web::Data<App>,
    unit: web::Json<UnitStub>,
) -> Result<impl Responder> {
    let mut ops = Operations::new("register unit");

    let unit = Unit::register(
        unit.full_name_singular.clone(),
        unit.full_name_plural.clone(),
        unit.short_name.clone(),
        unit.description.clone(),
        &mut ops,
    );

    ops.apply(&app).await?;

    Ok(HttpResponse::Ok().json(unit.id))
}