Refactor for clearer code

This commit is contained in:
Greg Burri 2025-05-01 16:16:05 +02:00
parent 8be2aa0129
commit 630ad418bd

View file

@ -10,17 +10,12 @@ pub async fn check_user_rights_recipe(
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(
match user {
Some(user) if connection.can_edit_recipe(user.id, recipe_id).await? => Ok(()),
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -29,17 +24,12 @@ pub async fn check_user_rights_recipe_group(
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(
match user {
Some(user) if connection.can_edit_recipe_group(user.id, group_id).await? => Ok(()),
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -48,17 +38,18 @@ pub async fn check_user_rights_recipe_groups(
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(
match user {
Some(user)
if connection
.can_edit_recipe_all_groups(user.id, group_ids)
.await? =>
{
Ok(())
}
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -67,17 +58,12 @@ pub async fn check_user_rights_recipe_step(
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(
match user {
Some(user) if connection.can_edit_recipe_step(user.id, step_id).await? => Ok(()),
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -86,17 +72,18 @@ pub async fn check_user_rights_recipe_steps(
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(
match user {
Some(user)
if connection
.can_edit_recipe_all_steps(user.id, step_ids)
.await? =>
{
Ok(())
}
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -105,17 +92,18 @@ pub async fn check_user_rights_recipe_ingredient(
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(
match user {
Some(user)
if connection
.can_edit_recipe_ingredient(user.id, ingredient_id)
.await? =>
{
Ok(())
}
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -124,17 +112,18 @@ pub async fn check_user_rights_recipe_ingredients(
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(
match user {
Some(user)
if connection
.can_edit_recipe_all_ingredients(user.id, ingredient_ids)
.await? =>
{
Ok(())
}
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}
@ -143,16 +132,17 @@ pub async fn check_user_rights_shopping_list_entry(
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(
match user {
Some(user)
if connection
.can_edit_shopping_list_entry(user.id, entry_id)
.await? =>
{
Ok(())
}
_ => Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
))),
}
}