little-hesinde/calibre-db/src/calibre.rs

150 lines
4.1 KiB
Rust

use std::path::Path;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use crate::data::{
author::Author, book::Book, error::DataStoreError, pagination::SortOrder, series::Series,
};
#[derive(Debug, Clone)]
pub struct Calibre {
pool: Pool<SqliteConnectionManager>,
}
impl Calibre {
pub fn load(path: &Path) -> Result<Self, DataStoreError> {
let manager = SqliteConnectionManager::file(path);
let pool = r2d2::Pool::new(manager)?;
Ok(Self { pool })
}
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)
}
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)
}
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)
}
pub fn recent_books(&self, limit: u64) -> Result<Vec<Book>, DataStoreError> {
let conn = self.pool.get()?;
Book::recents(&conn, limit)
}
pub fn scalar_book(&self, id: u64) -> Result<Book, DataStoreError> {
let conn = self.pool.get()?;
Book::scalar_book(&conn, id)
}
pub fn book_author(&self, id: u64) -> Result<Author, DataStoreError> {
let conn = self.pool.get()?;
Author::book_author(&conn, id)
}
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)
}
pub fn book_series(&self, id: u64) -> Result<Option<(Series, f64)>, DataStoreError> {
let conn = self.pool.get()?;
Series::book_series(&conn, id)
}
pub fn has_previous_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
let conn = self.pool.get()?;
Author::has_previous_authors(&conn, author_sort)
}
pub fn has_more_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
let conn = self.pool.get()?;
Author::has_more_authors(&conn, author_sort)
}
}
#[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");
}
}