Recipe edit (WIP): forms to edit groups, steps and ingredients

This commit is contained in:
Greg Burri 2024-12-26 01:39:07 +01:00
parent dd05a673d9
commit 07b7ff425e
25 changed files with 881 additions and 203 deletions

View file

@ -1,8 +1,9 @@
use axum::{
debug_handler,
extract::{Extension, State},
extract::{Extension, Query, State},
response::{IntoResponse, Result},
};
use serde::Deserialize;
// use tracing::{event, Level};
use crate::{
@ -10,9 +11,15 @@ use crate::{
html_templates::*,
};
#[derive(Deserialize)]
pub struct CurrentRecipeId {
current_recipe_id: Option<i64>,
}
#[debug_handler]
pub async fn recipes_list_fragments(
State(connection): State<db::Connection>,
current_recipe: Query<CurrentRecipeId>,
Extension(user): Extension<Option<model::User>>,
) -> Result<impl IntoResponse> {
let recipes = Recipes {
@ -24,8 +31,7 @@ pub async fn recipes_list_fragments(
} else {
vec![]
},
current_id: None,
current_id: current_recipe.current_recipe_id,
};
Ok(RecipesListFragmentTemplate { user, recipes })
}

View file

@ -48,13 +48,19 @@
use axum::{
debug_handler,
extract::{Extension, State},
extract::{Extension, Query, State},
http::StatusCode,
response::{ErrorResponse, IntoResponse, Result},
};
use serde::Deserialize;
// use tracing::{event, Level};
use crate::{data::db, model, ron_extractor::ExtractRon, ron_utils::ron_error};
use crate::{
data::db,
model,
ron_extractor::ExtractRon,
ron_utils::{ron_error, ron_response},
};
#[allow(dead_code)]
#[debug_handler]
@ -81,7 +87,7 @@ pub async fn update_user(
Ok(StatusCode::OK)
}
async fn check_user_rights(
async fn check_user_rights_recipe(
connection: &db::Connection,
user: &Option<model::User>,
recipe_id: i64,
@ -100,13 +106,32 @@ async fn check_user_rights(
}
}
async fn check_user_rights_recipe_group(
connection: &db::Connection,
user: &Option<model::User>,
group_id: i64,
) -> Result<()> {
if user.is_none()
|| !connection
.can_edit_recipe_group(user.as_ref().unwrap().id, group_id)
.await?
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
"Action not authorized",
)))
} else {
Ok(())
}
}
#[debug_handler]
pub async fn set_recipe_title(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeTitle>,
) -> Result<StatusCode> {
check_user_rights(&connection, &user, ron.recipe_id).await?;
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_title(ron.recipe_id, &ron.title)
.await?;
@ -119,7 +144,7 @@ pub async fn set_recipe_description(
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeDescription>,
) -> Result<StatusCode> {
check_user_rights(&connection, &user, ron.recipe_id).await?;
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_description(ron.recipe_id, &ron.description)
.await?;
@ -132,7 +157,7 @@ pub async fn set_estimated_time(
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeEstimatedTime>,
) -> Result<StatusCode> {
check_user_rights(&connection, &user, ron.recipe_id).await?;
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_estimated_time(ron.recipe_id, ron.estimated_time)
.await?;
@ -145,7 +170,7 @@ pub async fn set_difficulty(
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeDifficulty>,
) -> Result<StatusCode> {
check_user_rights(&connection, &user, ron.recipe_id).await?;
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_difficulty(ron.recipe_id, ron.difficulty)
.await?;
@ -158,7 +183,7 @@ pub async fn set_language(
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeLanguage>,
) -> Result<StatusCode> {
check_user_rights(&connection, &user, ron.recipe_id).await?;
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_language(ron.recipe_id, &ron.lang)
.await?;
@ -171,13 +196,128 @@ pub async fn set_is_published(
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetIsPublished>,
) -> Result<StatusCode> {
check_user_rights(&connection, &user, ron.recipe_id).await?;
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_is_published(ron.recipe_id, ron.is_published)
.await?;
Ok(StatusCode::OK)
}
impl From<model::Group> for common::ron_api::Group {
fn from(group: model::Group) -> Self {
Self {
id: group.id,
name: group.name,
comment: group.comment,
steps: group
.steps
.into_iter()
.map(common::ron_api::Step::from)
.collect(),
}
}
}
impl From<model::Step> for common::ron_api::Step {
fn from(step: model::Step) -> Self {
Self {
id: step.id,
action: step.action,
ingredients: step
.ingredients
.into_iter()
.map(common::ron_api::Ingredient::from)
.collect(),
}
}
}
impl From<model::Ingredient> for common::ron_api::Ingredient {
fn from(ingredient: model::Ingredient) -> Self {
Self {
id: ingredient.id,
name: ingredient.name,
comment: ingredient.comment,
quantity_value: ingredient.quantity_value,
quantity_unit: ingredient.quantity_unit,
}
}
}
#[derive(Deserialize)]
pub struct RecipeId {
#[serde(rename = "recipe_id")]
id: i64,
}
#[debug_handler]
pub async fn get_groups(
State(connection): State<db::Connection>,
recipe_id: Query<RecipeId>,
) -> Result<impl IntoResponse> {
println!("PROUT");
// Here we don't check user rights on purpose.
Ok(ron_response(
StatusCode::OK,
connection
.get_groups(recipe_id.id)
.await?
.into_iter()
.map(common::ron_api::Group::from)
.collect::<Vec<_>>(),
))
}
#[debug_handler]
pub async fn add_group(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::AddRecipeGroup>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
let group_id = connection.add_recipe_group(ron.recipe_id).await?;
Ok(ron_response(
StatusCode::OK,
common::ron_api::AddRecipeGroupResult { group_id },
))
}
#[debug_handler]
pub async fn rm_group(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::RemoveRecipeGroup>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
connection.rm_recipe_group(ron.group_id).await?;
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn set_group_name(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetGroupName>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
connection.set_group_name(ron.group_id, &ron.name).await?;
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn set_group_comment(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetGroupComment>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
connection
.set_group_comment(ron.group_id, &ron.comment)
.await?;
Ok(StatusCode::OK)
}
///// 404 /////
#[debug_handler]
pub async fn not_found(Extension(_user): Extension<Option<model::User>>) -> impl IntoResponse {

View file

@ -22,7 +22,7 @@ use crate::{
utils, AppState,
};
//// SIGN UP /////
/// SIGN UP ///
#[debug_handler]
pub async fn sign_up_get(
@ -207,7 +207,7 @@ pub async fn sign_up_validation(
}
}
///// SIGN IN /////
/// SIGN IN ///
#[debug_handler]
pub async fn sign_in_get(
@ -271,7 +271,7 @@ pub async fn sign_in_post(
}
}
///// SIGN OUT /////
/// SIGN OUT ///
#[debug_handler]
pub async fn sign_out(
@ -287,7 +287,7 @@ pub async fn sign_out(
Ok((jar, Redirect::to("/")))
}
///// RESET PASSWORD /////
/// RESET PASSWORD ///
#[debug_handler]
pub async fn ask_reset_password_get(
@ -510,7 +510,7 @@ pub async fn reset_password_post(
}
}
///// EDIT PROFILE /////
/// EDIT PROFILE ///
#[debug_handler]
pub async fn edit_user_get(Extension(user): Extension<Option<model::User>>) -> Response {
@ -614,7 +614,7 @@ pub async fn edit_user_post(
match connection
.update_user(
user.id,
Some(&email_trimmed),
Some(email_trimmed),
Some(&form_data.name),
new_password,
)