summaryrefslogtreecommitdiffstats
path: root/src/routes/model.rs
blob: 66f8ec62c8221d3c78aa44e998da0cd344125264 (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
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
80
81
82
83
84
use crate::config::MAX_UPLOAD_FILE_SIZE;
use crate::errors::AppError;
use crate::files::upload;
use crate::models::{
    auth::Claims,
    model::{Model, ModelCreate, ModelUser},
};
use crate::pagination::Pagination;
use axum::{
    extract::{ContentLengthLimit, Multipart, Path, Query},
    routing::{get, post},
    Json, Router,
};
use serde::Serialize;

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

#[derive(Serialize)]
struct ModelPagination {
    count: i64,
    results: Vec<ModelUser>,
}

/// List models.
async fn list_models(pagination: Query<Pagination>) -> Result<Json<ModelPagination>, AppError> {
    let page = pagination.0.page.unwrap_or_default();
    let results = Model::list(page).await?;
    let count = Model::count().await?;

    Ok(Json(ModelPagination { count, results }))
}

/// 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))
}

/// Upload a file for a model
async fn upload_model_file(
    claims: Claims,
    Path(model_id): Path<i32>,
    ContentLengthLimit(multipart): ContentLengthLimit<Multipart, { MAX_UPLOAD_FILE_SIZE }>,
) -> Result<String, AppError> {
    let model = match Model::find_by_id(model_id).await {
        Ok(model) => model,
        Err(_) => {
            return Err(AppError::NotFound("Model not found".to_string()));
        }
    };

    if model.author_id() != claims.user_id {
        return Err(AppError::Unauthorized);
    }

    match upload(multipart, vec!["stl"]).await {
        Ok(saved_file) => {
            return Ok(format!("Uploaded {}", saved_file));
        }
        Err(e) => {
            return Err(e);
        }
    }
}