add history model

This commit is contained in:
yggverse 2024-11-12 17:07:00 +02:00
parent 4a593f2245
commit 5b3e091f2b
2 changed files with 70 additions and 0 deletions

View File

@ -1,7 +1,9 @@
mod bookmark;
mod history;
mod identity;
use bookmark::Bookmark;
use history::History;
use identity::Identity;
use sqlite::{Connection, Error};
@ -55,6 +57,7 @@ fn init(mut connection: RwLockWriteGuard<'_, Connection>) -> Result<(), Error> {
// Init profile components
Bookmark::init(&transaction)?;
History::init(&transaction)?;
Identity::init(&transaction)?;
// Apply changes

View File

@ -0,0 +1,67 @@
use gtk::glib::DateTime;
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
pub time: DateTime,
pub request: String,
}
pub struct History {
// nothing yet..
}
impl History {
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `history`
(
`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<usize, Error> {
tx.execute(
"INSERT INTO `history` (
`time`,
`request`
) VALUES (?, ?)",
(time.to_unix(), request),
)
}
pub fn records(tx: &Transaction, request: Option<&str>) -> Result<Vec<Table>, Error> {
let mut stmt =
tx.prepare("SELECT `id`, `time`, `request` FROM `history` 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<usize, Error> {
tx.execute("DELETE FROM `history` WHERE `id` = ?", [id])
}
}