From e608c1663f78202e13a2f0bb694438bd0c908897 Mon Sep 17 00:00:00 2001 From: Clover Johnson <95094165+callmeclover@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:44:54 -0400 Subject: [PATCH] fix --- src/user/auth.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/user/auth.rs b/src/user/auth.rs index 079ded7a..f76412de 100644 --- a/src/user/auth.rs +++ b/src/user/auth.rs @@ -1,7 +1,8 @@ use anyhow::Error; use sqlx::{query_as, query, postgres::{PgPoolOptions, PgPool}, types::Json}; -use super::model::{Model, User, GlassModeration}; +use super::model::{Model, GlassModeration}; use uuid::Uuid; +use validator::Validate; pub struct DatabaseConnectix { connection: PgPool @@ -38,7 +39,7 @@ impl DatabaseConnectix { /// Gets a possible user id (if one exists) for a username. pub async fn get_user_id(&mut self, username: &str) -> Result { - let user: Option = query_as( + let user: Option = query_as( "select max(id) from users where username=$1 limit 1;" ) .bind(username) @@ -48,14 +49,14 @@ impl DatabaseConnectix { if user.is_none() { Ok(1) } else { - if user.id == 9999 { return Err("username is taken"); } + if user.id == 9999 { return Err(anyhow!("username is taken")); } Ok(user.id+1) } } pub async fn post_user(&mut self, username: String, password: String) -> Result<(), Error> { let data: Model = Model { - id: self.get_user_id(username).await?, + id: self.get_user_id(&username).await?, uuid: Uuid::new_v4(), username, password, @@ -65,7 +66,7 @@ impl DatabaseConnectix { let _ = sqlx::query("insert into users (id, uuid, username, password, mod) values ($1, $2, $3, $4, $5)") .bind(data.id).bind(data.uuid).bind(data.username).bind(data.password).bind(data.moderation_stats) - .exectute(&mut self.connection) + .execute(&mut self.connection) .await?; Ok(()) } @@ -75,7 +76,7 @@ impl DatabaseConnectix { let _ = query("update users set username=$1, id=$2 where username=$3 and id=$4") .bind(username).bind(id).bind(prev_username).bind(prev_id) - .exectute(&mut self.connection) + .execute(&mut self.connection) .await?; Ok(()) }