Recipe edit (WIP)
This commit is contained in:
parent
fce4eade73
commit
c6dfff065c
24 changed files with 1157 additions and 971 deletions
|
|
@ -1,4 +1,8 @@
|
|||
use super::{model, Connection, DBError, Result};
|
||||
use super::{Connection, DBError, Result};
|
||||
use crate::{
|
||||
consts,
|
||||
data::model::{self, Difficulty},
|
||||
};
|
||||
|
||||
impl Connection {
|
||||
pub async fn get_all_recipe_titles(&self) -> Result<Vec<(i64, String)>> {
|
||||
|
|
@ -11,7 +15,10 @@ impl Connection {
|
|||
pub async fn get_recipe(&self, id: i64) -> Result<Option<model::Recipe>> {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
SELECT [id], [user_id], [title], [description]
|
||||
SELECT
|
||||
[id], [user_id], [title], [lang],
|
||||
[estimated_time], [description], [difficulty], [servings],
|
||||
[is_published]
|
||||
FROM [Recipe] WHERE [id] = $1
|
||||
"#,
|
||||
)
|
||||
|
|
@ -24,6 +31,7 @@ FROM [Recipe] WHERE [id] = $1
|
|||
pub async fn create_recipe(&self, user_id: i64) -> Result<i64> {
|
||||
let mut tx = self.tx().await?;
|
||||
|
||||
// Search for an existing empty recipe and return its id instead of creating a new one.
|
||||
match sqlx::query_scalar::<_, i64>(
|
||||
r#"
|
||||
SELECT [Recipe].[id] FROM [Recipe]
|
||||
|
|
@ -31,7 +39,7 @@ 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].[estimated_time] IS NULL
|
||||
AND [Recipe].[description] = ''
|
||||
AND [Image].[id] IS NULL
|
||||
AND [Group].[id] IS NULL
|
||||
|
|
@ -74,6 +82,57 @@ WHERE [Recipe].[user_id] = $1
|
|||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_recipe_estimated_time(
|
||||
&self,
|
||||
recipe_id: i64,
|
||||
estimated_time: Option<u32>,
|
||||
) -> Result<()> {
|
||||
sqlx::query("UPDATE [Recipe] SET [estimated_time] = $2 WHERE [id] = $1")
|
||||
.bind(recipe_id)
|
||||
.bind(estimated_time)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_recipe_difficulty(
|
||||
&self,
|
||||
recipe_id: i64,
|
||||
difficulty: Difficulty,
|
||||
) -> Result<()> {
|
||||
sqlx::query("UPDATE [Recipe] SET [difficulty] = $2 WHERE [id] = $1")
|
||||
.bind(recipe_id)
|
||||
.bind(u32::from(difficulty))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_recipe_language(&self, recipe_id: i64, lang: &str) -> Result<()> {
|
||||
if !consts::LANGUAGES.iter().any(|(_, l)| *l == lang) {
|
||||
return Err(DBError::UnknownLanguage(lang.to_string()));
|
||||
}
|
||||
sqlx::query("UPDATE [Recipe] SET [lang] = $2 WHERE [id] = $1")
|
||||
.bind(recipe_id)
|
||||
.bind(lang)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
pub async fn set_recipe_is_published(&self, recipe_id: i64, is_published: bool) -> Result<()> {
|
||||
sqlx::query("UPDATE [Recipe] SET [is_published] = $2 WHERE [id] = $1")
|
||||
.bind(recipe_id)
|
||||
.bind(is_published)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -84,6 +143,69 @@ mod tests {
|
|||
async fn create_a_new_recipe_then_update_its_title() -> Result<()> {
|
||||
let connection = Connection::new_in_memory().await?;
|
||||
|
||||
let user_id = create_a_user(&connection).await?;
|
||||
let recipe_id = connection.create_recipe(user_id).await?;
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn setters() -> Result<()> {
|
||||
let connection = Connection::new_in_memory().await?;
|
||||
|
||||
let user_id = create_a_user(&connection).await?;
|
||||
let recipe_id = connection.create_recipe(user_id).await?;
|
||||
|
||||
connection.set_recipe_title(recipe_id, "Ouiche").await?;
|
||||
connection
|
||||
.set_recipe_description(recipe_id, "C'est bon, mangez-en")
|
||||
.await?;
|
||||
connection
|
||||
.set_recipe_estimated_time(recipe_id, Some(420))
|
||||
.await?;
|
||||
connection
|
||||
.set_recipe_difficulty(recipe_id, Difficulty::Medium)
|
||||
.await?;
|
||||
connection.set_recipe_language(recipe_id, "fr").await?;
|
||||
connection.set_recipe_is_published(recipe_id, true).await?;
|
||||
|
||||
let recipe = connection.get_recipe(recipe_id).await?.unwrap();
|
||||
|
||||
assert_eq!(recipe.id, recipe_id);
|
||||
assert_eq!(recipe.title, "Ouiche");
|
||||
assert_eq!(recipe.description, "C'est bon, mangez-en");
|
||||
assert_eq!(recipe.estimated_time, Some(420));
|
||||
assert_eq!(recipe.difficulty, Difficulty::Medium);
|
||||
assert_eq!(recipe.lang, "fr");
|
||||
assert_eq!(recipe.is_published, true);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn set_nonexistent_language() -> Result<()> {
|
||||
let connection = Connection::new_in_memory().await?;
|
||||
|
||||
let user_id = create_a_user(&connection).await?;
|
||||
let recipe_id = connection.create_recipe(user_id).await?;
|
||||
|
||||
match connection.set_recipe_language(recipe_id, "asdf").await {
|
||||
// Nominal case.
|
||||
Err(DBError::UnknownLanguage(message)) => {
|
||||
println!("Ok: {}", message);
|
||||
}
|
||||
other => panic!("Set an nonexistent language must fail: {:?}", other),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_a_user(connection: &Connection) -> Result<i64> {
|
||||
let user_id = 1;
|
||||
connection.execute_sql(
|
||||
sqlx::query(
|
||||
r#"
|
||||
|
|
@ -93,33 +215,13 @@ VALUES
|
|||
($1, $2, $3, $4, $5, $6)
|
||||
"#
|
||||
)
|
||||
.bind(1)
|
||||
.bind(user_id)
|
||||
.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(())
|
||||
Ok(user_id)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ use chrono::{prelude::*, Duration};
|
|||
use rand::distributions::{Alphanumeric, DistString};
|
||||
use sqlx::Sqlite;
|
||||
|
||||
use super::{model, Connection, DBError, Result};
|
||||
use super::{Connection, DBError, Result};
|
||||
use crate::{
|
||||
consts,
|
||||
data::model,
|
||||
hash::{hash, verify_password},
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue