Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeclover authored Apr 25, 2024
1 parent e608c16 commit 1fb38e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static USER_ID: Lazy<Arc<Mutex<i32>>> = Lazy::new(|| Arc::new(Mutex::new(0)));
lazy_static::lazy_static! {
static ref DB_CLIENT: Arc<Mutex<DatabaseConnectix>> = {
tokio::runtime::Runtime::new().unwrap().block_on(async {
return Arc::new(Mutex::new(DatabaseConnectix::default()));
return Arc::new(Mutex::new(DatabaseConnectix::new(std::env::var("DB_URL").unwrap())));
})
};
}
Expand Down
34 changes: 9 additions & 25 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Error;
use anyhow::{Error, anyhow};
use sqlx::{query_as, query, postgres::{PgPoolOptions, PgPool}, types::Json};
use super::model::{Model, GlassModeration};
use uuid::Uuid;
Expand All @@ -8,22 +8,6 @@ pub struct DatabaseConnectix {
connection: PgPool
}

impl Default for DatabaseConnectix {
fn default() -> Result<Self, Error> {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let uri = std::env::var("DB_URL")?;
let client = PgPoolOptions::new()
.max_connections(5)
.connect(&uri).await?;

Ok(Self {
connection: client
})
})

}
}

impl DatabaseConnectix {
pub fn new(uri: &str) -> Result<Self, Error> {
tokio::runtime::Runtime::new().unwrap().block_on(async {
Expand All @@ -38,23 +22,23 @@ 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> {
pub async fn get_user_id(&self, username: &str) -> Result<i32, Error> {
let user: Option<Model> = query_as(
"select max(id) from users where username=$1 limit 1;"
)
.bind(username)
.fetch_optional(&mut self.connection)
.fetch_optional(&self.connection)
.await?;

if user.is_none() {
Ok(1)
} else {
if user.id == 9999 { return Err(anyhow!("username is taken")); }
Ok(user.id+1)
if user.unwrap().id == 9999 { return Err(anyhow!("username is taken")); }
Ok(user.unwrap().id+1)
}
}

pub async fn post_user(&mut self, username: String, password: String) -> Result<(), Error> {
pub async fn post_user(&self, username: String, password: String) -> Result<(), Error> {
let data: Model = Model {
id: self.get_user_id(&username).await?,
uuid: Uuid::new_v4(),
Expand All @@ -66,17 +50,17 @@ 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)
.execute(&mut self.connection)
.execute(&self.connection)
.await?;
Ok(())
}

pub async fn update_user(&mut self, username: &str, prev_username: &str, prev_id: i32) -> Result<(), Error> {
pub async fn update_user(&self, username: &str, prev_username: &str, prev_id: i32) -> Result<(), Error> {
let id = self.get_user_id(username).await?;

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)
.execute(&mut self.connection)
.execute(&self.connection)
.await?;
Ok(())
}
Expand Down

0 comments on commit 1fb38e0

Please sign in to comment.