diff --git a/src/model/user.rs b/src/model/user.rs index fb3b323..4340ab9 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -245,6 +245,18 @@ impl User { false } + pub async fn has_membership_pdf(&self, db: &SqlitePool) -> bool { + match sqlx::query_scalar!("SELECT membership_pdf FROM user WHERE id = ?", self.id) + .fetch_one(db) + .await + .unwrap() + { + Some(a) if a.is_empty() => false, + None => false, + _ => true, + } + } + pub async fn roles(&self, db: &SqlitePool) -> Vec { sqlx::query!( "SELECT r.name FROM role r JOIN user_role ur ON r.id = ur.role_id JOIN user u ON u.id = ur.user_id WHERE ur.user_id = ? AND u.deleted = 0;", @@ -434,6 +446,7 @@ ORDER BY last_access DESC } pub async fn create(db: &SqlitePool, name: &str) -> bool { + let name = name.trim(); sqlx::query!("INSERT INTO USER(name) VALUES (?)", name) .execute(db) .await @@ -447,9 +460,7 @@ ORDER BY last_access DESC family_id = Some(Family::insert(db).await) } - let user_with_membershippdf = UserWithMembershipPdf::from(db, self.clone()).await; - - if user_with_membershippdf.membership_pdf.is_none() { + if !self.has_membership_pdf(db).await { if let Some(membership_pdf) = data.membership_pdf { let mut stream = membership_pdf.open().await.unwrap(); let mut buffer = Vec::new(); @@ -967,16 +978,7 @@ pub struct UserWithRolesAndMembershipPdf { impl UserWithRolesAndMembershipPdf { pub(crate) async fn from_user(db: &SqlitePool, user: User) -> Self { - let membership_pdf = - match sqlx::query_scalar!("SELECT membership_pdf FROM user WHERE id = ?", user.id) - .fetch_one(db) - .await - .unwrap() - { - Some(a) if a.is_empty() => false, - None => false, - _ => true, - }; + let membership_pdf = user.has_membership_pdf(db).await; Self { roles: user.roles(db).await,