diff --git a/src/app.rs b/src/app.rs index 580443f2..5dc859c2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -31,8 +31,11 @@ pub struct App { impl App { // Construct pub fn new(profile_database_connection: Arc) -> Self { - // Init app database model - let database = Arc::new(Database::init(profile_database_connection)); + // Init database model + let database = match Database::init(profile_database_connection) { + Ok(database) => Arc::new(database), + Err(e) => panic!("{e}"), // @TODO + }; // Init actions let action_debug = Action::new("win", true); diff --git a/src/app/database.rs b/src/app/database.rs index 134d2637..9ee711bb 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -1,8 +1,6 @@ use sqlite::{Connection, Error}; use std::sync::Arc; -const DEBUG: bool = true; // @TODO - pub struct Table { pub id: i64, pub time: i64, @@ -13,33 +11,21 @@ pub struct Database { } impl Database { - pub fn init(connection: Arc) -> Database { - // Init app table - if let Err(error) = connection.execute( + pub fn init(connection: Arc) -> Result { + connection.execute( "CREATE TABLE IF NOT EXISTS `app` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP )", [], - ) { - panic!("{error}"); // @TODO - } + )?; - // Return struct - Self { connection } + Ok(Self { connection }) } - pub fn add(&self) -> Result { - return match self.connection.execute("INSERT INTO `app`", []) { - Ok(total) => { - if DEBUG { - println!("Inserted {total} row to `app` table"); - } - Ok(total) - } - Err(error) => Err(error.to_string()), - }; + pub fn add(&self) -> Result { + self.connection.execute("INSERT INTO `app`", []) } pub fn records(&self) -> Result, Error> { @@ -57,19 +43,9 @@ impl Database { Ok(records) } - pub fn delete(&self, id: i64) -> Result { - return match self - .connection + pub fn delete(&self, id: i64) -> Result { + self.connection .execute("DELETE FROM `app` WHERE `id` = ?", [id]) - { - Ok(total) => { - if DEBUG { - println!("Deleted {total} row(s) from `app` table"); - } - Ok(total) - } - Err(error) => Err(error.to_string()), - }; } pub fn last_insert_id(&self) -> i64 {