[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),
|
||||
)
|
||||
}
|
||||
|
|
@ -79,7 +79,7 @@ pub async fn view(
|
|||
&& (user.is_none() || recipe.user_id != user.as_ref().unwrap().id)
|
||||
{
|
||||
return Ok(MessageTemplate::new_with_user(
|
||||
tr.tp(Sentence::RecipeNotAllowedToView, &[Box::new(recipe_id)]),
|
||||
&tr.tp(Sentence::RecipeNotAllowedToView, &[Box::new(recipe_id)]),
|
||||
tr,
|
||||
user,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ pub async fn sign_up_get(
|
|||
user,
|
||||
tr,
|
||||
email: String::new(),
|
||||
message: String::new(),
|
||||
message_email: String::new(),
|
||||
message_password: String::new(),
|
||||
message: "",
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -71,26 +71,27 @@ pub async fn sign_up_post(
|
|||
user: Option<model::User>,
|
||||
tr: translation::Tr,
|
||||
) -> Result<Response> {
|
||||
let invalid_password_mess = &tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
);
|
||||
Ok(SignUpFormTemplate {
|
||||
user,
|
||||
email: form_data.email.clone(),
|
||||
message_email: match error {
|
||||
SignUpError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
message_password: match error {
|
||||
SignUpError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
SignUpError::InvalidPassword => tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
),
|
||||
_ => String::new(),
|
||||
SignUpError::InvalidPassword => invalid_password_mess,
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
SignUpError::UserAlreadyExists => tr.t(Sentence::EmailAlreadyTaken),
|
||||
SignUpError::DatabaseError => "Database error".to_string(),
|
||||
SignUpError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
SignUpError::UnableSendEmail => tr.t(Sentence::UnableToSendEmail),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
}
|
||||
|
|
@ -235,8 +236,8 @@ pub async fn sign_in_get(
|
|||
Ok(SignInFormTemplate {
|
||||
user,
|
||||
tr,
|
||||
email: String::new(),
|
||||
message: String::new(),
|
||||
email: "",
|
||||
message: "",
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -271,7 +272,7 @@ pub async fn sign_in_post(
|
|||
jar,
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
email: form_data.email,
|
||||
email: &form_data.email,
|
||||
message: tr.t(Sentence::AccountMustBeValidatedFirst),
|
||||
tr,
|
||||
}
|
||||
|
|
@ -281,7 +282,7 @@ pub async fn sign_in_post(
|
|||
jar,
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
email: form_data.email,
|
||||
email: &form_data.email,
|
||||
message: tr.t(Sentence::WrongEmailOrPassword),
|
||||
tr,
|
||||
}
|
||||
|
|
@ -326,9 +327,9 @@ pub async fn ask_reset_password_get(
|
|||
Ok(AskResetPasswordTemplate {
|
||||
user,
|
||||
tr,
|
||||
email: String::new(),
|
||||
message: String::new(),
|
||||
message_email: String::new(),
|
||||
email: "",
|
||||
message: "",
|
||||
message_email: "",
|
||||
}
|
||||
.into_response())
|
||||
}
|
||||
|
|
@ -364,10 +365,10 @@ pub async fn ask_reset_password_post(
|
|||
) -> Result<Response> {
|
||||
Ok(AskResetPasswordTemplate {
|
||||
user,
|
||||
email: email.to_string(),
|
||||
email,
|
||||
message_email: match error {
|
||||
AskResetPasswordError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
AskResetPasswordError::EmailAlreadyReset => {
|
||||
|
|
@ -376,7 +377,7 @@ pub async fn ask_reset_password_post(
|
|||
AskResetPasswordError::EmailUnknown => tr.t(Sentence::EmailUnknown),
|
||||
AskResetPasswordError::UnableSendEmail => tr.t(Sentence::UnableToSendResetEmail),
|
||||
AskResetPasswordError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
}
|
||||
|
|
@ -470,9 +471,9 @@ pub async fn reset_password_get(
|
|||
Ok(ResetPasswordTemplate {
|
||||
user,
|
||||
tr,
|
||||
reset_token: reset_token.to_string(),
|
||||
message: String::new(),
|
||||
message_password: String::new(),
|
||||
reset_token,
|
||||
message: "",
|
||||
message_password: "",
|
||||
}
|
||||
.into_response())
|
||||
} else {
|
||||
|
|
@ -510,21 +511,22 @@ pub async fn reset_password_post(
|
|||
user: Option<model::User>,
|
||||
tr: translation::Tr,
|
||||
) -> Result<Response> {
|
||||
let reset_password_mess = &tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
);
|
||||
Ok(ResetPasswordTemplate {
|
||||
user,
|
||||
reset_token: form_data.reset_token.clone(),
|
||||
reset_token: &form_data.reset_token,
|
||||
message_password: match error {
|
||||
ResetPasswordError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
ResetPasswordError::InvalidPassword => tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
),
|
||||
_ => String::new(),
|
||||
ResetPasswordError::InvalidPassword => reset_password_mess,
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
ResetPasswordError::TokenExpired => tr.t(Sentence::AskResetTokenExpired),
|
||||
ResetPasswordError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
}
|
||||
|
|
@ -571,12 +573,12 @@ pub async fn edit_user_get(
|
|||
) -> Response {
|
||||
if let Some(user) = user {
|
||||
ProfileTemplate {
|
||||
username: user.name.clone(),
|
||||
email: user.email.clone(),
|
||||
message: String::new(),
|
||||
message_email: String::new(),
|
||||
message_password: String::new(),
|
||||
user: Some(user),
|
||||
username: &user.name,
|
||||
email: &user.email,
|
||||
message: "",
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
user: Some(user.clone()),
|
||||
tr,
|
||||
}
|
||||
.into_response()
|
||||
|
|
@ -592,6 +594,7 @@ pub struct EditUserForm {
|
|||
password_1: String,
|
||||
password_2: String,
|
||||
}
|
||||
|
||||
enum ProfileUpdateError {
|
||||
InvalidEmail,
|
||||
EmailAlreadyTaken,
|
||||
|
|
@ -618,27 +621,28 @@ pub async fn edit_user_post(
|
|||
user: model::User,
|
||||
tr: translation::Tr,
|
||||
) -> Result<Response> {
|
||||
let invalid_password_mess = &tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
);
|
||||
Ok(ProfileTemplate {
|
||||
user: Some(user),
|
||||
username: form_data.name.clone(),
|
||||
email: form_data.email.clone(),
|
||||
username: &form_data.name,
|
||||
email: &form_data.email,
|
||||
message_email: match error {
|
||||
ProfileUpdateError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
ProfileUpdateError::EmailAlreadyTaken => tr.t(Sentence::EmailAlreadyTaken),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
message_password: match error {
|
||||
ProfileUpdateError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
ProfileUpdateError::InvalidPassword => tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
),
|
||||
_ => String::new(),
|
||||
ProfileUpdateError::InvalidPassword => invalid_password_mess,
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
ProfileUpdateError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
ProfileUpdateError::UnableSendEmail => tr.t(Sentence::UnableToSendEmail),
|
||||
_ => String::new(),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
}
|
||||
|
|
@ -666,7 +670,7 @@ pub async fn edit_user_post(
|
|||
};
|
||||
|
||||
let email_trimmed = form_data.email.trim();
|
||||
let message: String;
|
||||
let message: &str;
|
||||
|
||||
match connection
|
||||
.update_user(
|
||||
|
|
@ -725,11 +729,11 @@ pub async fn edit_user_post(
|
|||
|
||||
Ok(ProfileTemplate {
|
||||
user,
|
||||
username: form_data.name,
|
||||
email: form_data.email,
|
||||
username: &form_data.name,
|
||||
email: &form_data.email,
|
||||
message,
|
||||
message_email: String::new(),
|
||||
message_password: String::new(),
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
tr,
|
||||
}
|
||||
.into_response())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue