recipes/backend/src/services/fragments.rs
2025-01-14 15:57:02 +01:00

42 lines
1.1 KiB
Rust

use axum::{
debug_handler,
extract::{Extension, Query, State},
response::{Html, IntoResponse},
};
use rinja::Template;
use serde::Deserialize;
// use tracing::{event, Level};
use crate::{
data::{db, model},
html_templates::*,
translation, Result,
};
#[derive(Deserialize)]
pub struct CurrentRecipeId {
current_recipe_id: Option<i64>,
}
#[debug_handler]
pub async fn recipes_list_fragments(
State(connection): State<db::Connection>,
current_recipe: Query<CurrentRecipeId>,
Extension(user): Extension<Option<model::User>>,
Extension(tr): Extension<translation::Tr>,
) -> Result<impl IntoResponse> {
let recipes = Recipes {
published: connection
.get_all_published_recipe_titles(tr.current_lang_code(), user.as_ref().map(|u| u.id))
.await?,
unpublished: if let Some(user) = user.as_ref() {
connection
.get_all_unpublished_recipe_titles(user.id)
.await?
} else {
vec![]
},
current_id: current_recipe.current_recipe_id,
};
Ok(Html(RecipesListFragmentTemplate { tr, recipes }.render()?))
}