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 crate::config::definition::Config;
 | 
			
		||||
| 
						 | 
				
			
			@ -14,12 +15,24 @@ pub struct Database {
 | 
			
		|||
 | 
			
		||||
impl Database {
 | 
			
		||||
    pub async fn new(config: &Config) -> anyhow::Result<Self> {
 | 
			
		||||
        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<Postgres> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user