paginate books

This commit is contained in:
Sebastian Hugentobler 2024-05-06 14:42:14 +02:00
parent 6d949bb21e
commit 6a79f0c1ed
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
10 changed files with 99 additions and 13 deletions

View file

@ -28,9 +28,9 @@ impl Book {
conn: &Connection,
limit: u64,
cursor: Option<&str>,
sort_order: SortOrder,
sort_order: &SortOrder,
) -> Result<Vec<Self>, DataStoreError> {
let pagination = Pagination::new("sort", cursor, limit, sort_order);
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
pagination.paginate(
conn,
"SELECT id, title, sort, path FROM books",
@ -71,4 +71,22 @@ impl Book {
let params = named_params! { ":id": id };
Ok(stmt.query_row(params, Self::from_row)?)
}
pub fn has_previous_books(conn: &Connection, sort_title: &str) -> Result<bool, DataStoreError> {
let mut stmt = conn
.prepare("SELECT Count(1) FROM books WHERE sort < (:sort_title) ORDER BY sort DESC")?;
let params = named_params! { ":sort_title": sort_title};
let count: u64 = stmt.query_row(params, |x| x.get(0))?;
Ok(count > 0)
}
pub fn has_more_books(conn: &Connection, sort_title: &str) -> Result<bool, DataStoreError> {
let mut stmt = conn
.prepare("SELECT Count(1) FROM books WHERE sort > (:sort_title) ORDER BY sort ASC")?;
let params = named_params! { ":sort_title": sort_title};
let count: u64 = stmt.query_row(params, |x| x.get(0))?;
Ok(count > 0)
}
}