mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
add comments, handle results
This commit is contained in:
parent
41515b0f2b
commit
fa7080762e
@ -11,12 +11,13 @@ pub struct Table {
|
|||||||
|
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
connection: Rc<RwLock<Connection>>,
|
connection: Rc<RwLock<Connection>>,
|
||||||
profile_id: i64, // multi-profile relationship @TODO
|
profile_id: i64, // multi-profile relationship @TODO shared reference?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
|
/// Create new `Self`
|
||||||
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: i64) -> Self {
|
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: i64) -> Self {
|
||||||
Self {
|
Self {
|
||||||
connection,
|
connection,
|
||||||
@ -26,6 +27,7 @@ impl Database {
|
|||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
|
/// Get bookmark records from database with optional filter by `request`
|
||||||
pub fn records(&self, request: Option<&str>) -> Vec<Table> {
|
pub fn records(&self, request: Option<&str>) -> Vec<Table> {
|
||||||
let readable = self.connection.read().unwrap();
|
let readable = self.connection.read().unwrap();
|
||||||
let tx = readable.unchecked_transaction().unwrap();
|
let tx = readable.unchecked_transaction().unwrap();
|
||||||
@ -34,7 +36,9 @@ impl Database {
|
|||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
pub fn add(&self, time: DateTime, request: String) -> Result<i64, ()> {
|
/// Create new bookmark record in database
|
||||||
|
/// * return last insert ID on success
|
||||||
|
pub fn add(&self, time: DateTime, request: String) -> Result<i64, Error> {
|
||||||
// Begin new transaction
|
// Begin new transaction
|
||||||
let mut writable = self.connection.write().unwrap();
|
let mut writable = self.connection.write().unwrap();
|
||||||
let tx = writable.transaction().unwrap();
|
let tx = writable.transaction().unwrap();
|
||||||
@ -48,24 +52,24 @@ impl Database {
|
|||||||
// Done
|
// Done
|
||||||
match tx.commit() {
|
match tx.commit() {
|
||||||
Ok(_) => Ok(id),
|
Ok(_) => Ok(id),
|
||||||
Err(_) => Err(()), // @TODO
|
Err(reason) => Err(reason),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&self, id: i64) -> Result<(), ()> {
|
/// Delete bookmark record from database
|
||||||
|
pub fn delete(&self, id: i64) -> Result<(), Error> {
|
||||||
// Begin new transaction
|
// Begin new transaction
|
||||||
let mut writable = self.connection.write().unwrap();
|
let mut writable = self.connection.write().unwrap();
|
||||||
let tx = writable.transaction().unwrap();
|
let tx = writable.transaction().unwrap();
|
||||||
|
|
||||||
// Delete records match request
|
// Delete record by ID
|
||||||
match delete(&tx, id) {
|
match delete(&tx, id) {
|
||||||
Ok(_) => match tx.commit() {
|
Ok(_) => match tx.commit() {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => Err(()),
|
Err(reason) => Err(reason),
|
||||||
},
|
},
|
||||||
Err(_) => Err(()),
|
Err(reason) => Err(reason),
|
||||||
}
|
}
|
||||||
// @TODO handle result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user