Add default servings to profile + choose servings when scheduling

This commit is contained in:
Greg Burri 2025-02-03 23:42:07 +01:00
parent d8641d4db6
commit ae6da1a5ae
13 changed files with 70 additions and 34 deletions

View file

@ -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?;

View file

@ -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,
}

View file

@ -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,

View file

@ -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(

View file

@ -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>,

View file

@ -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: "",

View file

@ -76,6 +76,7 @@ pub enum Sentence {
// Profile
ProfileTitle,
ProfileEmail,
ProfileDefaultServings,
ProfileNewPassword,
ProfileFollowEmailLink,
ProfileEmailSent,

View file

@ -2,8 +2,7 @@
{% block main_container %}
{% match user %}
{% when Some with (user) %}
{% if let Some(user) = user %}
<div class="content" id="user-edit">
<h1>{{ tr.t(Sentence::ProfileTitle) }}</h1>
@ -27,6 +26,14 @@
{{ message_email }}
<label for="input-servings">{{ tr.t(Sentence::ProfileDefaultServings) }}</label>
<input
id="input-servings"
type="number"
step="1" min="1" max="100"
name="default_servings"
value="{{ default_servings }}"/>
<label for="input-password-1">{{ tr.tp(Sentence::ProfileNewPassword, [Box::new(common::consts::MIN_PASSWORD_SIZE)]) }}</label>
<input id="input-password-1" type="password" name="password_1" autocomplete="new-password" />
@ -40,7 +47,6 @@
{{ message }}
</div>
{% when None %}
{% endmatch %}
{% endif %}
{% endblock %}

View file

@ -80,7 +80,18 @@
{% endfor %}
<div id="hidden-templates">
{% include "calendar.html" %}
{# To create a modal dialog to choose a date and and servings #}
{% if let Some(user) = user %}
<div class="date-and-servings" >
{% include "calendar.html" %}
<label for="input-servings">{{ tr.t(Sentence::RecipeServings) }}</label>
<input
id="input-servings"
type="number"
step="1" min="1" max="100"
value="{{ user.default_servings }}"/>
</div>
{% endif %}
</div>
</div>

View file

@ -62,6 +62,7 @@
(ProfileTitle, "Profile"),
(ProfileEmail, "Email (need to be revalidated if changed)"),
(ProfileDefaultServings, "Default servings"),
(ProfileNewPassword, "New password (minimum {} characters)"),
(ProfileFollowEmailLink, "Follow this link to validate this email address, {}"),
(ProfileEmailSent, "An email has been sent, follow the link to validate your new email"),
@ -188,6 +189,7 @@
(ProfileTitle, "Profile"),
(ProfileEmail, "Email (doit être revalidé si changé)"),
(ProfileDefaultServings, "Nombre de portions par défaut"),
(ProfileNewPassword, "Nouveau mot de passe (minimum {} caractères)"),
(ProfileFollowEmailLink, "Suivez ce lien pour valider l'adresse email, {}"),
(ProfileEmailSent, "Un email a été envoyé, suivez le lien pour valider la nouvelle adresse email"),
@ -198,7 +200,7 @@
(RecipeNotFound, "Recette non-trouvée"),
(RecipeTitle, "Titre"),
(RecipeDescription, "Description"),
(RecipeServings, "Nombre de personnes"),
(RecipeServings, "Nombre de portions"),
(RecipeEstimatedTime, "Temps estimé"),
(RecipeDifficulty, "Difficulté"),
(RecipeDifficultyEasy, "Facile"),