Added some logging in database code and fixed some warnings
This commit is contained in:
parent
2cb4acc604
commit
779381df2d
|
@ -1,3 +1,4 @@
|
||||||
|
use anyhow::bail;
|
||||||
use sqlx::{postgres::PgPoolOptions, Postgres};
|
use sqlx::{postgres::PgPoolOptions, Postgres};
|
||||||
|
|
||||||
use crate::config::definition::Config;
|
use crate::config::definition::Config;
|
||||||
|
@ -14,12 +15,24 @@ pub struct Database {
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
pub async fn new(config: &Config) -> anyhow::Result<Self> {
|
pub async fn new(config: &Config) -> anyhow::Result<Self> {
|
||||||
|
log::info!("Database connecting to {}", config.database.url);
|
||||||
let conn = PgPoolOptions::new()
|
let conn = PgPoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
.connect(&config.database.url).await?;
|
.connect(&config.database.url).await;
|
||||||
Ok(Self {
|
|
||||||
connection: conn
|
|
||||||
})
|
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<Postgres> {
|
pub fn connection(&self) -> &sqlx::Pool<Postgres> {
|
||||||
|
|
|
@ -19,10 +19,10 @@ impl Users {
|
||||||
let hash = bcrypt::hash(password, 15)?;
|
let hash = bcrypt::hash(password, 15)?;
|
||||||
|
|
||||||
sqlx::query(r#"
|
sqlx::query(r#"
|
||||||
INSERT INTO users ( id, email, username, pw_hash, permissions )
|
INSERT INTO users ( id, email, username, pw_hash, permissions )
|
||||||
VALUES ( $1, $2, $3, $4, 0 )
|
VALUES ( $1, $2, $3, $4, 0 )
|
||||||
RETURNING id
|
RETURNING id
|
||||||
"#)
|
"#)
|
||||||
.bind(id)
|
.bind(id)
|
||||||
.bind(email)
|
.bind(email)
|
||||||
.bind(username)
|
.bind(username)
|
||||||
|
@ -30,6 +30,7 @@ impl Users {
|
||||||
.execute(db.connection())
|
.execute(db.connection())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
log::debug!("Created user with id '{id}'");
|
||||||
|
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
@ -119,13 +120,15 @@ impl Users {
|
||||||
|
|
||||||
pub async fn remove_user(&self, db: &mut Database) -> anyhow::Result<()> {
|
pub async fn remove_user(&self, db: &mut Database) -> anyhow::Result<()> {
|
||||||
sqlx::query(r#"
|
sqlx::query(r#"
|
||||||
DELETE FROM users
|
DELETE FROM users
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
"#)
|
"#)
|
||||||
.bind(self.id)
|
.bind(self.id)
|
||||||
.execute(db.connection())
|
.execute(db.connection())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
log::debug!("Deleted user with id '{}'", self.id);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,6 @@ mod templates;
|
||||||
|
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
use actix_files as actix_fs;
|
use actix_files as actix_fs;
|
||||||
use sqlx::database;
|
|
||||||
|
|
||||||
use crate::{config::definition::Config, database::Database};
|
use crate::{config::definition::Config, database::Database};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user