Recipe edit (WIP): forms to edit groups, steps and ingredients
This commit is contained in:
parent
dd05a673d9
commit
07b7ff425e
25 changed files with 881 additions and 203 deletions
|
|
@ -45,6 +45,21 @@ ORDER BY [title]
|
|||
.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(*)
|
||||
FROM [Recipe] INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
|
||||
WHERE [Group].[id] = $1 AND [user_id] = $2
|
||||
"#,
|
||||
)
|
||||
.bind(group_id)
|
||||
.bind(user_id)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn get_recipe(&self, id: i64) -> Result<Option<model::Recipe>> {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
|
|
@ -166,6 +181,88 @@ WHERE [Recipe].[user_id] = $1
|
|||
.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(
|
||||
r#"
|
||||
SELECT [id], [name], [comment]
|
||||
FROM [Group]
|
||||
WHERE [recipe_id] = $1
|
||||
ORDER BY [order]
|
||||
"#,
|
||||
)
|
||||
.bind(recipe_id)
|
||||
.fetch_all(&mut *tx)
|
||||
.await?;
|
||||
|
||||
for group in groups.iter_mut() {
|
||||
group.steps = sqlx::query_as(
|
||||
r#"
|
||||
SELECT [id], [action]
|
||||
FROM [Step]
|
||||
WHERE [group_id] = $1
|
||||
ORDER BY [order]
|
||||
"#,
|
||||
)
|
||||
.bind(group.id)
|
||||
.fetch_all(&mut *tx)
|
||||
.await?;
|
||||
|
||||
for step in group.steps.iter_mut() {
|
||||
step.ingredients = sqlx::query_as(
|
||||
r#"
|
||||
SELECT [id], [name], [comment], [quantity_value], [quantity_unit]
|
||||
FROM [Ingredient]
|
||||
WHERE [step_id] = $1
|
||||
ORDER BY [name]
|
||||
"#,
|
||||
)
|
||||
.bind(step.id)
|
||||
.fetch_all(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(groups)
|
||||
}
|
||||
|
||||
pub async fn add_recipe_group(&self, recipe_id: i64) -> Result<i64> {
|
||||
let db_result = sqlx::query("INSERT INTO [Group] ([recipe_id]) VALUES ($1)")
|
||||
.bind(recipe_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(db_result.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub async fn rm_recipe_group(&self, group_id: i64) -> Result<()> {
|
||||
sqlx::query("DELETE FROM [Group] WHERE [id] = $1")
|
||||
.bind(group_id)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_group_name(&self, group_id: i64, name: &str) -> Result<()> {
|
||||
sqlx::query("UPDATE [Group] SET [name] = $2 WHERE [id] = $1")
|
||||
.bind(group_id)
|
||||
.bind(name)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_group_comment(&self, group_id: i64, comment: &str) -> Result<()> {
|
||||
sqlx::query("UPDATE [Group] SET [comment] = $2 WHERE [id] = $1")
|
||||
.bind(group_id)
|
||||
.bind(comment)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -214,7 +311,7 @@ mod tests {
|
|||
assert_eq!(recipe.estimated_time, Some(420));
|
||||
assert_eq!(recipe.difficulty, Difficulty::Medium);
|
||||
assert_eq!(recipe.lang, "fr");
|
||||
assert_eq!(recipe.is_published, true);
|
||||
assert!(recipe.is_published);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue