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
|
|
@ -73,9 +73,6 @@ body {
|
||||||
margin: consts.$margin;
|
margin: consts.$margin;
|
||||||
}
|
}
|
||||||
|
|
||||||
// .create-recipe {
|
|
||||||
// }
|
|
||||||
|
|
||||||
#select-website-language {
|
#select-website-language {
|
||||||
margin: consts.$margin;
|
margin: consts.$margin;
|
||||||
padding: consts.$margin;
|
padding: consts.$margin;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use askama::Template;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Context,
|
Context,
|
||||||
data::model,
|
data::{db, model},
|
||||||
translation::{self, Sentence, Tr},
|
translation::{self, Sentence, Tr},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -13,6 +13,24 @@ pub struct Recipes {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
pub fn is_current(&self, id: &&i64) -> bool {
|
||||||
self.current_id == Some(**id)
|
self.current_id == Some(**id)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,21 +20,12 @@ pub async fn recipes_list_fragments(
|
||||||
current_recipe: Query<CurrentRecipeId>,
|
current_recipe: Query<CurrentRecipeId>,
|
||||||
Extension(context): Extension<Context>,
|
Extension(context): Extension<Context>,
|
||||||
) -> Result<impl IntoResponse> {
|
) -> Result<impl IntoResponse> {
|
||||||
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: current_recipe.current_recipe_id,
|
|
||||||
};
|
|
||||||
Ok(Html(
|
Ok(Html(
|
||||||
RecipesListFragmentTemplate { context, recipes }.render()?,
|
RecipesListFragmentTemplate {
|
||||||
|
recipes: Recipes::new(connection, &context.user, context.tr.current_lang_code())
|
||||||
|
.await?,
|
||||||
|
context,
|
||||||
|
}
|
||||||
|
.render()?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,22 +50,31 @@ pub async fn home_page(
|
||||||
State(connection): State<db::Connection>,
|
State(connection): State<db::Connection>,
|
||||||
Extension(context): Extension<Context>,
|
Extension(context): Extension<Context>,
|
||||||
) -> Result<impl IntoResponse> {
|
) -> Result<impl IntoResponse> {
|
||||||
let recipes = Recipes {
|
Ok(Html(
|
||||||
public: connection
|
HomeTemplate {
|
||||||
.get_all_public_recipe_titles(
|
recipes: Recipes::new(connection, &context.user, context.tr.current_lang_code())
|
||||||
context.tr.current_lang_code(),
|
.await?,
|
||||||
context.user.as_ref().map(|u| u.id),
|
context,
|
||||||
)
|
}
|
||||||
.await?,
|
.render()?,
|
||||||
private: if let Some(user) = context.user.as_ref() {
|
))
|
||||||
connection.get_all_private_recipe_titles(user.id).await?
|
}
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
},
|
|
||||||
current_id: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Html(HomeTemplate { context, recipes }.render()?))
|
///// DEV_PANEL /////
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
pub async fn dev_panel(
|
||||||
|
State(connection): State<db::Connection>,
|
||||||
|
Extension(context): Extension<Context>,
|
||||||
|
) -> Result<impl IntoResponse> {
|
||||||
|
Ok(Html(
|
||||||
|
HomeTemplate {
|
||||||
|
recipes: Recipes::new(connection, &context.user, context.tr.current_lang_code())
|
||||||
|
.await?,
|
||||||
|
context,
|
||||||
|
}
|
||||||
|
.render()?,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
///// 404 /////
|
///// 404 /////
|
||||||
|
|
|
||||||
|
|
@ -43,20 +43,15 @@ pub async fn edit(
|
||||||
if let Some(ref user) = context.user {
|
if let Some(ref user) = context.user {
|
||||||
if let Some(recipe) = connection.get_recipe(recipe_id, false).await? {
|
if let Some(recipe) = connection.get_recipe(recipe_id, false).await? {
|
||||||
if model::can_user_edit_recipe(user, &recipe) {
|
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(
|
Ok(Html(
|
||||||
RecipeEditTemplate {
|
RecipeEditTemplate {
|
||||||
|
recipes: Recipes::new(
|
||||||
|
connection,
|
||||||
|
&context.user,
|
||||||
|
context.tr.current_lang_code(),
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
context,
|
context,
|
||||||
recipes,
|
|
||||||
recipe,
|
recipe,
|
||||||
}
|
}
|
||||||
.render()?,
|
.render()?,
|
||||||
|
|
@ -111,27 +106,15 @@ pub async fn view(
|
||||||
.into_response());
|
.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(
|
Ok(Html(
|
||||||
RecipeViewTemplate {
|
RecipeViewTemplate {
|
||||||
|
recipes: Recipes::new(
|
||||||
|
connection,
|
||||||
|
&context.user,
|
||||||
|
context.tr.current_lang_code(),
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
context,
|
context,
|
||||||
recipes,
|
|
||||||
recipe,
|
recipe,
|
||||||
}
|
}
|
||||||
.render()?,
|
.render()?,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue