From 611293122213f83e82d851cd8dc83fd1e4f79dcd Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 17 Oct 2022 21:50:04 +0200 Subject: Get warning by id --- src/routes/warning.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/routes') 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, + claims: Claims, +) -> Result, 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, -- cgit v1.2.3-71-g8e6c