Check for duplicate email
This commit is contained in:
		@@ -29,7 +29,18 @@ pub async fn create_account(
 | 
				
			|||||||
        return Ok(HttpResponse::BadRequest().json("Size constraints were not respected!"));
 | 
					        return Ok(HttpResponse::BadRequest().json("Size constraints were not respected!"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO : check the mail address
 | 
					    // Check if email is already attached to an account
 | 
				
			||||||
 | 
					    match users_service::exists_email(&req.email).await {
 | 
				
			||||||
 | 
					        Ok(false) => {}
 | 
				
			||||||
 | 
					        Ok(true) => {
 | 
				
			||||||
 | 
					            return Ok(HttpResponse::Conflict()
 | 
				
			||||||
 | 
					                .json("An account with the same email address already exists!"));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Err(e) => {
 | 
				
			||||||
 | 
					            log::error!("Failed to check email existence! {}", e);
 | 
				
			||||||
 | 
					            return Err(ErrorInternalServerError(e));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Create the account
 | 
					    // Create the account
 | 
				
			||||||
    let user_id = users_service::create_account(&req.name, &req.email)
 | 
					    let user_id = users_service::create_account(&req.name, &req.email)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,3 +20,15 @@ pub async fn create_account(name: &str, email: &str) -> anyhow::Result<User> {
 | 
				
			|||||||
        Ok(res)
 | 
					        Ok(res)
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Check if an email address is already associated with an account
 | 
				
			||||||
 | 
					pub async fn exists_email(email: &str) -> anyhow::Result<bool> {
 | 
				
			||||||
 | 
					    db_connection::execute(|conn| {
 | 
				
			||||||
 | 
					        let count: i64 = users::table
 | 
				
			||||||
 | 
					            .filter(users::email.eq(email))
 | 
				
			||||||
 | 
					            .count()
 | 
				
			||||||
 | 
					            .get_result(conn)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Ok(count != 0)
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user