[Database] Add 'creation_datetime' to User + some little things
This commit is contained in:
parent
91ab379718
commit
7a09e2360e
14 changed files with 179 additions and 131 deletions
83
backend/src/services/mod.rs
Normal file
83
backend/src/services/mod.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
use axum::{
|
||||
body, debug_handler,
|
||||
extract::{Extension, Request, State},
|
||||
http::{header, StatusCode},
|
||||
middleware::Next,
|
||||
response::{IntoResponse, Response, Result},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
data::{db, model},
|
||||
html_templates::*,
|
||||
ron_utils, translation,
|
||||
};
|
||||
|
||||
pub mod fragments;
|
||||
pub mod recipe;
|
||||
pub mod ron;
|
||||
pub mod user;
|
||||
|
||||
// Will embed RON error in HTML page.
|
||||
pub async fn ron_error_to_html(
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
req: Request,
|
||||
next: Next,
|
||||
) -> Result<Response> {
|
||||
let response = next.run(req).await;
|
||||
|
||||
if let Some(content_type) = response.headers().get(header::CONTENT_TYPE) {
|
||||
if content_type == ron_utils::RON_CONTENT_TYPE {
|
||||
let message = match body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(bytes) => String::from_utf8(bytes.to_vec()).unwrap_or_default(),
|
||||
Err(error) => error.to_string(),
|
||||
};
|
||||
return Ok(MessageTemplate {
|
||||
user: None,
|
||||
message: &message,
|
||||
as_code: true,
|
||||
tr,
|
||||
}
|
||||
.into_response());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
///// HOME /////
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn home_page(
|
||||
State(connection): State<db::Connection>,
|
||||
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: None,
|
||||
};
|
||||
|
||||
Ok(HomeTemplate { user, recipes, tr })
|
||||
}
|
||||
|
||||
///// 404 /////
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn not_found(
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
) -> impl IntoResponse {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
MessageTemplate::new_with_user("404: Not found", tr, user),
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue