Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeclover committed Apr 25, 2024
1 parent 760db8b commit e608c16
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<i32, Error> {
let user: Option<User> = query_as(
let user: Option<Model> = query_as(
"select max(id) from users where username=$1 limit 1;"
)
.bind(username)
Expand All @@ -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,
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand Down

0 comments on commit e608c16

Please sign in to comment.