213 lines
6.9 KiB
Rust
213 lines
6.9 KiB
Rust
//! Bundle all functions together.
|
|
|
|
use std::path::Path;
|
|
|
|
use r2d2::Pool;
|
|
use r2d2_sqlite::SqliteConnectionManager;
|
|
|
|
use crate::data::{
|
|
author::Author, book::Book, error::DataStoreError, pagination::SortOrder, series::Series,
|
|
};
|
|
|
|
/// Top level calibre functions, bundling all sub functions in one place and providing secure access to
|
|
/// the database.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Calibre {
|
|
pool: Pool<SqliteConnectionManager>,
|
|
}
|
|
|
|
impl Calibre {
|
|
/// Open a connection to the calibre database.
|
|
///
|
|
/// Fail if the database file can not be opened or not be found.
|
|
pub fn load(path: &Path) -> Result<Self, DataStoreError> {
|
|
let manager = SqliteConnectionManager::file(path);
|
|
let pool = r2d2::Pool::new(manager)?;
|
|
|
|
Ok(Self { pool })
|
|
}
|
|
|
|
/// Fetch book data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
|
|
/// ordering by `sort_order`.
|
|
pub fn books(
|
|
&self,
|
|
limit: u64,
|
|
cursor: Option<&str>,
|
|
sort_order: &SortOrder,
|
|
) -> Result<Vec<Book>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::multiple(&conn, limit, cursor, sort_order)
|
|
}
|
|
|
|
/// Fetch author data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
|
|
/// ordering by `sort_order`.
|
|
pub fn authors(
|
|
&self,
|
|
limit: u64,
|
|
cursor: Option<&str>,
|
|
sort_order: &SortOrder,
|
|
) -> Result<Vec<Author>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Author::multiple(&conn, limit, cursor, sort_order)
|
|
}
|
|
|
|
/// Fetch books for an author specified by `author_id`, paginate the books by starting at `cursor`,
|
|
/// fetching up to an amount of `limit` and ordering by `sort_order`.
|
|
pub fn author_books(
|
|
&self,
|
|
author_id: u64,
|
|
limit: u64,
|
|
cursor: Option<&str>,
|
|
sort_order: SortOrder,
|
|
) -> Result<Vec<Book>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::author_books(&conn, author_id, limit, cursor, sort_order)
|
|
}
|
|
|
|
/// Get recent books up to a limit of `limit`.
|
|
pub fn recent_books(&self, limit: u64) -> Result<Vec<Book>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::recents(&conn, limit)
|
|
}
|
|
|
|
/// Get a single book, specified `id`.
|
|
pub fn scalar_book(&self, id: u64) -> Result<Book, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::scalar_book(&conn, id)
|
|
}
|
|
|
|
/// Get the author to a book with id `id`.
|
|
pub fn book_author(&self, id: u64) -> Result<Author, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Author::book_author(&conn, id)
|
|
}
|
|
|
|
/// Fetch series data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
|
|
/// ordering by `sort_order`.
|
|
pub fn series(
|
|
&self,
|
|
limit: u64,
|
|
cursor: Option<&str>,
|
|
sort_order: &SortOrder,
|
|
) -> Result<Vec<Series>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Series::multiple(&conn, limit, cursor, sort_order)
|
|
}
|
|
|
|
/// Get the series a book with id `id` is in, as well as the book's position within the series.
|
|
pub fn book_series(&self, id: u64) -> Result<Option<(Series, f64)>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Series::book_series(&conn, id)
|
|
}
|
|
|
|
/// Get all books belonging to the series with id `id`.
|
|
pub fn series_books(&self, id: u64) -> Result<Vec<Book>, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::series_books(&conn, id)
|
|
}
|
|
|
|
/// Check if there are more authors before the specified cursor.
|
|
pub fn has_previous_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Author::has_previous_authors(&conn, author_sort)
|
|
}
|
|
|
|
/// Check if there are more authors after the specified cursor.
|
|
pub fn has_more_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Author::has_more_authors(&conn, author_sort)
|
|
}
|
|
|
|
/// Check if there are more books before the specified cursor.
|
|
pub fn has_previous_books(&self, book_sort: &str) -> Result<bool, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::has_previous_books(&conn, book_sort)
|
|
}
|
|
|
|
/// Check if there are more books after the specified cursor.
|
|
pub fn has_more_books(&self, book_sort: &str) -> Result<bool, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Book::has_more_books(&conn, book_sort)
|
|
}
|
|
|
|
/// Check if there are more series before the specified cursor.
|
|
pub fn has_previous_series(&self, series_sort: &str) -> Result<bool, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Series::has_previous_series(&conn, series_sort)
|
|
}
|
|
|
|
/// Check if there are more series after the specified cursor.
|
|
pub fn has_more_series(&self, series_sort: &str) -> Result<bool, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Series::has_more_series(&conn, series_sort)
|
|
}
|
|
|
|
/// Fetch a single author with id `id`.
|
|
pub fn scalar_author(&self, id: u64) -> Result<Author, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Author::scalar_author(&conn, id)
|
|
}
|
|
|
|
/// Fetch a single series with id `id`.
|
|
pub fn scalar_series(&self, id: u64) -> Result<Series, DataStoreError> {
|
|
let conn = self.pool.get()?;
|
|
Series::scalar_series(&conn, id)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn init_calibre() -> Calibre {
|
|
Calibre::load(Path::new("./testdata/metadata.db")).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn books() {
|
|
let c = init_calibre();
|
|
let books = c.books(10, None, &SortOrder::ASC).unwrap();
|
|
assert_eq!(books.len(), 4);
|
|
}
|
|
|
|
#[test]
|
|
fn authors() {
|
|
let c = init_calibre();
|
|
let authors = c.authors(10, None, &SortOrder::ASC).unwrap();
|
|
assert_eq!(authors.len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn author_books() {
|
|
let c = init_calibre();
|
|
let books = c.author_books(1, 10, None, SortOrder::ASC).unwrap();
|
|
assert_eq!(books.len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn pagination() {
|
|
let c = init_calibre();
|
|
let authors = c.authors(1, None, &SortOrder::ASC).unwrap();
|
|
assert_eq!(authors.len(), 1);
|
|
assert_eq!(authors[0].name, "Kevin R. Grazier");
|
|
|
|
let authors = c
|
|
.authors(1, Some(&authors[0].sort), &SortOrder::ASC)
|
|
.unwrap();
|
|
assert_eq!(authors.len(), 1);
|
|
assert_eq!(authors[0].name, "Terry Pratchett");
|
|
|
|
let authors = c
|
|
.authors(1, Some(&authors[0].sort), &SortOrder::ASC)
|
|
.unwrap();
|
|
assert_eq!(authors.len(), 1);
|
|
assert_eq!(authors[0].name, "Edward Noyes Westcott");
|
|
|
|
let authors = c
|
|
.authors(1, Some(&authors[0].sort), &SortOrder::DESC)
|
|
.unwrap();
|
|
assert_eq!(authors.len(), 1);
|
|
assert_eq!(authors[0].name, "Terry Pratchett");
|
|
}
|
|
}
|