diff --git a/src/database/mod.rs b/src/database/mod.rs index 36eed74..7accd78 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,3 +1,4 @@ +use anyhow::bail; use sqlx::{postgres::PgPoolOptions, Postgres}; use crate::config::definition::Config; @@ -14,12 +15,24 @@ pub struct Database { impl Database { pub async fn new(config: &Config) -> anyhow::Result { + log::info!("Database connecting to {}", config.database.url); let conn = PgPoolOptions::new() .max_connections(5) - .connect(&config.database.url).await?; - Ok(Self { - connection: conn - }) + .connect(&config.database.url).await; + + + match conn { + Ok(c) => { + log::info!("Connection successfull"); + Ok(Self { + connection: c + }) + }, + Err(e) => { + log::error!("Failed to connect to database: {e}"); + bail!(e) + }, + } } pub fn connection(&self) -> &sqlx::Pool { diff --git a/src/database/models/users.rs b/src/database/models/users.rs index 5c111c3..1744111 100644 --- a/src/database/models/users.rs +++ b/src/database/models/users.rs @@ -19,10 +19,10 @@ impl Users { let hash = bcrypt::hash(password, 15)?; sqlx::query(r#" - INSERT INTO users ( id, email, username, pw_hash, permissions ) - VALUES ( $1, $2, $3, $4, 0 ) - RETURNING id - "#) + INSERT INTO users ( id, email, username, pw_hash, permissions ) + VALUES ( $1, $2, $3, $4, 0 ) + RETURNING id + "#) .bind(id) .bind(email) .bind(username) @@ -30,6 +30,7 @@ impl Users { .execute(db.connection()) .await?; + log::debug!("Created user with id '{id}'"); Ok(id) } @@ -119,13 +120,15 @@ impl Users { pub async fn remove_user(&self, db: &mut Database) -> anyhow::Result<()> { sqlx::query(r#" - DELETE FROM users - WHERE id = $1 - "#) + DELETE FROM users + WHERE id = $1 + "#) .bind(self.id) .execute(db.connection()) .await?; + log::debug!("Deleted user with id '{}'", self.id); + Ok(()) } } \ No newline at end of file diff --git a/src/public/mod.rs b/src/public/mod.rs index 349afc6..210f9b1 100644 --- a/src/public/mod.rs +++ b/src/public/mod.rs @@ -4,7 +4,6 @@ mod templates; use actix_web::{web, App, HttpServer}; use actix_files as actix_fs; -use sqlx::database; use crate::{config::definition::Config, database::Database};