about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/get/auth/unit_property.rs
blob: f5b070ab8ce00abe77113b034510bc2439f304bb (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use actix_identity::Identity;
use actix_web::{HttpResponse, Responder, Result, get, web};

use crate::{
    app::App,
    storage::sql::unit_property::{UnitProperty, UnitPropertyId, UnitPropertyIdStub},
};

/// Return all registered unit properties
#[utoipa::path(
    responses(
        (
            status = OK,
            description = "All unit properties founds",
            body = Vec<UnitProperty>
        ),
        (
            status = UNAUTHORIZED,
            description = "You did not login before calling this endpoint",
        ),
        (
            status = INTERNAL_SERVER_ERROR,
            description = "Server encountered error",
            body = String
        )
    ),
)]
#[get("/unit-properties/")]
pub(crate) async fn unit_properties(
    app: web::Data<App>,
    _user: Identity,
) -> Result<impl Responder> {
    let all = UnitProperty::get_all(&app).await?;

    Ok(HttpResponse::Ok().json(all))
}

/// Get Unit property by id
#[utoipa::path(
    responses(
        (
            status = OK,
            description = "Unit property found from database",
            body = UnitProperty
        ),
        (
            status = NOT_FOUND,
            description = "Unit Property not found in database"
        ),
        (
            status = UNAUTHORIZED,
            description = "You did not login before calling this endpoint",
        ),
        (
            status = INTERNAL_SERVER_ERROR,
            description = "Server encountered error",
            body = String
        )
    ),
    params(
        (
            "id" = UnitPropertyId,
            description = "Unit Property id"
        ),
    )
)]
#[get("/unit-property/{id}")]
pub(crate) async fn unit_property_by_id(
    app: web::Data<App>,
    id: web::Path<UnitPropertyIdStub>,
    _user: Identity,
) -> Result<impl Responder> {
    let id = id.into_inner();

    match UnitProperty::from_id(&app, id.into()).await? {
        Some(unit_property) => Ok(HttpResponse::Ok().json(unit_property)),
        None => Ok(HttpResponse::NotFound().finish()),
    }
}