Skip to content

Commit

Permalink
fix alot of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeclover committed Apr 25, 2024
1 parent a23e097 commit 6486411
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
19 changes: 9 additions & 10 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::error::Error;
use anyhow::Result;
use sqlx::{query_as, query, postgres::{PgPoolOptions, PgPool}, types::Json};
use super::model::{Model, User};
use super::model::{Model, User, GlassModeration};
use uuid::Uuid;

pub struct DatabaseConnectix {
Expand All @@ -12,9 +11,9 @@ impl Default for DatabaseConnectix {
fn default() -> Result<Self> {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let uri = std::env::var("DB_URL")?;
let pool = PgPoolOptions::new()
let client = PgPoolOptions::new()
.max_connections(5)
.connect(uri).await?;
.connect(&uri).await?;

return Ok(Self {
connection: client
Expand All @@ -38,7 +37,7 @@ impl DatabaseConnectix {
}

/// Gets a possible user id (if one exists) for a username.
pub fn get_user_id(&mut self, username: &str) -> Result<i32> {
pub async fn get_user_id(&mut self, username: &str) -> Result<i32> {
let user: Option<User> = query_as(
"select max(id) from users where username=$1 limit 1;"
)
Expand All @@ -54,27 +53,27 @@ impl DatabaseConnectix {
}
}

pub fn post_user(&mut self, username: &str, password: &str) -> Result<()> {
pub async fn post_user(&mut self, username: String, password: String) -> Result<()> {
let data: Model = Model {
id: self.get_user_id(username)?,
uuid: Uuid::new_v4(),
username,
password,
glass: Json(GlassModeration::default())
moderation_stats: Json(GlassModeration::default())
};
data.validate()?;

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.glass)
.bind(data.id).bind(data.uuid).bind(data.username).bind(data.password).bind(data.moderation_stats)
.exectute(&mut self.connection)
.await?;
Ok(())
}

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

let _ = sqlx::query("update users set username=$1, id=$2 where username=$3 and id=$4")
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)
.await?;
Expand Down
3 changes: 2 additions & 1 deletion src/user/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustrict::{Censor, Type};
use serde::{Serialize, Deserialize};
use sqlx::{FromRow, types::Json};
use uuid::Uuid;
use validator::Validate;

/// What am I?
/// A class meant to hold all the values the server uses to compute messages.
Expand Down Expand Up @@ -37,7 +38,7 @@ impl User {

/// What am I?
/// A struct so that we can save user data in the database.
#[derive(Serialize, Deserialize, Clone, sqlx::FromRow, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, FromRow, Debug, Validate)]
pub struct Model {
pub id: i32,
pub uuid: Uuid,
Expand Down

0 comments on commit 6486411

Please sign in to comment.