mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 16:45:27 +00:00
store optional page title
This commit is contained in:
parent
24ee6bb28f
commit
ce2b02569d
4 changed files with 46 additions and 30 deletions
|
|
@ -78,7 +78,7 @@ impl Page {
|
|||
pub fn bookmark(&self) {
|
||||
self.profile
|
||||
.bookmark
|
||||
.toggle(&self.navigation.request())
|
||||
.toggle(&self.navigation.request(), Some(&self.title()))
|
||||
.unwrap(); // @TODO
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,11 +27,8 @@ impl Bookmark {
|
|||
// Build initial index
|
||||
{
|
||||
let mut memory = memory.borrow_mut();
|
||||
for record in database.records(None)? {
|
||||
memory.add(Item {
|
||||
id: record.id,
|
||||
request: record.request,
|
||||
});
|
||||
for item in database.records(None, None)? {
|
||||
memory.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,17 +40,18 @@ impl Bookmark {
|
|||
|
||||
/// Toggle bookmark in `database` and `memory` index
|
||||
/// * return `true` on bookmark create, `false` on delete
|
||||
pub fn toggle(&self, request: &str) -> Result<bool> {
|
||||
pub fn toggle(&self, request: &str, title: Option<&str>) -> Result<bool> {
|
||||
let mut memory = self.memory.borrow_mut();
|
||||
Ok(match memory.delete_by_request(request) {
|
||||
Some(record) => {
|
||||
self.database.delete(record.id)?;
|
||||
Some(item) => {
|
||||
self.database.delete(item.id)?;
|
||||
false
|
||||
}
|
||||
None => {
|
||||
memory.add(Item {
|
||||
id: self.database.add(DateTime::now_local()?, request)?,
|
||||
id: self.database.add(DateTime::now_local()?, request, title)?,
|
||||
request: request.into(),
|
||||
title: title.map(|t| t.to_string()),
|
||||
});
|
||||
true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,20 +23,20 @@ impl Database {
|
|||
// Getters
|
||||
|
||||
/// Get bookmark records from database with optional filter by `request`
|
||||
pub fn records(&self, request: Option<&str>) -> Result<Vec<Item>> {
|
||||
pub fn records(&self, request: Option<&str>, title: Option<&str>) -> Result<Vec<Item>> {
|
||||
let readable = self.connection.read().unwrap(); // @TODO
|
||||
let tx = readable.unchecked_transaction()?;
|
||||
select(&tx, *self.profile_id, request)
|
||||
select(&tx, *self.profile_id, request, title)
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
||||
/// Create new bookmark record in database
|
||||
/// * return last insert ID on success
|
||||
pub fn add(&self, time: DateTime, request: &str) -> Result<i64> {
|
||||
pub fn add(&self, time: DateTime, request: &str, title: Option<&str>) -> Result<i64> {
|
||||
let mut writable = self.connection.write().unwrap(); // @TODO
|
||||
let tx = writable.transaction()?;
|
||||
let id = insert(&tx, *self.profile_id, time, request)?;
|
||||
let id = insert(&tx, *self.profile_id, time, request, title)?;
|
||||
tx.commit()?;
|
||||
Ok(id)
|
||||
}
|
||||
|
|
@ -61,40 +61,57 @@ pub fn init(tx: &Transaction) -> Result<usize> {
|
|||
`profile_id` INTEGER NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`request` TEXT NOT NULL,
|
||||
`title` TEXT NULL,
|
||||
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`)
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
||||
)",
|
||||
[],
|
||||
)?)
|
||||
}
|
||||
|
||||
pub fn insert(tx: &Transaction, profile_id: i64, time: DateTime, request: &str) -> Result<i64> {
|
||||
pub fn insert(
|
||||
tx: &Transaction,
|
||||
profile_id: i64,
|
||||
time: DateTime,
|
||||
request: &str,
|
||||
title: Option<&str>,
|
||||
) -> Result<i64> {
|
||||
tx.execute(
|
||||
"INSERT INTO `profile_bookmark` (
|
||||
`profile_id`,
|
||||
`time`,
|
||||
`request`
|
||||
) VALUES (?, ?, ?)",
|
||||
(profile_id, time.to_unix(), request),
|
||||
`request`,
|
||||
`title`
|
||||
) VALUES (?, ?, ?, ?)",
|
||||
(profile_id, time.to_unix(), request, title),
|
||||
)?;
|
||||
Ok(tx.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn select(tx: &Transaction, profile_id: i64, request: Option<&str>) -> Result<Vec<Item>> {
|
||||
pub fn select(
|
||||
tx: &Transaction,
|
||||
profile_id: i64,
|
||||
request: Option<&str>,
|
||||
title: Option<&str>,
|
||||
) -> Result<Vec<Item>> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`, `profile_id`, `time`, `request`
|
||||
"SELECT `id`, `profile_id`, `time`, `request`, `title`
|
||||
FROM `profile_bookmark`
|
||||
WHERE `profile_id` = ? AND `request` LIKE ?",
|
||||
WHERE `profile_id` = ? AND (`request` LIKE ? OR `title` LIKE ?)",
|
||||
)?;
|
||||
|
||||
let result = stmt.query_map((profile_id, request.unwrap_or("%")), |row| {
|
||||
Ok(Item {
|
||||
id: row.get(0)?,
|
||||
//profile_id: row.get(1)?,
|
||||
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
request: row.get(3)?,
|
||||
})
|
||||
})?;
|
||||
let result = stmt.query_map(
|
||||
(profile_id, request.unwrap_or("%"), title.unwrap_or("%")),
|
||||
|row| {
|
||||
Ok(Item {
|
||||
id: row.get(0)?,
|
||||
//profile_id: row.get(1)?,
|
||||
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
request: row.get(3)?,
|
||||
title: row.get(4)?,
|
||||
})
|
||||
},
|
||||
)?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,4 +2,5 @@
|
|||
pub struct Item {
|
||||
pub id: i64,
|
||||
pub request: String,
|
||||
pub title: Option<String>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue