Refactor recipe fetching logic to use a new constructor for Recipes, improving code clarity and reducing duplication.

This commit is contained in:
Greg Burri 2025-04-12 18:45:51 +02:00
parent aebca7a7e2
commit 9daa852add
5 changed files with 61 additions and 63 deletions

View file

@ -43,20 +43,15 @@ pub async fn edit(
if let Some(ref user) = context.user {
if let Some(recipe) = connection.get_recipe(recipe_id, false).await? {
if model::can_user_edit_recipe(user, &recipe) {
let recipes = Recipes {
public: connection
.get_all_public_recipe_titles(context.tr.current_lang_code(), Some(user.id))
.await?,
private: connection
.get_all_private_recipe_titles(user.id)
.await?,
current_id: Some(recipe_id),
};
Ok(Html(
RecipeEditTemplate {
recipes: Recipes::new(
connection,
&context.user,
context.tr.current_lang_code(),
)
.await?,
context,
recipes,
recipe,
}
.render()?,
@ -111,27 +106,15 @@ pub async fn view(
.into_response());
}
let recipes = Recipes {
public: connection
.get_all_public_recipe_titles(
context.tr.current_lang_code(),
context.user.as_ref().map(|u| u.id),
)
.await?,
private: if let Some(user) = context.user.as_ref() {
connection
.get_all_private_recipe_titles(user.id)
.await?
} else {
vec![]
},
current_id: Some(recipe_id),
};
Ok(Html(
RecipeViewTemplate {
recipes: Recipes::new(
connection,
&context.user,
context.tr.current_lang_code(),
)
.await?,
context,
recipes,
recipe,
}
.render()?,