summaryrefslogtreecommitdiffstats
path: root/src/routes
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-23 18:45:39 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-23 18:45:39 +0000
commit7cb5079e1d8e5fbd3659ff0d6115ad6d0f1960b1 (patch)
tree8e8db991ca3ae972722ca7fcde036454e9cca2d7 /src/routes
parentb4764fc85c36f5cac6a2a43413da8ad374519fd8 (diff)
Edit model
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/model.rs87
1 files changed, 60 insertions, 27 deletions
diff --git a/src/routes/model.rs b/src/routes/model.rs
index e40b31e..d19c6df 100644
--- a/src/routes/model.rs
+++ b/src/routes/model.rs
@@ -21,7 +21,7 @@ use serde::Serialize;
pub fn create_route() -> Router {
Router::new()
.route("/", get(list_models).post(create_model))
- .route("/:id", get(get_model).delete(delete_model))
+ .route("/:id", get(get_model).delete(delete_model).put(edit_model))
.route("/:id/upload", post(upload_model_file))
.route("/:id/upload/:uid", delete(delete_model_file))
}
@@ -70,12 +70,8 @@ async fn get_model(Path(model_id): Path<i32>) -> Result<Json<ModelUser>, AppErro
}
}
-/// Upload a file for a model
-async fn upload_model_file(
- claims: Claims,
- Path(model_id): Path<i32>,
- ContentLengthLimit(multipart): ContentLengthLimit<Multipart, { 1024 * 1024 }>,
-) -> Result<Json<ModelUpload>, AppError> {
+/// The owner or a staffer can delete a model
+async fn delete_model(claims: Claims, Path(model_id): Path<i32>) -> Result<StatusCode, AppError> {
let model = match Model::find_by_id(model_id).await {
Ok(model) => model,
Err(_) => {
@@ -83,24 +79,30 @@ async fn upload_model_file(
}
};
- if model.author_id() != claims.user_id {
- return Err(AppError::Unauthorized);
- }
+ let user = User::find_by_id(claims.user_id).await?;
- let allowed_extensions = vec!["stl", "obj", "png", "jpg", "jpeg", "gif", "webp", "blend"];
+ let uploads: Vec<String> = model.list_upload_filepaths().await.unwrap();
- match upload(multipart, allowed_extensions, None).await {
- Ok(saved_file) => {
- let model_file = ModelUpload::create(ModelUpload::new(saved_file, model_id)).await?;
+ if !(model.author_id() == user.id || user.is_staff.unwrap()) {
+ return Err(AppError::Unauthorized);
+ }
- Ok(Json(model_file))
- }
- Err(e) => Err(e),
+ // If the model has been deleted, remove all old uploads from the file system
+ if Model::delete(model_id).await.is_ok() {
+ uploads
+ .iter()
+ .for_each(|path: &String| delete_upload(path).unwrap_or_default());
}
+
+ Ok(StatusCode::NO_CONTENT)
}
-/// The owner or a staffer can delete a model
-async fn delete_model(claims: Claims, Path(model_id): Path<i32>) -> Result<StatusCode, AppError> {
+/// The owner or a staffer can edit a model
+async fn edit_model(
+ Json(payload): Json<ModelCreate>,
+ claims: Claims,
+ Path(model_id): Path<i32>,
+) -> Result<Json<ModelUser>, AppError> {
let model = match Model::find_by_id(model_id).await {
Ok(model) => model,
Err(_) => {
@@ -110,20 +112,51 @@ async fn delete_model(claims: Claims, Path(model_id): Path<i32>) -> Result<Statu
let user = User::find_by_id(claims.user_id).await?;
- let uploads: Vec<String> = model.list_upload_filepaths().await.unwrap();
-
if !(model.author_id() == user.id || user.is_staff.unwrap()) {
return Err(AppError::Unauthorized);
}
- // If the model has been deleted, remove all old uploads from the file system
- if Model::delete(model_id).await.is_ok() {
- uploads
- .iter()
- .for_each(|path: &String| delete_upload(path).unwrap_or_default());
+ let model_body = Model::new(
+ payload.name,
+ payload.description,
+ payload.duration,
+ payload.height,
+ payload.weight,
+ payload.printer,
+ payload.material,
+ claims.user_id,
+ );
+ Model::edit(model.id, model_body).await?;
+ Ok(Json(model))
+}
+
+/// Upload a file for a model
+async fn upload_model_file(
+ claims: Claims,
+ Path(model_id): Path<i32>,
+ ContentLengthLimit(multipart): ContentLengthLimit<Multipart, { 1024 * 1024 }>,
+) -> Result<Json<ModelUpload>, 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);
}
- Ok(StatusCode::NO_CONTENT)
+ let allowed_extensions = vec!["stl", "obj", "png", "jpg", "jpeg", "gif", "webp", "blend"];
+
+ match upload(multipart, allowed_extensions, None).await {
+ Ok(saved_file) => {
+ let model_file = ModelUpload::create(ModelUpload::new(saved_file, model_id)).await?;
+
+ Ok(Json(model_file))
+ }
+ Err(e) => Err(e),
+ }
}
/// The owner or a staffer can delete a model upload