summaryrefslogtreecommitdiffstats
path: root/src/routes/warning.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-26 21:04:23 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-26 21:04:23 +0000
commit484dd4c1c6bed35ff2ad315c1546eebfd5e9f428 (patch)
tree8e411701a382093b354663c3a1304015707c16d0 /src/routes/warning.rs
parent9a3713339f7f38db7249ba4086d0622a1a9c3896 (diff)
Edit warning
Diffstat (limited to 'src/routes/warning.rs')
-rw-r--r--src/routes/warning.rs33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/routes/warning.rs b/src/routes/warning.rs
index 3a8573e..bb1306e 100644
--- a/src/routes/warning.rs
+++ b/src/routes/warning.rs
@@ -4,14 +4,14 @@ use crate::{
auth::Claims,
model::Model,
user::User,
- warning::{Warning, WarningCreate, WarningFilter, WarningFilterPayload},
+ warning::{Warning, WarningCreate, WarningEdit, WarningFilter, WarningFilterPayload},
},
pagination::{Pagination, WarningPagination},
routes::JsonCreate,
};
use axum::{
- extract::Query,
- routing::{get, post},
+ extract::{Path, Query},
+ routing::{get, post, put},
Json, Router,
};
@@ -19,6 +19,7 @@ use axum::{
pub fn create_route() -> Router {
Router::new()
.route("/", get(list_warnings).post(create_warning))
+ .route("/:id", put(edit_warning))
.route("/filter", post(filter_warnings))
}
@@ -52,7 +53,7 @@ async fn create_warning(
) -> Result<JsonCreate<Warning>, AppError> {
let model = match Model::find_by_id(payload.model_id).await {
Ok(model) => model,
- Err(_) => return Err(AppError::NotFound("Model not found".to_string())),
+ Err(_) => return Err(AppError::NotFound("Report not found".to_string())),
};
let warning = Warning::new(claims.user_id, model.id, payload.note);
@@ -62,6 +63,30 @@ async fn create_warning(
Ok(JsonCreate(warning_new))
}
+/// Staffers can edit a warning
+async fn edit_warning(
+ Json(payload): Json<WarningEdit>,
+ claims: Claims,
+ Path(warning_id): Path<i32>,
+) -> Result<Json<Warning>, AppError> {
+ let mut warning: Warning = match Warning::find_by_id(warning_id).await {
+ Ok(warning) => warning.into(),
+ Err(_) => {
+ return Err(AppError::NotFound("Report not found".to_string()));
+ }
+ };
+
+ let user = User::find_by_id(claims.user_id).await?;
+
+ if !(user.is_staff.unwrap()) {
+ return Err(AppError::Unauthorized);
+ }
+
+ warning.edit(user.id, payload).await?;
+
+ Ok(Json(warning))
+}
+
/// Apply a filter to warnings list
async fn filter_warnings(
Json(payload): Json<WarningFilterPayload>,