basic recents view
This commit is contained in:
parent
65e17fc55b
commit
687c33829f
20 changed files with 2156 additions and 20 deletions
|
@ -1,15 +1,17 @@
|
|||
use rusqlite::{Connection, Row};
|
||||
use rusqlite::{named_params, Connection, Row};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Book {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub sort: String,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
impl Book {
|
||||
|
@ -18,6 +20,7 @@ impl Book {
|
|||
id: row.get(0)?,
|
||||
title: row.get(1)?,
|
||||
sort: row.get(2)?,
|
||||
path: row.get(3)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -30,7 +33,7 @@ impl Book {
|
|||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT id, title, sort FROM books",
|
||||
"SELECT id, title, sort, path FROM books",
|
||||
&[],
|
||||
Self::from_row,
|
||||
)
|
||||
|
@ -46,11 +49,26 @@ impl Book {
|
|||
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT books.id, books.title, books.sort FROM books \
|
||||
"SELECT books.id, books.title, books.sort, books.path FROM books \
|
||||
INNER JOIN books_authors_link ON books.id = books_authors_link.book \
|
||||
WHERE books_authors_link.author = (:author_id) AND",
|
||||
&[(":author_id", &author_id)],
|
||||
Self::from_row,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn recents(conn: &Connection, limit: u64) -> Result<Vec<Book>, DataStoreError> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT id, title, sort, path FROM books ORDER BY timestamp DESC LIMIT (:limit)",
|
||||
)?;
|
||||
let params = named_params! { ":limit": limit };
|
||||
let iter = stmt.query_map(params, Self::from_row)?;
|
||||
Ok(iter.filter_map(Result::ok).collect())
|
||||
}
|
||||
|
||||
pub fn scalar_book(conn: &Connection, id: u64) -> Result<Book, DataStoreError> {
|
||||
let mut stmt = conn.prepare("SELECT id, title, sort, path FROM books WHERE id = (:id)")?;
|
||||
let params = named_params! { ":id": id };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue