implement bookmarks model

This commit is contained in:
yggverse 2024-11-12 14:13:13 +02:00
parent bb03d7710e
commit 4a593f2245

View File

@ -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<usize, Error> {
tx.execute(
"INSERT INTO `bookmark` (
`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 `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<usize, Error> {
tx.execute("DELETE FROM `bookmark` WHERE `id` = ?", [id])
}
}