Update to Axum 0.8
This commit is contained in:
parent
975d1ceee2
commit
e355800f98
20 changed files with 1377 additions and 1199 deletions
|
|
@ -11,7 +11,6 @@ use sqlx::{
|
|||
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
|
||||
Pool, Sqlite, Transaction,
|
||||
};
|
||||
use thiserror::Error;
|
||||
use tracing::{event, Level};
|
||||
|
||||
use crate::consts;
|
||||
|
|
@ -21,7 +20,7 @@ pub mod user;
|
|||
|
||||
const CURRENT_DB_VERSION: u32 = 1;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum DBError {
|
||||
#[error("Sqlx error: {0}")]
|
||||
Sqlx(#[from] sqlx::Error),
|
||||
|
|
|
|||
|
|
@ -128,6 +128,28 @@ WHERE [Step].[id] = $1 AND [user_id] = $2
|
|||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn can_edit_recipe_all_steps(&self, user_id: i64, steps_ids: &[i64]) -> Result<bool> {
|
||||
let params = (0..steps_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]
|
||||
WHERE [Step].[id] IN ({}) AND [user_id] = $1
|
||||
"#,
|
||||
params
|
||||
);
|
||||
|
||||
let mut query = sqlx::query_scalar::<_, u64>(&query_str).bind(user_id);
|
||||
for id in steps_ids {
|
||||
query = query.bind(id);
|
||||
}
|
||||
Ok(query.fetch_one(&self.pool).await? == steps_ids.len() as u64)
|
||||
}
|
||||
|
||||
pub async fn can_edit_recipe_ingredient(
|
||||
&self,
|
||||
user_id: i64,
|
||||
|
|
@ -475,10 +497,22 @@ ORDER BY [name]
|
|||
}
|
||||
|
||||
pub async fn add_recipe_group(&self, recipe_id: i64) -> Result<i64> {
|
||||
let db_result = sqlx::query("INSERT INTO [Group] ([recipe_id]) VALUES ($1)")
|
||||
let mut tx = self.tx().await?;
|
||||
|
||||
let last_order = sqlx::query_scalar(
|
||||
"SELECT [order] FROM [Group] WHERE [recipe_id] = $1 ORDER BY [order] DESC LIMIT 1",
|
||||
)
|
||||
.bind(recipe_id)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.unwrap_or(-1);
|
||||
|
||||
let db_result = sqlx::query("INSERT INTO [Group] ([recipe_id, [order]) VALUES ($1, $2)")
|
||||
.bind(recipe_id)
|
||||
.execute(&self.pool)
|
||||
.bind(last_order + 1)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
Ok(db_result.last_insert_rowid())
|
||||
}
|
||||
|
||||
|
|
@ -554,6 +588,22 @@ ORDER BY [name]
|
|||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_steps_order(&self, step_ids: &[i64]) -> Result<()> {
|
||||
let mut tx = self.tx().await?;
|
||||
|
||||
for (order, id) in step_ids.iter().enumerate() {
|
||||
sqlx::query("UPDATE [Step] 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_ingredient(&self, step_id: i64) -> Result<i64> {
|
||||
let db_result = sqlx::query("INSERT INTO [Ingredient] ([step_id]) VALUES ($1)")
|
||||
.bind(step_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue