diff --git a/src/profile/database/bookmark.rs b/src/profile/database/bookmark.rs index aa7f8203..781ddeac 100644 --- a/src/profile/database/bookmark.rs +++ b/src/profile/database/bookmark.rs @@ -1,8 +1,10 @@ +use gtk::glib::DateTime; use sqlite::{Error, Transaction}; pub struct Table { pub id: i64, - // pub app_id: i64, not in use + pub time: DateTime, + pub request: String, } pub struct Bookmark { @@ -14,11 +16,52 @@ impl Bookmark { tx.execute( "CREATE TABLE IF NOT EXISTS `bookmark` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL, - `request` TEXT + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL, + `request` TEXT NOT NULL )", [], ) } + + pub fn add(tx: &Transaction, time: &DateTime, request: &str) -> Result { + tx.execute( + "INSERT INTO `bookmark` ( + `time`, + `request` + ) VALUES (?, ?)", + (time.to_unix(), request), + ) + } + + pub fn records(tx: &Transaction, request: Option<&str>) -> Result, Error> { + let mut stmt = + tx.prepare("SELECT `id`, `time`, `request` FROM `bookmark` WHERE `request` LIKE ?")?; + + let filter = match request { + Some(value) => value, + None => "%", + }; + + let result = stmt.query_map([filter], |row| { + Ok(Table { + id: row.get(0)?, + time: DateTime::from_unix_local(row.get(1)?).unwrap(), + request: row.get(2)?, + }) + })?; + + let mut records = Vec::new(); + + for record in result { + let table = record?; + records.push(table); + } + + Ok(records) + } + + pub fn delete(tx: &Transaction, id: &i64) -> Result { + tx.execute("DELETE FROM `bookmark` WHERE `id` = ?", [id]) + } }