Add a way to delete recipe
This commit is contained in:
parent
5ce3391466
commit
31bc31035a
10 changed files with 247 additions and 175 deletions
|
|
@ -4,10 +4,11 @@ use std::{
|
|||
io::Read,
|
||||
path::Path,
|
||||
str::FromStr,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use sqlx::{
|
||||
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions},
|
||||
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
|
||||
Pool, Sqlite, Transaction,
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
|
@ -75,8 +76,9 @@ impl Connection {
|
|||
))?
|
||||
.journal_mode(SqliteJournalMode::Wal) // TODO: use 'Wal2' when available.
|
||||
.create_if_missing(true)
|
||||
.pragma("foreign_keys", "ON")
|
||||
.pragma("synchronous", "NORMAL");
|
||||
.busy_timeout(Duration::from_secs(10))
|
||||
.foreign_keys(true)
|
||||
.synchronous(SqliteSynchronous::Normal);
|
||||
|
||||
Self::create_connection(
|
||||
SqlitePoolOptions::new()
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ pub struct Recipe {
|
|||
#[sqlx(try_from = "u32")]
|
||||
pub difficulty: Difficulty,
|
||||
|
||||
pub servings: u32,
|
||||
pub servings: Option<u32>,
|
||||
pub is_published: bool,
|
||||
// pub tags: Vec<String>,
|
||||
// pub groups: Vec<Group>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue