From 2a69c89065fbb1615c3927ce9fe1f7044259b2a2 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 31 Aug 2023 19:23:21 +0200 Subject: [PATCH] Automatically trim values before saving --- .../src/controllers/members_controller.rs | 23 ++++++++++++++++++- geneit_backend/src/schema.rs | 21 +++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/geneit_backend/src/controllers/members_controller.rs b/geneit_backend/src/controllers/members_controller.rs index 3749706..61f5b6e 100644 --- a/geneit_backend/src/controllers/members_controller.rs +++ b/geneit_backend/src/controllers/members_controller.rs @@ -103,8 +103,29 @@ fn check_opt_str_val( Ok(()) } +fn trim_opt_val(val: &mut Option) { + if let Some(s) = val { + *val = Some(s.trim().to_string()); + } + + if val.as_deref() == Some("") { + *val = None; + } +} + impl MemberRequest { - pub async fn to_member(self, member: &mut Member) -> anyhow::Result<()> { + pub async fn to_member(mut self, member: &mut Member) -> anyhow::Result<()> { + // Trim values before processing + trim_opt_val(&mut self.first_name); + trim_opt_val(&mut self.last_name); + trim_opt_val(&mut self.birth_last_name); + trim_opt_val(&mut self.email); + trim_opt_val(&mut self.country); + trim_opt_val(&mut self.address); + trim_opt_val(&mut self.city); + trim_opt_val(&mut self.note); + trim_opt_val(&mut self.phone); + let c = StaticConstraints::default(); check_opt_str_val( &self.first_name, diff --git a/geneit_backend/src/schema.rs b/geneit_backend/src/schema.rs index 265bbfd..708d30a 100644 --- a/geneit_backend/src/schema.rs +++ b/geneit_backend/src/schema.rs @@ -6,6 +6,7 @@ diesel::table! { family_id -> Int4, wife -> Nullable, husband -> Nullable, + #[max_length = 1] state -> Nullable, photo_id -> Nullable, time_create -> Int8, @@ -23,7 +24,9 @@ diesel::table! { families (id) { id -> Int4, time_create -> Int8, + #[max_length = 30] name -> Varchar, + #[max_length = 7] invitation_code -> Varchar, disable_couple_photos -> Bool, } @@ -33,16 +36,26 @@ diesel::table! { members (id) { id -> Int4, family_id -> Int4, + #[max_length = 30] first_name -> Nullable, + #[max_length = 30] last_name -> Nullable, + #[max_length = 30] birth_last_name -> Nullable, photo_id -> Nullable, + #[max_length = 255] email -> Nullable, + #[max_length = 30] phone -> Nullable, + #[max_length = 155] address -> Nullable, + #[max_length = 150] city -> Nullable, + #[max_length = 12] postal_code -> Nullable, + #[max_length = 2] country -> Nullable, + #[max_length = 1] sex -> Nullable, time_create -> Int8, time_update -> Int8, @@ -71,11 +84,15 @@ diesel::table! { diesel::table! { photos (id) { id -> Int4, + #[max_length = 36] file_id -> Varchar, time_create -> Int8, + #[max_length = 150] mime_type -> Varchar, + #[max_length = 130] sha512 -> Varchar, file_size -> Int4, + #[max_length = 130] thumb_sha512 -> Varchar, } } @@ -83,12 +100,16 @@ diesel::table! { diesel::table! { users (id) { id -> Int4, + #[max_length = 30] name -> Varchar, + #[max_length = 255] email -> Varchar, password -> Nullable, time_create -> Int8, + #[max_length = 150] reset_password_token -> Nullable, time_gen_reset_token -> Int8, + #[max_length = 150] delete_account_token -> Nullable, time_gen_delete_account_token -> Int8, time_activate -> Int8,