Add isUsernameUsed to query methods

Fixes #13
Have fun @David!
This commit is contained in:
Elias Schriefer 2021-11-25 16:05:26 +01:00
parent 1e7b685f64
commit a669c9cfd9

View File

@ -395,6 +395,17 @@ impl Query {
.map_err(FieldError::from)
}
async fn isUsernameUsed(context: &Context, username: String) -> FieldResult<bool> {
if !is_valid_user_name(&username) {
return Err(format!("{:?} is not a valid username", username).into());
}
Ok(sqlx::query(format!(
r#"SELECT user_name FROM users WHERE user_name="{}""#,
username,
).as_str()).fetch_optional(&context.db).await?.is_some())
}
async fn users(context: &Context) -> FieldResult<Vec<User>> {
let rows = sqlx::query("SELECT * FROM users")
.fetch_all(&context.db).await?;