summaryrefslogtreecommitdiffstats
path: root/src/routes/model.rs
blob: a5cd2b3fcfe9b674081bbc8a52416e867dad54af (plain)
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
use crate::errors::AppError;
use crate::models::{
    auth::Claims,
    model::{Model, ModelCreate, ModelUser},
};
use axum::{routing::get, Json, Router};

/// Create routes for `/v1/models/` namespace
pub fn create_route() -> Router {
    Router::new().route("/", get(list_models).post(create_model))
}

/// List models.
async fn list_models() -> Result<Json<Vec<ModelUser>>, AppError> {
    let models = Model::list().await?;

    Ok(Json(models))
}

/// Create a model. Checks Authorization token
async fn create_model(
    Json(payload): Json<ModelCreate>,
    claims: Claims,
) -> Result<Json<Model>, AppError> {
    let model = Model::new(
        payload.name,
        payload.description,
        payload.duration,
        payload.height,
        payload.weight,
        payload.printer,
        payload.material,
        claims.user_id,
    );

    let model_new = Model::create(model).await?;

    Ok(Json(model_new))
}