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

@ -37,18 +37,20 @@ ORDER BY [title]
}
pub async fn can_edit_recipe(&self, user_id: i64, recipe_id: i64) -> Result<bool> {
sqlx::query_scalar(r#"SELECT COUNT(*) FROM [Recipe] WHERE [id] = $1 AND [user_id] = $2"#)
.bind(recipe_id)
.bind(user_id)
.fetch_one(&self.pool)
.await
.map_err(DBError::from)
sqlx::query_scalar(
r#"SELECT COUNT(*) = 1 FROM [Recipe] WHERE [id] = $1 AND [user_id] = $2"#,
)
.bind(recipe_id)
.bind(user_id)
.fetch_one(&self.pool)
.await
.map_err(DBError::from)
}
pub async fn can_edit_recipe_group(&self, user_id: i64, group_id: i64) -> Result<bool> {
sqlx::query_scalar(
r#"
SELECT COUNT(*)
SELECT COUNT(*) = 1
FROM [Recipe]
INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
WHERE [Group].[id] = $1 AND [user_id] = $2
@ -64,7 +66,7 @@ WHERE [Group].[id] = $1 AND [user_id] = $2
pub async fn can_edit_recipe_step(&self, user_id: i64, step_id: i64) -> Result<bool> {
sqlx::query_scalar(
r#"
SELECT COUNT(*)
SELECT COUNT(*) = 1
FROM [Recipe]
INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
INNER JOIN [Step] ON [Step].[group_id] = [Group].[id]
@ -171,6 +173,16 @@ WHERE [Recipe].[user_id] = $1
.map_err(DBError::from)
}
pub async fn set_recipe_servings(&self, recipe_id: i64, servings: Option<u32>) -> Result<()> {
sqlx::query("UPDATE [Recipe] SET [servings] = $2 WHERE [id] = $1")
.bind(recipe_id)
.bind(servings)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
}
pub async fn set_recipe_estimated_time(
&self,
recipe_id: i64,
@ -222,6 +234,15 @@ WHERE [Recipe].[user_id] = $1
.map_err(DBError::from)
}
pub async fn rm_recipe(&self, recipe_id: i64) -> Result<()> {
sqlx::query("DELETE FROM [Recipe] WHERE [id] = $1")
.bind(recipe_id)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
}
pub async fn get_groups(&self, recipe_id: i64) -> Result<Vec<model::Group>> {
let mut tx = self.tx().await?;
let mut groups: Vec<model::Group> = sqlx::query_as(