Skip to content

Commit

Permalink
finish code
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeclover authored Apr 25, 2024
1 parent 0ad1fe7 commit af94a5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
22 changes: 15 additions & 7 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;
use postgres::{Client, NoTls};

use super::model::Model;
use super::model::{Model, User};

pub struct DatabaseConnectix {
connection: Client
Expand All @@ -11,7 +11,7 @@ impl Default for DatabaseConnectix {
fn default() -> Self {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let uri = std::env::var("DB_URL").unwrap();
let mut client = Client::connect(&uri, NoTls)?;
let mut client = Client::connect(&uri, NoTls).expect("can't connect to database!");

return Self {
connection: client
Expand All @@ -24,7 +24,7 @@ impl Default for DatabaseConnectix {
impl DatabaseConnectix {
pub fn new(uri: &str) -> Self {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let mut client = Client::connect(uri, NoTls)?;
let mut client = Client::connect(uri, NoTls).expect("can't connect to database!");

return Self {
connection: client
Expand All @@ -33,13 +33,21 @@ impl DatabaseConnectix {
}

/// Gets a possible user id (if one exists) for a username.
pub async fn get_user_id(&self, name: &str) -> Result<i32, Box<dyn Error>> {
todo!("am eepy must finish tomorrow");
/*if let Some(res) = ModelEntity::find().expr(Expr::col("id").max()).filter(ModelColumn::Name.contains(name)).one(&self.connection).await? {
pub fn get_user_id(&self, name: &str) -> Result<i32, Box<dyn Error>> {
if let Some(res) = self.connection.query_one("select max(id) from users where name=$1", &[&name])? {
if res.id == 9999 { return Err("username is taken".into()); }
Ok(res.id+1)
} else {
Ok(1)
}*/
}
}

pub fn post_user(&self, user: User) {
let data: Model = user.into();
let Model { id, name, uuid, moderation_stats } = data;
self.connection.execute(
"INSERT INTO users (id, name, uuid, mod) VALUES ($1, $2, $3, $4)",
&[&id, &name, &uuid, &moderation_stats],
)?;
}
}
19 changes: 15 additions & 4 deletions src/user/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,26 @@ impl User {
/// A struct so that we can save user data in the database.
#[derive(Serialize, Deserialize, Clone)]
pub struct Model {
pub user_number: i32,
pub id: i32,
pub name: String,
pub uuid: Uuid,
pub password: String,
pub email: String,
//pub password: String,
//pub email: String,
/// This is just the DB equivalent of `glass`.
/// It's in JSON format.
pub moderation_stats: String
pub moderation_stats: Json<GlassModeration>
}

impl From<User> for Model {
fn from(item: User) -> Self {
let (name, id) = item.name_split();
Self {
id,
name,
uuid: item.uuid,
moderation_stats: item.glass
}
}
}

/// What am I?
Expand Down

0 comments on commit af94a5b

Please sign in to comment.