Toast message when scheduling a recipe

This commit is contained in:
Greg Burri 2025-02-05 15:44:48 +01:00
parent fbef990022
commit ccb1248da3
11 changed files with 168 additions and 66 deletions

View file

@ -2,7 +2,7 @@ use axum::{
debug_handler,
extract::{Extension, Query, State},
http::{HeaderMap, StatusCode},
response::{ErrorResponse, IntoResponse, Result},
response::{ErrorResponse, IntoResponse, Response, Result},
};
use axum_extra::extract::cookie::{Cookie, CookieJar};
use chrono::NaiveDate;
@ -11,10 +11,10 @@ use serde::Deserialize;
use crate::{
consts,
data::db,
data::{self, db},
model,
ron_extractor::ExtractRon,
ron_utils::{ron_error, ron_response},
ron_utils::{ron_error, ron_response_ok},
};
const NOT_AUTHORIZED_MESSAGE: &str = "Action not authorized";
@ -257,13 +257,10 @@ pub async fn get_tags(
State(connection): State<db::Connection>,
recipe_id: Query<RecipeId>,
) -> Result<impl IntoResponse> {
Ok(ron_response(
StatusCode::OK,
common::ron_api::Tags {
recipe_id: recipe_id.id,
tags: connection.get_recipes_tags(recipe_id.id).await?,
},
))
Ok(ron_response_ok(common::ron_api::Tags {
recipe_id: recipe_id.id,
tags: connection.get_recipes_tags(recipe_id.id).await?,
}))
}
#[debug_handler]
@ -401,8 +398,7 @@ pub async fn get_groups(
recipe_id: Query<RecipeId>,
) -> Result<impl IntoResponse> {
// Here we don't check user rights on purpose.
Ok(ron_response(
StatusCode::OK,
Ok(ron_response_ok(
connection
.get_groups(recipe_id.id)
.await?
@ -421,7 +417,7 @@ pub async fn add_group(
check_user_rights_recipe(&connection, &user, ron.id).await?;
let id = connection.add_recipe_group(ron.id).await?;
Ok(ron_response(StatusCode::OK, common::ron_api::Id { id }))
Ok(ron_response_ok(common::ron_api::Id { id }))
}
#[debug_handler]
@ -479,7 +475,7 @@ pub async fn add_step(
check_user_rights_recipe_group(&connection, &user, ron.id).await?;
let id = connection.add_recipe_step(ron.id).await?;
Ok(ron_response(StatusCode::OK, common::ron_api::Id { id }))
Ok(ron_response_ok(common::ron_api::Id { id }))
}
#[debug_handler]
@ -523,7 +519,7 @@ pub async fn add_ingredient(
) -> Result<impl IntoResponse> {
check_user_rights_recipe_step(&connection, &user, ron.id).await?;
let id = connection.add_recipe_ingredient(ron.id).await?;
Ok(ron_response(StatusCode::OK, common::ron_api::Id { id }))
Ok(ron_response_ok(common::ron_api::Id { id }))
}
#[debug_handler]
@ -615,14 +611,11 @@ pub async fn get_scheduled_recipes(
date_range: Query<DateRange>,
) -> Result<impl IntoResponse> {
if let Some(user) = user {
Ok(ron_response(
StatusCode::OK,
common::ron_api::ScheduledRecipes {
recipes: connection
.get_scheduled_recipes(user.id, date_range.start_date, date_range.end_date)
.await?,
},
))
Ok(ron_response_ok(common::ron_api::ScheduledRecipes {
recipes: connection
.get_scheduled_recipes(user.id, date_range.start_date, date_range.end_date)
.await?,
}))
} else {
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
@ -631,19 +624,35 @@ pub async fn get_scheduled_recipes(
}
}
impl From<data::db::recipe::AddScheduledRecipeResult> for common::ron_api::ScheduleRecipeResult {
fn from(db_res: data::db::recipe::AddScheduledRecipeResult) -> Self {
match db_res {
db::recipe::AddScheduledRecipeResult::Ok => Self::Ok,
db::recipe::AddScheduledRecipeResult::RecipeAlreadyScheduledAtThisDate => {
Self::RecipeAlreadyScheduledAtThisDate
}
}
}
}
#[debug_handler]
pub async fn schedule_recipe(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::ScheduleRecipe>,
) -> Result<impl IntoResponse> {
) -> Result<Response> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
if let Some(user) = user {
connection
.add_scheduled_recipe(user.id, ron.recipe_id, ron.date, ron.servings)
.await?;
.await
.map(|res| {
ron_response_ok(common::ron_api::ScheduleRecipeResult::from(res)).into_response()
})
.map_err(ErrorResponse::from)
} else {
Ok(StatusCode::OK.into_response())
}
Ok(StatusCode::OK)
}
#[debug_handler]