Set user language to new recipe + cleaning

This commit is contained in:
Greg Burri 2025-01-06 22:52:22 +01:00
parent 8b4b788562
commit 03ebbb74fa
12 changed files with 73 additions and 86 deletions

View file

@ -25,7 +25,9 @@ pub async fn recipes_list_fragments(
Extension(tr): Extension<translation::Tr>,
) -> Result<impl IntoResponse> {
let recipes = Recipes {
published: connection.get_all_published_recipe_titles().await?,
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)
@ -35,5 +37,5 @@ pub async fn recipes_list_fragments(
},
current_id: current_recipe.current_recipe_id,
};
Ok(RecipesListFragmentTemplate { user, tr, recipes })
Ok(RecipesListFragmentTemplate { tr, recipes })
}

View file

@ -6,7 +6,6 @@ use axum::{
// use tracing::{event, Level};
use crate::{
consts,
data::{db, model},
html_templates::*,
translation::{self, Sentence},
@ -37,21 +36,20 @@ pub async fn edit_recipe(
if let Some(recipe) = connection.get_recipe(recipe_id, false).await? {
if recipe.user_id == user.id {
let recipes = Recipes {
published: connection.get_all_published_recipe_titles().await?,
published: connection
.get_all_published_recipe_titles(tr.current_lang_code(), Some(user.id))
.await?,
unpublished: connection
.get_all_unpublished_recipe_titles(user.id)
.await?,
current_id: Some(recipe_id),
};
dbg!(&recipe);
Ok(RecipeEditTemplate {
user: Some(user),
tr,
recipes,
recipe,
languages: *consts::LANGUAGES,
}
.into_response())
} else {
@ -89,7 +87,12 @@ pub async fn view(
}
let recipes = Recipes {
published: connection.get_all_published_recipe_titles().await?,
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)

View file

@ -249,6 +249,14 @@ pub async fn set_language(
Extension(user): Extension<Option<model::User>>,
ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeLanguage>,
) -> Result<StatusCode> {
if !crate::translation::Tr::available_codes()
.iter()
.any(|&l| l == ron.lang)
{
// TODO: log?
return Ok(StatusCode::BAD_REQUEST);
}
check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
connection
.set_recipe_language(ron.recipe_id, &ron.lang)