Ingredients can now be manually ordered
This commit is contained in:
parent
afd42ba1d0
commit
ca2227037f
11 changed files with 205 additions and 32 deletions
|
|
@ -172,6 +172,33 @@ WHERE [Ingredient].[id] = $1 AND [user_id] = $2
|
|||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn can_edit_recipe_all_ingredients(
|
||||
&self,
|
||||
user_id: i64,
|
||||
ingredients_ids: &[i64],
|
||||
) -> Result<bool> {
|
||||
let params = (0..ingredients_ids.len())
|
||||
.map(|n| format!("${}", n + 2))
|
||||
.join(", ");
|
||||
let query_str = format!(
|
||||
r#"
|
||||
SELECT COUNT(*)
|
||||
FROM [Recipe]
|
||||
INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
|
||||
INNER JOIN [Step] ON [Step].[group_id] = [Group].[id]
|
||||
INNER JOIN [Ingredient] ON [Ingredient].[step_id] = [Step].[id]
|
||||
WHERE [Ingredient].[id] IN ({}) AND [user_id] = $1
|
||||
"#,
|
||||
params
|
||||
);
|
||||
|
||||
let mut query = sqlx::query_scalar::<_, u64>(&query_str).bind(user_id);
|
||||
for id in ingredients_ids {
|
||||
query = query.bind(id);
|
||||
}
|
||||
Ok(query.fetch_one(&self.pool).await? == ingredients_ids.len() as u64)
|
||||
}
|
||||
|
||||
pub async fn get_recipe(&self, id: i64, complete: bool) -> Result<Option<model::Recipe>> {
|
||||
match sqlx::query_as::<_, model::Recipe>(
|
||||
r#"
|
||||
|
|
@ -485,7 +512,7 @@ ORDER BY [order]
|
|||
SELECT [id], [name], [comment], [quantity_value], [quantity_unit]
|
||||
FROM [Ingredient]
|
||||
WHERE [step_id] = $1
|
||||
ORDER BY [name]
|
||||
ORDER BY [order]
|
||||
"#,
|
||||
)
|
||||
.bind(step.id)
|
||||
|
|
@ -622,10 +649,27 @@ ORDER BY [name]
|
|||
}
|
||||
|
||||
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?;
|
||||
let mut tx = self.tx().await?;
|
||||
|
||||
let last_order = sqlx::query_scalar(
|
||||
"SELECT [order] FROM [Ingredient] WHERE [step_id] = $1 ORDER BY [order] DESC LIMIT 1",
|
||||
)
|
||||
.bind(step_id)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.unwrap_or(-1);
|
||||
|
||||
let db_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO [Ingredient] ([step_id], [order])
|
||||
VALUES ($1, $2)
|
||||
"#,
|
||||
)
|
||||
.bind(step_id)
|
||||
.bind(last_order as i64)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
Ok(db_result.last_insert_rowid())
|
||||
}
|
||||
|
||||
|
|
@ -681,6 +725,22 @@ ORDER BY [name]
|
|||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_ingredients_order(&self, ingredient_ids: &[i64]) -> Result<()> {
|
||||
let mut tx = self.tx().await?;
|
||||
|
||||
for (order, id) in ingredient_ids.iter().enumerate() {
|
||||
sqlx::query("UPDATE [Ingredient] SET [order] = $2 WHERE [id] = $1")
|
||||
.bind(id)
|
||||
.bind(order as i64)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue