Groups can now be ordered (via drag and drop)

This commit is contained in:
Greg Burri 2025-01-10 22:38:34 +01:00
parent 16c484c2d1
commit 975d1ceee2
14 changed files with 461 additions and 54 deletions

View file

@ -1,3 +1,6 @@
use chrono::prelude::*;
use itertools::Itertools;
use super::{Connection, DBError, Result};
use crate::data::model;
@ -17,7 +20,7 @@ impl Connection {
SELECT [id], [title]
FROM [Recipe]
WHERE [is_published] = true AND ([lang] = $1 OR [user_id] = $2)
ORDER BY [title]
ORDER BY [title] COLLATE NOCASE
"#,
)
.bind(lang)
@ -28,7 +31,7 @@ impl Connection {
SELECT [id], [title]
FROM [Recipe]
WHERE [is_published] = true AND [lang] = $1
ORDER BY [title]
ORDER BY [title] COLLATE NOCASE
"#,
)
.bind(lang)
@ -83,6 +86,31 @@ WHERE [Group].[id] = $1 AND [user_id] = $2
.map_err(DBError::from)
}
pub async fn can_edit_recipe_all_groups(
&self,
user_id: i64,
group_ids: &[i64],
) -> Result<bool> {
let params = (0..group_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]
WHERE [Group].[id] IN ({}) AND [user_id] = $1
"#,
params
);
let mut query = sqlx::query_scalar::<_, u64>(&query_str).bind(user_id);
for id in group_ids {
query = query.bind(id);
}
Ok(query.fetch_one(&self.pool).await? == group_ids.len() as u64)
}
pub async fn can_edit_recipe_step(&self, user_id: i64, step_id: i64) -> Result<bool> {
sqlx::query_scalar(
r#"
@ -173,10 +201,11 @@ WHERE [Recipe].[user_id] = $1
.await?;
let db_result = sqlx::query(
"INSERT INTO [Recipe] ([user_id], [lang], [title]) VALUES ($1, $2, '')",
"INSERT INTO [Recipe] ([user_id], [lang], [title], [creation_datetime]) VALUES ($1, $2, '', $3)",
)
.bind(user_id)
.bind(lang)
.bind(Utc::now())
.execute(&mut *tx)
.await?;
@ -482,6 +511,22 @@ ORDER BY [name]
.map_err(DBError::from)
}
pub async fn set_groups_order(&self, group_ids: &[i64]) -> Result<()> {
let mut tx = self.tx().await?;
for (order, id) in group_ids.iter().enumerate() {
sqlx::query("UPDATE [Group] SET [order] = $2 WHERE [id] = $1")
.bind(id)
.bind(order as i64)
.execute(&mut *tx)
.await?;
}
tx.commit().await?;
Ok(())
}
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)