From 630ad418bd687809cae8f0a1a068b37a4e0cc1e6 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 1 May 2025 16:16:05 +0200 Subject: [PATCH] Refactor for clearer code --- backend/src/services/ron/rights.rs | 134 +++++++++++++---------------- 1 file changed, 62 insertions(+), 72 deletions(-) diff --git a/backend/src/services/ron/rights.rs b/backend/src/services/ron/rights.rs index 8e64109..30c58a6 100644 --- a/backend/src/services/ron/rights.rs +++ b/backend/src/services/ron/rights.rs @@ -10,17 +10,12 @@ pub async fn check_user_rights_recipe( user: &Option, 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, 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, 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, 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, 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, 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, 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, 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(()) + ))), } }