Add a way to delete recipe

This commit is contained in:
Greg Burri 2024-12-31 11:26:51 +01:00
parent 5ce3391466
commit 31bc31035a
10 changed files with 247 additions and 175 deletions

View file

@ -14,6 +14,8 @@ use crate::{
ron_utils::{ron_error, ron_response},
};
const NOT_AUTHORIZED_MESSAGE: &str = "Action not authorized";
#[allow(dead_code)]
#[debug_handler]
pub async fn update_user(
@ -33,7 +35,7 @@ pub async fn update_user(
} else {
return Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
"Action not authorized",
NOT_AUTHORIZED_MESSAGE,
)));
}
Ok(StatusCode::OK)
@ -51,7 +53,7 @@ async fn check_user_rights_recipe(
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
"Action not authorized",
NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
@ -70,7 +72,7 @@ async fn check_user_rights_recipe_group(
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
"Action not authorized",
NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
@ -89,7 +91,7 @@ async fn check_user_rights_recipe_step(
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
"Action not authorized",
NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
@ -108,7 +110,7 @@ async fn check_user_rights_recipe_ingredient(
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
"Action not authorized",
NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
@ -141,6 +143,19 @@ pub async fn set_recipe_description(
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn set_servings(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeServings>,
) -> Result<StatusCode> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_servings(ron.recipe_id, ron.servings)
.await?;
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn set_estimated_time(
State(connection): State<db::Connection>,
@ -193,6 +208,17 @@ pub async fn set_is_published(
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn rm(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::Remove>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection.rm_recipe(ron.recipe_id).await?;
Ok(StatusCode::OK)
}
impl From<model::Group> for common::ron_api::Group {
fn from(group: model::Group) -> Self {
Self {