Toast message when scheduling a recipe
This commit is contained in:
parent
fbef990022
commit
ccb1248da3
11 changed files with 168 additions and 66 deletions
|
|
@ -1,9 +1,15 @@
|
|||
use chrono::{prelude::*, Days};
|
||||
use chrono::prelude::*;
|
||||
use common::ron_api::Difficulty;
|
||||
use itertools::Itertools;
|
||||
use sqlx::Error;
|
||||
|
||||
use super::{Connection, DBError, Result};
|
||||
use crate::{data::model, user_authentication};
|
||||
use crate::data::model;
|
||||
|
||||
pub enum AddScheduledRecipeResult {
|
||||
Ok,
|
||||
RecipeAlreadyScheduledAtThisDate,
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
/// Returns all the recipe titles where recipe is written in the given language.
|
||||
|
|
@ -758,8 +764,8 @@ VALUES ($1, $2)
|
|||
recipe_id: i64,
|
||||
date: NaiveDate,
|
||||
servings: u32,
|
||||
) -> Result<()> {
|
||||
sqlx::query(
|
||||
) -> Result<AddScheduledRecipeResult> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
INSERT INTO [RecipeScheduled] (user_id, recipe_id, date, servings)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
|
|
@ -771,8 +777,15 @@ VALUES ($1, $2, $3, $4)
|
|||
.bind(servings)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(DBError::from)
|
||||
{
|
||||
Err(Error::Database(error))
|
||||
if error.code() == Some(std::borrow::Cow::Borrowed("2067"))
|
||||
&& error.message() == "UNIQUE constraint failed: RecipeScheduled.user_id, RecipeScheduled.recipe_id, RecipeScheduled.date" =>
|
||||
{
|
||||
Ok(AddScheduledRecipeResult::RecipeAlreadyScheduledAtThisDate)
|
||||
}
|
||||
_ => Ok(AddScheduledRecipeResult::Ok),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rm_scheduled_recipe(
|
||||
|
|
@ -783,9 +796,9 @@ VALUES ($1, $2, $3, $4)
|
|||
) -> Result<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
DELETE FROM [RecipeScheduled]
|
||||
WHERE [user_id] = $1 AND [recipe_id] = $2 AND [date] = $3
|
||||
"#,
|
||||
DELETE FROM [RecipeScheduled]
|
||||
WHERE [user_id] = $1 AND [recipe_id] = $2 AND [date] = $3
|
||||
"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(recipe_id)
|
||||
|
|
@ -823,6 +836,7 @@ ORDER BY [date]
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Days;
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_a_new_recipe_then_update_its_title() -> Result<()> {
|
||||
|
|
@ -1007,6 +1021,14 @@ VALUES
|
|||
]
|
||||
);
|
||||
|
||||
// Recipe scheduled at the same date is forbidden.
|
||||
let Ok(AddScheduledRecipeResult::RecipeAlreadyScheduledAtThisDate) = connection
|
||||
.add_scheduled_recipe(user_id, recipe_id_1, today, 4)
|
||||
.await
|
||||
else {
|
||||
panic!("DBError::RecipeAlreadyScheduledAtThisDate must be returned");
|
||||
};
|
||||
|
||||
connection
|
||||
.rm_scheduled_recipe(user_id, recipe_id_1, today)
|
||||
.await?;
|
||||
|
|
|
|||
|
|
@ -945,6 +945,7 @@ VALUES
|
|||
1,
|
||||
Some("muaddib@fremen.com"),
|
||||
Some("muaddib"),
|
||||
None,
|
||||
Some("Chani"),
|
||||
)
|
||||
.await?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue