Recipe edit (WIP)

This commit is contained in:
Greg Burri 2024-12-21 23:13:06 +01:00
parent fce4eade73
commit c6dfff065c
24 changed files with 1157 additions and 971 deletions

View file

@ -13,7 +13,6 @@ use sqlx::{
use thiserror::Error;
use tracing::{event, Level};
use super::model;
use crate::consts;
pub mod recipe;
@ -32,6 +31,9 @@ pub enum DBError {
)]
UnsupportedVersion(u32),
#[error("Unknown language: {0}")]
UnknownLanguage(String),
#[error("Unknown error: {0}")]
Other(String),
}

View file

@ -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)
}
}

View file

@ -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},
};

View file

@ -1,3 +1,2 @@
pub mod db;
pub mod model;
mod utils;

View file

@ -1,77 +1,78 @@
use chrono::prelude::*;
use sqlx::{self, FromRow};
#[derive(Debug, Clone)]
#[derive(Debug, Clone, FromRow)]
pub struct User {
pub id: i64,
pub name: String,
pub email: String,
}
#[derive(FromRow)]
pub struct UserLoginInfo {
pub last_login_datetime: DateTime<Utc>,
pub ip: String,
pub user_agent: String,
}
#[derive(FromRow)]
pub struct Recipe {
pub id: i64,
pub user_id: i64,
pub title: String,
pub description: String,
pub estimate_time: Option<i32>, // [s].
pub difficulty: Difficulty,
pub lang: String,
pub estimated_time: Option<u32>, // [s].
pub description: String,
//ingredients: Vec<Ingredient>, // For four people.
pub process: Vec<Group>,
}
#[sqlx(try_from = "u32")]
pub difficulty: Difficulty,
impl Recipe {
pub fn empty(id: i64, user_id: i64) -> Recipe {
Self::new(id, user_id, String::new(), String::new())
}
pub fn new(id: i64, user_id: i64, title: String, description: String) -> Recipe {
Recipe {
id,
user_id,
title,
description,
estimate_time: None,
difficulty: Difficulty::Unknown,
lang: "en".to_string(),
process: Vec::new(),
}
}
}
pub struct Ingredient {
pub quantity: Option<Quantity>,
pub name: String,
}
pub struct Quantity {
pub value: f32,
pub unit: String,
pub servings: u32,
pub is_published: bool,
// pub tags: Vec<String>,
// pub groups: Vec<Group>,
}
pub struct Group {
pub name: Option<String>,
pub input: Vec<StepInput>,
pub name: String,
pub comment: String,
pub steps: Vec<Step>,
}
pub struct Step {
pub action: String,
pub ingredients: Vec<Ingredient>,
}
pub enum StepInput {
Ingredient(Ingredient),
pub struct Ingredient {
pub name: String,
pub comment: String,
pub quantity: i32,
pub quantity_unit: String,
}
#[derive(PartialEq, Debug)]
pub enum Difficulty {
Unknown = 0,
Easy = 1,
Medium = 2,
Hard = 3,
}
impl TryFrom<u32> for Difficulty {
type Error = &'static str;
fn try_from(value: u32) -> Result<Self, Self::Error> {
Ok(match value {
1 => Self::Easy,
2 => Self::Medium,
3 => Self::Hard,
_ => Self::Unknown,
})
}
}
impl From<Difficulty> for u32 {
fn from(value: Difficulty) -> Self {
value as u32
}
}

View file

@ -1,34 +0,0 @@
use sqlx::{sqlite::SqliteRow, FromRow, Row};
use super::model;
impl FromRow<'_, SqliteRow> for model::Recipe {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
Ok(model::Recipe::new(
row.try_get("id")?,
row.try_get("user_id")?,
row.try_get("title")?,
row.try_get("description")?,
))
}
}
impl FromRow<'_, SqliteRow> for model::UserLoginInfo {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
Ok(model::UserLoginInfo {
last_login_datetime: row.try_get("last_login_datetime")?,
ip: row.try_get("ip")?,
user_agent: row.try_get("user_agent")?,
})
}
}
impl FromRow<'_, SqliteRow> for model::User {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
Ok(model::User {
id: row.try_get("id")?,
email: row.try_get("email")?,
name: row.try_get("name")?,
})
}
}