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

@ -2,7 +2,7 @@ use askama::Template;
use crate::{
Context,
data::model,
data::{db, model},
translation::{self, Sentence, Tr},
};
@ -13,6 +13,24 @@ pub struct Recipes {
}
impl Recipes {
pub async fn new(
connection: db::Connection,
user: &Option<model::User>,
lang: &str,
) -> Result<Self, db::DBError> {
Ok(Recipes {
public: connection
.get_all_public_recipe_titles(lang, user.as_ref().map(|u| u.id))
.await?,
private: if let Some(user) = user {
connection.get_all_private_recipe_titles(user.id).await?
} else {
vec![]
},
current_id: None,
})
}
pub fn is_current(&self, id: &&i64) -> bool {
self.current_id == Some(**id)
}