Shopping list items can now be checked/unchecked
This commit is contained in:
parent
3a3288bc93
commit
a1fd63ad08
14 changed files with 940 additions and 790 deletions
|
|
@ -1,744 +0,0 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{ErrorResponse, IntoResponse, Response, Result},
|
||||
};
|
||||
use axum_extra::extract::{
|
||||
cookie::{Cookie, CookieJar},
|
||||
Query,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use tracing::{event, Level};
|
||||
|
||||
use crate::{
|
||||
consts,
|
||||
data::{self, db},
|
||||
model,
|
||||
ron_extractor::ExtractRon,
|
||||
ron_utils::{ron_error, ron_response_ok},
|
||||
};
|
||||
|
||||
const NOT_AUTHORIZED_MESSAGE: &str = "Action not authorized";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Id {
|
||||
id: i64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Ids {
|
||||
ids: Vec<i64>,
|
||||
}
|
||||
|
||||
// #[allow(dead_code)]
|
||||
// #[debug_handler]
|
||||
// pub async fn update_user(
|
||||
// State(connection): State<db::Connection>,
|
||||
// Extension(user): Extension<Option<model::User>>,
|
||||
// ExtractRon(ron): ExtractRon<common::ron_api::UpdateProfile>,
|
||||
// ) -> Result<StatusCode> {
|
||||
// if let Some(user) = user {
|
||||
// connection
|
||||
// .update_user(
|
||||
// user.id,
|
||||
// ron.email.as_deref().map(str::trim),
|
||||
// ron.name.as_deref(),
|
||||
// ron.password.as_deref(),
|
||||
// )
|
||||
// .await?;
|
||||
// } else {
|
||||
// return Err(ErrorResponse::from(ron_error(
|
||||
// StatusCode::UNAUTHORIZED,
|
||||
// NOT_AUTHORIZED_MESSAGE,
|
||||
// )));
|
||||
// }
|
||||
// Ok(StatusCode::OK)
|
||||
// }
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_lang(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
headers: HeaderMap,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetLang>,
|
||||
) -> Result<(CookieJar, StatusCode)> {
|
||||
let mut jar = CookieJar::from_headers(&headers);
|
||||
if let Some(user) = user {
|
||||
connection.set_user_lang(user.id, &ron.lang).await?;
|
||||
} else {
|
||||
let cookie = Cookie::build((consts::COOKIE_LANG_NAME, ron.lang)).path("/");
|
||||
jar = jar.add(cookie);
|
||||
}
|
||||
Ok((jar, StatusCode::OK))
|
||||
}
|
||||
|
||||
/*** Rights ***/
|
||||
|
||||
async fn check_user_rights_recipe(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
recipe_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe(user.as_ref().unwrap().id, recipe_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe_group(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
group_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_group(user.as_ref().unwrap().id, group_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe_groups(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
group_ids: &[i64],
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_all_groups(user.as_ref().unwrap().id, group_ids)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe_step(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
step_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_step(user.as_ref().unwrap().id, step_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe_steps(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
step_ids: &[i64],
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_all_steps(user.as_ref().unwrap().id, step_ids)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe_ingredient(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
ingredient_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_ingredient(user.as_ref().unwrap().id, ingredient_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_user_rights_recipe_ingredients(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
ingredient_ids: &[i64],
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_all_ingredients(user.as_ref().unwrap().id, ingredient_ids)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/*** Recipe ***/
|
||||
|
||||
/// Ask recipe titles associated with each given id. The returned titles are in the same order
|
||||
/// as the given ids.
|
||||
#[debug_handler]
|
||||
pub async fn get_titles(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_ids: Query<Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(common::ron_api::Strings {
|
||||
strs: connection.get_recipe_titles(&recipe_ids.ids).await?,
|
||||
}))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_recipe_title(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeTitle>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_title(ron.recipe_id, &ron.title)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_recipe_description(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeDescription>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_description(ron.recipe_id, &ron.description)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_servings(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeServings>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_servings(ron.recipe_id, ron.servings)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_estimated_time(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeEstimatedTime>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_estimated_time(ron.recipe_id, ron.estimated_time)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn get_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_id: Query<Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(common::ron_api::Tags {
|
||||
recipe_id: recipe_id.id,
|
||||
tags: connection.get_recipes_tags(recipe_id.id).await?,
|
||||
}))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Tags>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.add_recipe_tags(
|
||||
ron.recipe_id,
|
||||
&ron.tags
|
||||
.into_iter()
|
||||
.map(|tag| tag.to_lowercase())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Tags>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection.rm_recipe_tags(ron.recipe_id, &ron.tags).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_difficulty(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeDifficulty>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_difficulty(ron.recipe_id, ron.difficulty)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_language(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeLanguage>,
|
||||
) -> Result<StatusCode> {
|
||||
if !crate::translation::available_codes()
|
||||
.iter()
|
||||
.any(|&l| l == ron.lang)
|
||||
{
|
||||
// TODO: log?
|
||||
return Ok(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_language(ron.recipe_id, &ron.lang)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_is_published(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetIsPublished>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_is_published(ron.recipe_id, ron.is_published)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
impl From<model::Group> for common::ron_api::Group {
|
||||
fn from(group: model::Group) -> Self {
|
||||
Self {
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
comment: group.comment,
|
||||
steps: group
|
||||
.steps
|
||||
.into_iter()
|
||||
.map(common::ron_api::Step::from)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<model::Step> for common::ron_api::Step {
|
||||
fn from(step: model::Step) -> Self {
|
||||
Self {
|
||||
id: step.id,
|
||||
action: step.action,
|
||||
ingredients: step
|
||||
.ingredients
|
||||
.into_iter()
|
||||
.map(common::ron_api::Ingredient::from)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<model::Ingredient> for common::ron_api::Ingredient {
|
||||
fn from(ingredient: model::Ingredient) -> Self {
|
||||
Self {
|
||||
id: ingredient.id,
|
||||
name: ingredient.name,
|
||||
comment: ingredient.comment,
|
||||
quantity_value: ingredient.quantity_value,
|
||||
quantity_unit: ingredient.quantity_unit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn get_groups(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_id: Query<Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
// Here we don't check user rights on purpose.
|
||||
Ok(ron_response_ok(
|
||||
connection
|
||||
.get_groups(recipe_id.id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(common::ron_api::Group::from)
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_group(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.id).await?;
|
||||
let id = connection.add_recipe_group(ron.id).await?;
|
||||
|
||||
Ok(ron_response_ok(common::ron_api::Id { id }))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_group(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe_group(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_group_name(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetGroupName>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
|
||||
connection.set_group_name(ron.group_id, &ron.name).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_group_comment(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetGroupComment>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
|
||||
connection
|
||||
.set_group_comment(ron.group_id, &ron.comment)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_groups_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_groups(&connection, &user, &ron.ids).await?;
|
||||
connection.set_groups_order(&ron.ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_step(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.id).await?;
|
||||
let id = connection.add_recipe_step(ron.id).await?;
|
||||
|
||||
Ok(ron_response_ok(common::ron_api::Id { id }))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_step(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe_step(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_step_action(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetStepAction>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
|
||||
connection.set_step_action(ron.step_id, &ron.action).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_steps_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_steps(&connection, &user, &ron.ids).await?;
|
||||
connection.set_steps_order(&ron.ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_ingredient(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> 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_ok(common::ron_api::Id { id }))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_ingredient(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe_ingredient(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_name(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientName>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_name(ron.ingredient_id, &ron.name)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_comment(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientComment>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_comment(ron.ingredient_id, &ron.comment)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_quantity(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientQuantity>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_quantity(ron.ingredient_id, ron.quantity)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_unit(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientUnit>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_unit(ron.ingredient_id, &ron.unit)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredients_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredients(&connection, &user, &ron.ids).await?;
|
||||
connection.set_ingredients_order(&ron.ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
/*** Calendar ***/
|
||||
|
||||
#[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,
|
||||
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)
|
||||
}
|
||||
|
||||
/*** Shopping list ***/
|
||||
|
||||
impl From<model::ShoppingListItem> for common::ron_api::ShoppingListItem {
|
||||
fn from(item: model::ShoppingListItem) -> Self {
|
||||
Self {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
quantity_value: item.quantity_value,
|
||||
quantity_unit: item.quantity_unit,
|
||||
recipe_id: item.recipe_id,
|
||||
recipe_title: item.recipe_title,
|
||||
date: item.date,
|
||||
is_checked: item.is_checked,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn get_shopping_list(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
if let Some(user) = user {
|
||||
Ok(ron_response_ok(
|
||||
connection
|
||||
.get_shopping_list(user.id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(common::ron_api::ShoppingListItem::from)
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
} else {
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/*** 404 ***/
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn not_found(Extension(_user): Extension<Option<model::User>>) -> impl IntoResponse {
|
||||
ron_error(StatusCode::NOT_FOUND, "Not found")
|
||||
}
|
||||
94
backend/src/services/ron/calendar.rs
Normal file
94
backend/src/services/ron/calendar.rs
Normal 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)
|
||||
}
|
||||
41
backend/src/services/ron/mod.rs
Normal file
41
backend/src/services/ron/mod.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{IntoResponse, Result},
|
||||
};
|
||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||||
// use tracing::{event, Level};
|
||||
|
||||
use crate::{consts, data::db, model, ron_extractor::ExtractRon, ron_utils::ron_error};
|
||||
|
||||
pub mod calendar;
|
||||
pub mod recipe;
|
||||
mod rights;
|
||||
pub mod shopping_list;
|
||||
|
||||
const NOT_AUTHORIZED_MESSAGE: &str = "Action not authorized";
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_lang(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
headers: HeaderMap,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetLang>,
|
||||
) -> Result<(CookieJar, StatusCode)> {
|
||||
let mut jar = CookieJar::from_headers(&headers);
|
||||
if let Some(user) = user {
|
||||
connection.set_user_lang(user.id, &ron.lang).await?;
|
||||
} else {
|
||||
let cookie = Cookie::build((consts::COOKIE_LANG_NAME, ron.lang)).path("/");
|
||||
jar = jar.add(cookie);
|
||||
}
|
||||
Ok((jar, StatusCode::OK))
|
||||
}
|
||||
|
||||
/*** 404 ***/
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn not_found(Extension(_user): Extension<Option<model::User>>) -> impl IntoResponse {
|
||||
ron_error(StatusCode::NOT_FOUND, "Not found")
|
||||
}
|
||||
417
backend/src/services/ron/recipe.rs
Normal file
417
backend/src/services/ron/recipe.rs
Normal file
|
|
@ -0,0 +1,417 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Result},
|
||||
};
|
||||
use axum_extra::extract::Query;
|
||||
use common::ron_api;
|
||||
// use tracing::{event, Level};
|
||||
|
||||
use crate::{data::db, model, ron_extractor::ExtractRon, ron_utils::ron_response_ok};
|
||||
|
||||
use super::rights::*;
|
||||
|
||||
/// Ask recipe titles associated with each given id. The returned titles are in the same order
|
||||
/// as the given ids.
|
||||
#[debug_handler]
|
||||
pub async fn get_titles(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_ids: Query<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(ron_api::Strings {
|
||||
strs: connection.get_recipe_titles(&recipe_ids.ids).await?,
|
||||
}))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_title(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeTitle>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_title(ron.recipe_id, &ron.title)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_description(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeDescription>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_description(ron.recipe_id, &ron.description)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_servings(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeServings>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_servings(ron.recipe_id, ron.servings)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_estimated_time(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeEstimatedTime>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_estimated_time(ron.recipe_id, ron.estimated_time)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn get_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_id: Query<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(ron_api::Tags {
|
||||
recipe_id: recipe_id.id,
|
||||
tags: connection.get_recipes_tags(recipe_id.id).await?,
|
||||
}))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Tags>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.add_recipe_tags(
|
||||
ron.recipe_id,
|
||||
&ron.tags
|
||||
.into_iter()
|
||||
.map(|tag| tag.to_lowercase())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Tags>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection.rm_recipe_tags(ron.recipe_id, &ron.tags).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_difficulty(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeDifficulty>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_difficulty(ron.recipe_id, ron.difficulty)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_language(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeLanguage>,
|
||||
) -> Result<StatusCode> {
|
||||
if !crate::translation::available_codes()
|
||||
.iter()
|
||||
.any(|&l| l == ron.lang)
|
||||
{
|
||||
// TODO: log?
|
||||
return Ok(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_language(ron.recipe_id, &ron.lang)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_is_published(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIsPublished>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_is_published(ron.recipe_id, ron.is_published)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
impl From<model::Group> for ron_api::Group {
|
||||
fn from(group: model::Group) -> Self {
|
||||
Self {
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
comment: group.comment,
|
||||
steps: group.steps.into_iter().map(ron_api::Step::from).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<model::Step> for ron_api::Step {
|
||||
fn from(step: model::Step) -> Self {
|
||||
Self {
|
||||
id: step.id,
|
||||
action: step.action,
|
||||
ingredients: step
|
||||
.ingredients
|
||||
.into_iter()
|
||||
.map(ron_api::Ingredient::from)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<model::Ingredient> for ron_api::Ingredient {
|
||||
fn from(ingredient: model::Ingredient) -> Self {
|
||||
Self {
|
||||
id: ingredient.id,
|
||||
name: ingredient.name,
|
||||
comment: ingredient.comment,
|
||||
quantity_value: ingredient.quantity_value,
|
||||
quantity_unit: ingredient.quantity_unit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn get_groups(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_id: Query<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
// Here we don't check user rights on purpose.
|
||||
Ok(ron_response_ok(
|
||||
connection
|
||||
.get_groups(recipe_id.id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(ron_api::Group::from)
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_group(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &user, ron.id).await?;
|
||||
let id = connection.add_recipe_group(ron.id).await?;
|
||||
|
||||
Ok(ron_response_ok(ron_api::Id { id }))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_group(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe_group(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_group_name(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetGroupName>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
|
||||
connection.set_group_name(ron.group_id, &ron.name).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_group_comment(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetGroupComment>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.group_id).await?;
|
||||
connection
|
||||
.set_group_comment(ron.group_id, &ron.comment)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_groups_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_groups(&connection, &user, &ron.ids).await?;
|
||||
connection.set_groups_order(&ron.ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_step(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &user, ron.id).await?;
|
||||
let id = connection.add_recipe_step(ron.id).await?;
|
||||
|
||||
Ok(ron_response_ok(ron_api::Id { id }))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_step(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe_step(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_step_action(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetStepAction>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
|
||||
connection.set_step_action(ron.step_id, &ron.action).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_steps_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_steps(&connection, &user, &ron.ids).await?;
|
||||
connection.set_steps_order(&ron.ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_ingredient(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> 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_ok(ron_api::Id { id }))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_ingredient(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.id).await?;
|
||||
connection.rm_recipe_ingredient(ron.id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_name(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientName>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_name(ron.ingredient_id, &ron.name)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_comment(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientComment>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_comment(ron.ingredient_id, &ron.comment)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_quantity(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientQuantity>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_quantity(ron.ingredient_id, ron.quantity)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredient_unit(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientUnit>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_unit(ron.ingredient_id, &ron.unit)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredients_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredients(&connection, &user, &ron.ids).await?;
|
||||
connection.set_ingredients_order(&ron.ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
158
backend/src/services/ron/rights.rs
Normal file
158
backend/src/services/ron/rights.rs
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{ErrorResponse, Result},
|
||||
};
|
||||
|
||||
use crate::{data::db, model, ron_utils::ron_error};
|
||||
|
||||
pub async fn check_user_rights_recipe(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
recipe_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe(user.as_ref().unwrap().id, recipe_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_recipe_group(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
group_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_group(user.as_ref().unwrap().id, group_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_recipe_groups(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
group_ids: &[i64],
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_all_groups(user.as_ref().unwrap().id, group_ids)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_recipe_step(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
step_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_step(user.as_ref().unwrap().id, step_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_recipe_steps(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
step_ids: &[i64],
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_all_steps(user.as_ref().unwrap().id, step_ids)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_recipe_ingredient(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
ingredient_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_ingredient(user.as_ref().unwrap().id, ingredient_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_recipe_ingredients(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
ingredient_ids: &[i64],
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_recipe_all_ingredients(user.as_ref().unwrap().id, ingredient_ids)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_user_rights_shopping_list_entry(
|
||||
connection: &db::Connection,
|
||||
user: &Option<model::User>,
|
||||
entry_id: i64,
|
||||
) -> Result<()> {
|
||||
if user.is_none()
|
||||
|| !connection
|
||||
.can_edit_shopping_list_entry(user.as_ref().unwrap().id, entry_id)
|
||||
.await?
|
||||
{
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
65
backend/src/services/ron/shopping_list.rs
Normal file
65
backend/src/services/ron/shopping_list.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, State},
|
||||
http::StatusCode,
|
||||
response::{ErrorResponse, IntoResponse, Result},
|
||||
};
|
||||
use common::ron_api;
|
||||
|
||||
use crate::{
|
||||
data::db,
|
||||
model,
|
||||
ron_extractor::ExtractRon,
|
||||
ron_utils::{ron_error, ron_response_ok},
|
||||
};
|
||||
|
||||
use super::rights::*;
|
||||
|
||||
impl From<model::ShoppingListItem> for common::ron_api::ShoppingListItem {
|
||||
fn from(item: model::ShoppingListItem) -> Self {
|
||||
Self {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
quantity_value: item.quantity_value,
|
||||
quantity_unit: item.quantity_unit,
|
||||
recipe_id: item.recipe_id,
|
||||
recipe_title: item.recipe_title,
|
||||
date: item.date,
|
||||
is_checked: item.is_checked,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn get(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
if let Some(user) = user {
|
||||
Ok(ron_response_ok(
|
||||
connection
|
||||
.get_shopping_list(user.id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(common::ron_api::ShoppingListItem::from)
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
} else {
|
||||
Err(ErrorResponse::from(ron_error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
super::NOT_AUTHORIZED_MESSAGE,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_entry_checked(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Value<bool>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_shopping_list_entry(&connection, &user, ron.id).await?;
|
||||
Ok(ron_response_ok(
|
||||
connection.set_entry_checked(ron.id, ron.value).await?,
|
||||
))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue