Recipe edit (WIP): Buttons to add steps and inrgedients

This commit is contained in:
Greg Burri 2024-12-27 12:51:29 +01:00
parent 6876a254e1
commit d4962c98ff
9 changed files with 182 additions and 15 deletions

View file

@ -304,6 +304,23 @@ ORDER BY [name]
.map_err(DBError::from)
}
pub async fn add_recipe_step(&self, group_id: i64) -> Result<i64> {
let db_result = sqlx::query("INSERT INTO [Step] ([group_id]) VALUES ($1)")
.bind(group_id)
.execute(&self.pool)
.await?;
Ok(db_result.last_insert_rowid())
}
pub async fn rm_recipe_step(&self, step_id: i64) -> Result<()> {
sqlx::query("DELETE FROM [Step] WHERE [id] = $1")
.bind(step_id)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
}
pub async fn set_step_action(&self, step_id: i64, action: &str) -> Result<()> {
sqlx::query("UPDATE [Step] SET [action] = $2 WHERE [id] = $1")
.bind(step_id)
@ -314,6 +331,23 @@ ORDER BY [name]
.map_err(DBError::from)
}
pub async fn add_recipe_ingredient(&self, step_id: i64) -> Result<i64> {
let db_result = sqlx::query("INSERT INTO [Ingredient] ([step_id]) VALUES ($1)")
.bind(step_id)
.execute(&self.pool)
.await?;
Ok(db_result.last_insert_rowid())
}
pub async fn rm_recipe_ingredient(&self, ingredient_id: i64) -> Result<()> {
sqlx::query("DELETE FROM [Ingredient] WHERE [id] = $1")
.bind(ingredient_id)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
}
pub async fn set_ingredient_name(&self, ingredient_id: i64, name: &str) -> Result<()> {
sqlx::query("UPDATE [Ingredient] SET [name] = $2 WHERE [id] = $1")
.bind(ingredient_id)

View file

@ -58,6 +58,6 @@ pub struct Ingredient {
pub id: i64,
pub name: String,
pub comment: String,
pub quantity_value: f64,
pub quantity_value: Option<f64>,
pub quantity_unit: String,
}

View file

@ -109,10 +109,20 @@ async fn main() {
"/recipe/set_group_comment",
put(services::ron::set_group_comment),
)
.route("/recipe/add_step", post(services::ron::add_step))
.route("/recipe/remove_step", delete(services::ron::rm_step))
.route(
"/recipe/set_step_action",
put(services::ron::set_step_action),
)
.route(
"/recipe/add_ingredient",
post(services::ron::add_ingredient),
)
.route(
"/recipe/remove_ingredient",
delete(services::ron::rm_ingredient),
)
.route(
"/recipe/set_ingredient_name",
put(services::ron::set_ingredient_name),

View file

@ -355,6 +355,32 @@ pub async fn set_group_comment(
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn add_step(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::AddRecipeStep>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
let step_id = connection.add_recipe_step(ron.group_id).await?;
Ok(ron_response(
StatusCode::OK,
common::ron_api::AddRecipeStepResult { step_id },
))
}
#[debug_handler]
pub async fn rm_step(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::RemoveRecipeStep>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
connection.rm_recipe_step(ron.step_id).await?;
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn set_step_action(
State(connection): State<db::Connection>,
@ -366,6 +392,32 @@ pub async fn set_step_action(
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn add_ingredient(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::AddRecipeIngredient>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
let ingredient_id = connection.add_recipe_ingredient(ron.step_id).await?;
Ok(ron_response(
StatusCode::OK,
common::ron_api::AddRecipeIngredientResult { ingredient_id },
))
}
#[debug_handler]
pub async fn rm_ingredient(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::RemoveRecipeIngredient>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
connection.rm_recipe_ingredient(ron.ingredient_id).await?;
Ok(StatusCode::OK)
}
#[debug_handler]
pub async fn set_ingredient_name(
State(connection): State<db::Connection>,