diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-10-17 19:50:04 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-10-17 19:50:04 +0000 |
| commit | 611293122213f83e82d851cd8dc83fd1e4f79dcd (patch) | |
| tree | bdbfe3badce6a483092edae29293da023e1c24fc /src | |
| parent | c3db6168934c9e29f29fcb098bc65691da87b37e (diff) | |
Get warning by id
Diffstat (limited to 'src')
| -rw-r--r-- | src/routes/warning.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/routes/warning.rs b/src/routes/warning.rs index f33b339..384218a 100644 --- a/src/routes/warning.rs +++ b/src/routes/warning.rs @@ -12,7 +12,7 @@ use crate::{ use axum::{ extract::{Path, Query}, http::StatusCode, - routing::{get, post, put}, + routing::{get, post}, Json, Router, }; @@ -20,7 +20,10 @@ use axum::{ pub fn create_route() -> Router { Router::new() .route("/", get(list_warnings).post(create_warning)) - .route("/:id", put(edit_warning).delete(delete_warning)) + .route( + "/:id", + get(get_warning).put(edit_warning).delete(delete_warning), + ) .route("/filter", post(filter_warnings)) } @@ -47,6 +50,23 @@ async fn list_warnings( Ok(Json(WarningPagination { count, results })) } +/// Get a warning with id = `model_id` +async fn get_warning( + Path(warning_id): Path<i32>, + claims: Claims, +) -> Result<Json<Warning>, AppError> { + let user = User::find_by_id(claims.user_id).await?; + + if !(user.is_staff.unwrap()) { + return Err(AppError::Unauthorized); + } + + match Warning::find_by_id(warning_id).await { + Ok(warning) => Ok(Json(warning.into())), + Err(_) => Err(AppError::NotFound("Warning not found".to_string())), + } +} + /// Create a warning. Checks Authorization token async fn create_warning( Json(payload): Json<WarningCreate>, |
