Split db::Connection implementation in submodules (db::user and db::recipe).
This commit is contained in:
parent
4248d11aa9
commit
fce4eade73
17 changed files with 1307 additions and 1234 deletions
|
|
@ -15,7 +15,12 @@ use serde::Deserialize;
|
|||
use tracing::{event, Level};
|
||||
|
||||
use crate::{
|
||||
config::Config, consts, data::db, email, html_templates::*, model, ron_utils, utils, AppState,
|
||||
config::Config,
|
||||
consts,
|
||||
data::{db, model},
|
||||
email,
|
||||
html_templates::*,
|
||||
ron_utils, utils, AppState,
|
||||
};
|
||||
|
||||
pub mod ron;
|
||||
|
|
@ -53,12 +58,41 @@ pub async fn home_page(
|
|||
|
||||
Ok(HomeTemplate {
|
||||
user,
|
||||
current_recipe_id: None,
|
||||
recipes,
|
||||
recipes: Recipes {
|
||||
list: recipes,
|
||||
current_id: None,
|
||||
}, // current_recipe_id: None,
|
||||
// recipes,
|
||||
})
|
||||
}
|
||||
|
||||
///// VIEW RECIPE /////
|
||||
///// RECIPE /////
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn create_recipe(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
) -> Result<Response> {
|
||||
if let Some(user) = user {
|
||||
let recipe_id = connection.create_recipe(user.id).await?;
|
||||
Ok(Redirect::to(&format!("/recipe/edit/{}", recipe_id)).into_response())
|
||||
} else {
|
||||
Ok(MessageTemplate::new("Not logged in").into_response())
|
||||
}
|
||||
}
|
||||
|
||||
// #[debug_handler]
|
||||
// pub async fn edit_recipe(
|
||||
// State(connection): State<db::Connection>,
|
||||
// Extension(user): Extension<Option<model::User>>,
|
||||
// Path(recipe_id): Path<i64>,
|
||||
// ) -> Result<Response> {
|
||||
// if let Some(user) = user {
|
||||
// Ok(RecipeEditTemplate { user }.into_response())
|
||||
// } else {
|
||||
// Ok(MessageTemplate::new("Not logged in").into_response())
|
||||
// }
|
||||
// }
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn view_recipe(
|
||||
|
|
@ -68,11 +102,13 @@ pub async fn view_recipe(
|
|||
) -> Result<Response> {
|
||||
let recipes = connection.get_all_recipe_titles().await?;
|
||||
match connection.get_recipe(recipe_id).await? {
|
||||
Some(recipe) => Ok(ViewRecipeTemplate {
|
||||
Some(recipe) => Ok(RecipeViewTemplate {
|
||||
user,
|
||||
current_recipe_id: Some(recipe.id),
|
||||
recipes,
|
||||
current_recipe: recipe,
|
||||
recipes: Recipes {
|
||||
list: recipes,
|
||||
current_id: Some(recipe.id),
|
||||
},
|
||||
recipe,
|
||||
}
|
||||
.into_response()),
|
||||
None => Ok(MessageTemplate::new_with_user(
|
||||
|
|
@ -173,10 +209,10 @@ pub async fn sign_up_post(
|
|||
.sign_up(&form_data.email, &form_data.password_1)
|
||||
.await
|
||||
{
|
||||
Ok(db::SignUpResult::UserAlreadyExists) => {
|
||||
Ok(db::user::SignUpResult::UserAlreadyExists) => {
|
||||
error_response(SignUpError::UserAlreadyExists, &form_data, user)
|
||||
}
|
||||
Ok(db::SignUpResult::UserCreatedWaitingForValidation(token)) => {
|
||||
Ok(db::user::SignUpResult::UserCreatedWaitingForValidation(token)) => {
|
||||
let url = utils::get_url_from_host(&host);
|
||||
let email = form_data.email.clone();
|
||||
match email::send_email(
|
||||
|
|
@ -236,7 +272,7 @@ pub async fn sign_up_validation(
|
|||
)
|
||||
.await?
|
||||
{
|
||||
db::ValidationResult::Ok(token, user_id) => {
|
||||
db::user::ValidationResult::Ok(token, user_id) => {
|
||||
let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
|
||||
jar = jar.add(cookie);
|
||||
let user = connection.load_user(user_id).await?;
|
||||
|
|
@ -248,14 +284,14 @@ pub async fn sign_up_validation(
|
|||
),
|
||||
))
|
||||
}
|
||||
db::ValidationResult::ValidationExpired => Ok((
|
||||
db::user::ValidationResult::ValidationExpired => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"The validation has expired. Try to sign up again",
|
||||
user,
|
||||
),
|
||||
)),
|
||||
db::ValidationResult::UnknownUser => Ok((
|
||||
db::user::ValidationResult::UnknownUser => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user("Validation error. Try to sign up again", user),
|
||||
)),
|
||||
|
|
@ -307,7 +343,7 @@ pub async fn sign_in_post(
|
|||
)
|
||||
.await?
|
||||
{
|
||||
db::SignInResult::AccountNotValidated => Ok((
|
||||
db::user::SignInResult::AccountNotValidated => Ok((
|
||||
jar,
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
|
|
@ -316,7 +352,7 @@ pub async fn sign_in_post(
|
|||
}
|
||||
.into_response(),
|
||||
)),
|
||||
db::SignInResult::UserNotFound | db::SignInResult::WrongPassword => Ok((
|
||||
db::user::SignInResult::UserNotFound | db::user::SignInResult::WrongPassword => Ok((
|
||||
jar,
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
|
|
@ -325,7 +361,7 @@ pub async fn sign_in_post(
|
|||
}
|
||||
.into_response(),
|
||||
)),
|
||||
db::SignInResult::Ok(token, _user_id) => {
|
||||
db::user::SignInResult::Ok(token, _user_id) => {
|
||||
let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
|
||||
Ok((jar.add(cookie), Redirect::to("/").into_response()))
|
||||
}
|
||||
|
|
@ -433,15 +469,15 @@ pub async fn ask_reset_password_post(
|
|||
)
|
||||
.await
|
||||
{
|
||||
Ok(db::GetTokenResetPasswordResult::PasswordAlreadyReset) => error_response(
|
||||
Ok(db::user::GetTokenResetPasswordResult::PasswordAlreadyReset) => error_response(
|
||||
AskResetPasswordError::EmailAlreadyReset,
|
||||
&form_data.email,
|
||||
user,
|
||||
),
|
||||
Ok(db::GetTokenResetPasswordResult::EmailUnknown) => {
|
||||
Ok(db::user::GetTokenResetPasswordResult::EmailUnknown) => {
|
||||
error_response(AskResetPasswordError::EmailUnknown, &form_data.email, user)
|
||||
}
|
||||
Ok(db::GetTokenResetPasswordResult::Ok(token)) => {
|
||||
Ok(db::user::GetTokenResetPasswordResult::Ok(token)) => {
|
||||
let url = utils::get_url_from_host(&host);
|
||||
match email::send_email(
|
||||
&form_data.email,
|
||||
|
|
@ -559,12 +595,12 @@ pub async fn reset_password_post(
|
|||
)
|
||||
.await
|
||||
{
|
||||
Ok(db::ResetPasswordResult::Ok) => Ok(MessageTemplate::new_with_user(
|
||||
Ok(db::user::ResetPasswordResult::Ok) => Ok(MessageTemplate::new_with_user(
|
||||
"Your password has been reset",
|
||||
user,
|
||||
)
|
||||
.into_response()),
|
||||
Ok(db::ResetPasswordResult::ResetTokenExpired) => {
|
||||
Ok(db::user::ResetPasswordResult::ResetTokenExpired) => {
|
||||
error_response(ResetPasswordError::TokenExpired, &form_data, user)
|
||||
}
|
||||
Err(_) => error_response(ResetPasswordError::DatabaseError, &form_data, user),
|
||||
|
|
@ -681,10 +717,10 @@ pub async fn edit_user_post(
|
|||
)
|
||||
.await
|
||||
{
|
||||
Ok(db::UpdateUserResult::EmailAlreadyTaken) => {
|
||||
Ok(db::user::UpdateUserResult::EmailAlreadyTaken) => {
|
||||
return error_response(ProfileUpdateError::EmailAlreadyTaken, &form_data, user);
|
||||
}
|
||||
Ok(db::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => {
|
||||
Ok(db::user::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => {
|
||||
let url = utils::get_url_from_host(&host);
|
||||
let email = form_data.email.clone();
|
||||
match email::send_email(
|
||||
|
|
@ -709,7 +745,7 @@ pub async fn edit_user_post(
|
|||
}
|
||||
}
|
||||
}
|
||||
Ok(db::UpdateUserResult::Ok) => {
|
||||
Ok(db::user::UpdateUserResult::Ok) => {
|
||||
message = "Profile saved";
|
||||
}
|
||||
Err(_) => return error_response(ProfileUpdateError::DatabaseError, &form_data, user),
|
||||
|
|
@ -760,7 +796,7 @@ pub async fn email_revalidation(
|
|||
)
|
||||
.await?
|
||||
{
|
||||
db::ValidationResult::Ok(token, user_id) => {
|
||||
db::user::ValidationResult::Ok(token, user_id) => {
|
||||
let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
|
||||
jar = jar.add(cookie);
|
||||
let user = connection.load_user(user_id).await?;
|
||||
|
|
@ -769,14 +805,14 @@ pub async fn email_revalidation(
|
|||
MessageTemplate::new_with_user("Email validation successful", user),
|
||||
))
|
||||
}
|
||||
db::ValidationResult::ValidationExpired => Ok((
|
||||
db::user::ValidationResult::ValidationExpired => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"The validation has expired. Try to sign up again with the same email",
|
||||
user,
|
||||
),
|
||||
)),
|
||||
db::ValidationResult::UnknownUser => Ok((
|
||||
db::user::ValidationResult::UnknownUser => Ok((
|
||||
jar,
|
||||
MessageTemplate::new_with_user(
|
||||
"Validation error. Try to sign up again with the same email",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue