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

@ -33,25 +33,28 @@ pub async fn edit_recipe(
Path(recipe_id): Path<i64>,
) -> Result<Response> {
if let Some(user) = user {
let recipe = connection.get_recipe(recipe_id).await?.unwrap();
if recipe.user_id == user.id {
let recipes = Recipes {
published: connection.get_all_published_recipe_titles().await?,
unpublished: connection
.get_all_unpublished_recipe_titles(user.id)
.await?,
current_id: Some(recipe_id),
};
if let Some(recipe) = connection.get_recipe(recipe_id).await? {
if recipe.user_id == user.id {
let recipes = Recipes {
published: connection.get_all_published_recipe_titles().await?,
unpublished: connection
.get_all_unpublished_recipe_titles(user.id)
.await?,
current_id: Some(recipe_id),
};
Ok(RecipeEditTemplate {
user: Some(user),
recipes,
recipe,
languages: consts::LANGUAGES,
Ok(RecipeEditTemplate {
user: Some(user),
recipes,
recipe,
languages: consts::LANGUAGES,
}
.into_response())
} else {
Ok(MessageTemplate::new("Not allowed to edit this recipe").into_response())
}
.into_response())
} else {
Ok(MessageTemplate::new("Not allowed to edit this recipe").into_response())
Ok(MessageTemplate::new("Recipe not found").into_response())
}
} else {
Ok(MessageTemplate::new("Not logged in").into_response())

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 {