Add default servings to profile + choose servings when scheduling
This commit is contained in:
parent
d8641d4db6
commit
ae6da1a5ae
13 changed files with 70 additions and 34 deletions
|
|
@ -77,7 +77,7 @@ FROM [UserLoginToken] WHERE [token] = $1
|
|||
|
||||
pub async fn load_user(&self, user_id: i64) -> Result<Option<model::User>> {
|
||||
sqlx::query_as(
|
||||
"SELECT [id], [email], [name], [lang], [is_admin] FROM [User] WHERE [id] = $1",
|
||||
"SELECT [id], [email], [name], [default_servings], [lang], [is_admin] FROM [User] WHERE [id] = $1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_optional(&self.pool)
|
||||
|
|
@ -92,13 +92,17 @@ FROM [UserLoginToken] WHERE [token] = $1
|
|||
user_id: i64,
|
||||
new_email: Option<&str>,
|
||||
new_name: Option<&str>,
|
||||
new_default_servings: Option<u32>,
|
||||
new_password: Option<&str>,
|
||||
) -> Result<UpdateUserResult> {
|
||||
let mut tx = self.tx().await?;
|
||||
let hashed_new_password = new_password.map(|p| hash(p).unwrap());
|
||||
|
||||
let (email, name, hashed_password) = sqlx::query_as::<_, (String, String, String)>(
|
||||
"SELECT [email], [name], [password] FROM [User] WHERE [id] = $1",
|
||||
let (email, name, default_servings, hashed_password) = sqlx::query_as::<
|
||||
_,
|
||||
(String, String, u32, String),
|
||||
>(
|
||||
"SELECT [email], [name], [default_servings], [password] FROM [User] WHERE [id] = $1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(&mut *tx)
|
||||
|
|
@ -144,13 +148,14 @@ WHERE [id] = $1
|
|||
sqlx::query(
|
||||
r#"
|
||||
UPDATE [User]
|
||||
SET [email] = $2, [name] = $3, [password] = $4
|
||||
SET [email] = $2, [name] = $3, [default_servings] = $4, [password] = $5
|
||||
WHERE [id] = $1
|
||||
"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(new_email.unwrap_or(&email))
|
||||
.bind(new_name.map(str::trim).unwrap_or(&name))
|
||||
.bind(new_default_servings.unwrap_or(default_servings))
|
||||
.bind(hashed_new_password.unwrap_or(hashed_password))
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ pub struct User {
|
|||
pub id: i64,
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
pub default_servings: u32,
|
||||
pub lang: String,
|
||||
pub is_admin: bool,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,6 +112,7 @@ pub struct ProfileTemplate<'a> {
|
|||
|
||||
pub username: &'a str,
|
||||
pub email: &'a str,
|
||||
pub default_servings: u32,
|
||||
pub message: &'a str,
|
||||
pub message_email: &'a str,
|
||||
pub message_password: &'a str,
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ async fn main() {
|
|||
)
|
||||
// Recipes.
|
||||
.route("/recipe/new", get(services::recipe::create))
|
||||
.route("/recipe/edit/{id}", get(services::recipe::edit_recipe))
|
||||
.route("/recipe/edit/{id}", get(services::recipe::edit))
|
||||
.route("/recipe/view/{id}", get(services::recipe::view))
|
||||
// User.
|
||||
.route(
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ pub async fn create(
|
|||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn edit_recipe(
|
||||
pub async fn edit(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
|
|
|
|||
|
|
@ -655,6 +655,7 @@ pub async fn edit_user_get(
|
|||
ProfileTemplate {
|
||||
username: &user.name,
|
||||
email: &user.email,
|
||||
default_servings: user.default_servings,
|
||||
message: "",
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
|
|
@ -673,6 +674,7 @@ pub async fn edit_user_get(
|
|||
pub struct EditUserForm {
|
||||
name: String,
|
||||
email: String,
|
||||
default_servings: u32,
|
||||
password_1: String,
|
||||
password_2: String,
|
||||
}
|
||||
|
|
@ -712,6 +714,7 @@ pub async fn edit_user_post(
|
|||
user: Some(user),
|
||||
username: &form_data.name,
|
||||
email: &form_data.email,
|
||||
default_servings: form_data.default_servings,
|
||||
message_email: match error {
|
||||
ProfileUpdateError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
ProfileUpdateError::EmailAlreadyTaken => tr.t(Sentence::EmailAlreadyTaken),
|
||||
|
|
@ -760,6 +763,7 @@ pub async fn edit_user_post(
|
|||
user.id,
|
||||
Some(email_trimmed),
|
||||
Some(&form_data.name),
|
||||
Some(form_data.default_servings),
|
||||
new_password,
|
||||
)
|
||||
.await
|
||||
|
|
@ -815,6 +819,7 @@ pub async fn edit_user_post(
|
|||
user,
|
||||
username: &form_data.name,
|
||||
email: &form_data.email,
|
||||
default_servings: form_data.default_servings,
|
||||
message,
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ pub enum Sentence {
|
|||
// Profile
|
||||
ProfileTitle,
|
||||
ProfileEmail,
|
||||
ProfileDefaultServings,
|
||||
ProfileNewPassword,
|
||||
ProfileFollowEmailLink,
|
||||
ProfileEmailSent,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue