Execute SQLs for email_signups in transaction

This commit is contained in:
Kitaiti Makoto 2022-01-06 20:27:55 +09:00
parent b6d38536e3
commit 1e3851ea69

View File

@ -90,27 +90,31 @@ impl EmailSignup {
} }
pub fn confirm(&self, conn: &DbConn) -> Result<()> { pub fn confirm(&self, conn: &DbConn) -> Result<()> {
Self::ensure_user_not_exist_by_email(conn, &self.email)?; conn.transaction(|| {
if self.expired() { Self::ensure_user_not_exist_by_email(conn, &self.email)?;
Self::delete_existings_by_email(conn, &self.email)?; if self.expired() {
return Err(Error::Expired); Self::delete_existings_by_email(conn, &self.email)?;
} return Err(Error::Expired);
Ok(()) }
Ok(())
})
} }
pub fn complete(&self, conn: &DbConn, username: String, password: String) -> Result<User> { pub fn complete(&self, conn: &DbConn, username: String, password: String) -> Result<User> {
Self::ensure_user_not_exist_by_email(conn, &self.email)?; conn.transaction(|| {
let user = NewUser::new_local( Self::ensure_user_not_exist_by_email(conn, &self.email)?;
conn, let user = NewUser::new_local(
username, conn,
"".to_string(), username,
Role::Normal, "".to_string(),
"", Role::Normal,
self.email.clone(), "",
Some(User::hash_pass(&password)?), self.email.clone(),
)?; Some(User::hash_pass(&password)?),
self.delete(conn)?; )?;
Ok(user) self.delete(conn)?;
Ok(user)
})
} }
fn delete(&self, conn: &DbConn) -> Result<()> { fn delete(&self, conn: &DbConn) -> Result<()> {