Split db::Connection implementation in submodules (db::user and db::recipe).

This commit is contained in:
Greg Burri 2024-12-18 23:10:19 +01:00
parent 4248d11aa9
commit fce4eade73
17 changed files with 1307 additions and 1234 deletions

View file

@ -0,0 +1,125 @@
use super::{model, Connection, DBError, Result};
impl Connection {
pub async fn get_all_recipe_titles(&self) -> Result<Vec<(i64, String)>> {
sqlx::query_as("SELECT [id], [title] FROM [Recipe] ORDER BY [title]")
.fetch_all(&self.pool)
.await
.map_err(DBError::from)
}
pub async fn get_recipe(&self, id: i64) -> Result<Option<model::Recipe>> {
sqlx::query_as(
r#"
SELECT [id], [user_id], [title], [description]
FROM [Recipe] WHERE [id] = $1
"#,
)
.bind(id)
.fetch_optional(&self.pool)
.await
.map_err(DBError::from)
}
pub async fn create_recipe(&self, user_id: i64) -> Result<i64> {
let mut tx = self.tx().await?;
match sqlx::query_scalar::<_, i64>(
r#"
SELECT [Recipe].[id] FROM [Recipe]
LEFT JOIN [Image] ON [Image].[recipe_id] = [Recipe].[id]
LEFT JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
WHERE [Recipe].[user_id] = $1
AND [Recipe].[title] = ''
AND [Recipe].[estimate_time] IS NULL
AND [Recipe].[description] = ''
AND [Image].[id] IS NULL
AND [Group].[id] IS NULL
"#,
)
.bind(user_id)
.fetch_optional(&mut *tx)
.await?
{
Some(recipe_id) => Ok(recipe_id),
None => {
let db_result =
sqlx::query("INSERT INTO [Recipe] ([user_id], [title]) VALUES ($1, '')")
.bind(user_id)
.execute(&mut *tx)
.await?;
tx.commit().await?;
Ok(db_result.last_insert_rowid())
}
}
}
pub async fn set_recipe_title(&self, recipe_id: i64, title: &str) -> Result<()> {
sqlx::query("UPDATE [Recipe] SET [title] = $2 WHERE [id] = $1")
.bind(recipe_id)
.bind(title)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
}
pub async fn set_recipe_description(&self, recipe_id: i64, description: &str) -> Result<()> {
sqlx::query("UPDATE [Recipe] SET [description] = $2 WHERE [id] = $1")
.bind(recipe_id)
.bind(description)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(DBError::from)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn create_a_new_recipe_then_update_its_title() -> Result<()> {
let connection = Connection::new_in_memory().await?;
connection.execute_sql(
sqlx::query(
r#"
INSERT INTO [User]
([id], [email], [name], [password], [validation_token_datetime], [validation_token])
VALUES
($1, $2, $3, $4, $5, $6)
"#
)
.bind(1)
.bind("paul@atreides.com")
.bind("paul")
.bind("$argon2id$v=19$m=4096,t=3,p=1$G4fjepS05MkRbTqEImUdYg$GGziE8uVQe1L1oFHk37lBno10g4VISnVqynSkLCH3Lc")
.bind("2022-11-29 22:05:04.121407300+00:00")
.bind(None::<&str>) // 'null'.
).await?;
match connection.create_recipe(2).await {
Err(DBError::Sqlx(sqlx::Error::Database(err))) => {
// SQLITE_CONSTRAINT_FOREIGNKEY
// https://www.sqlite.org/rescode.html#constraint_foreignkey
assert_eq!(err.code(), Some(std::borrow::Cow::from("787")));
} // Nominal case. TODO: check 'err' value.
other => panic!(
"Creating a recipe with an inexistant user must fail: {:?}",
other
),
}
let recipe_id = connection.create_recipe(1).await?;
assert_eq!(recipe_id, 1);
connection.set_recipe_title(recipe_id, "Crêpe").await?;
let recipe = connection.get_recipe(recipe_id).await?.unwrap();
assert_eq!(recipe.title, "Crêpe".to_string());
Ok(())
}
}