diff --git a/src/user/auth.rs b/src/user/auth.rs index bc131dff..ee7f554c 100644 --- a/src/user/auth.rs +++ b/src/user/auth.rs @@ -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 @@ -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 @@ -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 @@ -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> { - 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> { + 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], + )?; } } \ No newline at end of file diff --git a/src/user/model.rs b/src/user/model.rs index c2959904..2ad1ba85 100644 --- a/src/user/model.rs +++ b/src/user/model.rs @@ -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 +} + +impl From 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?