55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use axum::{
|
|
debug_handler,
|
|
extract::{Extension, Path, State},
|
|
http::{HeaderMap, StatusCode},
|
|
response::{IntoResponse, Result},
|
|
};
|
|
use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite};
|
|
|
|
use crate::{
|
|
app::Context,
|
|
consts,
|
|
data::{db, model},
|
|
ron_extractor::ExtractRon,
|
|
ron_utils::{ron_error, ron_response_ok},
|
|
};
|
|
|
|
pub mod calendar;
|
|
pub mod recipe;
|
|
mod rights;
|
|
pub mod shopping_list;
|
|
|
|
#[debug_handler]
|
|
pub async fn set_lang(
|
|
State(connection): State<db::Connection>,
|
|
Extension(context): Extension<Context>,
|
|
headers: HeaderMap,
|
|
ExtractRon(lang): ExtractRon<String>,
|
|
) -> Result<(CookieJar, StatusCode)> {
|
|
let mut jar = CookieJar::from_headers(&headers);
|
|
if let Some(user) = context.user {
|
|
connection.set_user_lang(user.id, &lang).await?;
|
|
} else {
|
|
// Only set the cookie if the user is not connected.
|
|
let cookie = Cookie::build((consts::COOKIE_LANG_NAME, lang))
|
|
.same_site(SameSite::Lax)
|
|
.path("/");
|
|
jar = jar.add(cookie);
|
|
}
|
|
Ok((jar, StatusCode::OK))
|
|
}
|
|
|
|
#[debug_handler]
|
|
pub async fn get_translation(
|
|
Extension(context): Extension<Context>,
|
|
Path(id): Path<i64>,
|
|
) -> Result<impl IntoResponse> {
|
|
Ok(ron_response_ok(context.tr.t_from_id(id)))
|
|
}
|
|
|
|
/*** 404 ***/
|
|
|
|
#[debug_handler]
|
|
pub async fn not_found(Extension(_user): Extension<Option<model::User>>) -> impl IntoResponse {
|
|
ron_error(StatusCode::NOT_FOUND, "Not found")
|
|
}
|