Refactor recipe fetching logic to use a new constructor for Recipes, improving code clarity and reducing duplication.
This commit is contained in:
parent
aebca7a7e2
commit
9daa852add
5 changed files with 61 additions and 63 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue