Shopping list items can now be checked/unchecked

This commit is contained in:
Greg Burri 2025-02-12 23:13:48 +01:00
parent 3a3288bc93
commit a1fd63ad08
14 changed files with 940 additions and 790 deletions

View file

@ -0,0 +1,94 @@
use axum::{
debug_handler,
extract::{Extension, State},
http::StatusCode,
response::{ErrorResponse, IntoResponse, Response, Result},
};
use axum_extra::extract::Query;
// use tracing::{event, Level};
use crate::{
data::{self, db},
model,
ron_extractor::ExtractRon,
ron_utils::{ron_error, ron_response_ok},
};
use super::rights::*;
#[debug_handler]
pub async fn get_scheduled_recipes(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
date_range: Query<common::ron_api::DateRange>,
) -> Result<impl IntoResponse> {
if let Some(user) = user {
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,
super::NOT_AUTHORIZED_MESSAGE,
)))
}
}
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<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,
ron.add_ingredients_to_shopping_list,
)
.await
.map(|res| {
ron_response_ok(common::ron_api::ScheduleRecipeResult::from(res)).into_response()
})
.map_err(ErrorResponse::from)
} else {
Ok(StatusCode::OK.into_response())
}
}
#[debug_handler]
pub async fn rm_scheduled_recipe(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::RemoveScheduledRecipe>,
) -> Result<impl IntoResponse> {
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
if let Some(user) = user {
connection
.rm_scheduled_recipe(
user.id,
ron.recipe_id,
ron.date,
ron.remove_ingredients_from_shopping_list,
)
.await?;
}
Ok(StatusCode::OK)
}