summaryrefslogtreecommitdiffstats
path: root/src/routes/model.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-06 17:58:35 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-06 17:58:35 +0000
commitacc32ccc1650b99e03c390a87e71b0a85b073162 (patch)
tree0de545ee0eb8eba5961c57c0a73ff7031e97613d /src/routes/model.rs
parente6dbcbb42e7a4a973887acde633cc3ee233aad3e (diff)
Create a 3d model object
Diffstat (limited to 'src/routes/model.rs')
-rw-r--r--src/routes/model.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/routes/model.rs b/src/routes/model.rs
new file mode 100644
index 0000000..b1d4cc2
--- /dev/null
+++ b/src/routes/model.rs
@@ -0,0 +1,39 @@
+use crate::errors::AppError;
+use crate::models::{
+ auth::Claims,
+ model::{Model, ModelCreate},
+};
+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<Model>>, 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))
+}