draft bookmark toggle action

This commit is contained in:
yggverse 2024-11-14 09:30:40 +02:00
parent feca899c5b
commit 232f67b9cc
4 changed files with 49 additions and 5 deletions

View file

@ -31,6 +31,26 @@ impl Database {
let tx = readable.unchecked_transaction().unwrap();
select(&tx, self.profile_id, request).unwrap()
}
// Setters
pub fn add(&self, time: DateTime, request: String) -> Result<i64, ()> {
// Begin new transaction
let mut writable = self.connection.write().unwrap();
let tx = writable.transaction().unwrap();
// Create new record
insert(&tx, self.profile_id, time, request).unwrap();
// Hold insert ID for result
let id = last_insert_id(&tx);
// Done
match tx.commit() {
Ok(_) => Ok(id),
Err(_) => Err(()), // @TODO
}
}
}
// Low-level DB API
@ -102,3 +122,7 @@ pub fn select(
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id])
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
}

View file

@ -9,6 +9,7 @@ pub struct Memory {
impl Memory {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
index: RefCell::new(HashMap::new()),
@ -32,6 +33,7 @@ impl Memory {
index.insert(request, time);
}
/// Check request exist in memory index
pub fn is_exist(&self, request: &str) -> bool {
self.index.borrow().get(request).is_some()
}