95 lines
2.6 KiB
Rust
95 lines
2.6 KiB
Rust
|
use rusqlite::Connection;
|
||
|
|
||
|
use crate::data::{author::Author, book::Book, error::DataStoreError, pagination::SortOrder};
|
||
|
|
||
|
pub struct Calibre {
|
||
|
conn: Connection,
|
||
|
}
|
||
|
|
||
|
impl Calibre {
|
||
|
pub fn load(url: &str) -> Result<Self, DataStoreError> {
|
||
|
let conn = Connection::open(url)?;
|
||
|
Ok(Self { conn })
|
||
|
}
|
||
|
|
||
|
pub fn books(
|
||
|
&self,
|
||
|
limit: u64,
|
||
|
cursor: Option<&str>,
|
||
|
sort_order: SortOrder,
|
||
|
) -> Result<Vec<Book>, DataStoreError> {
|
||
|
Book::books(&self.conn, limit, cursor, sort_order)
|
||
|
}
|
||
|
|
||
|
pub fn authors(
|
||
|
&self,
|
||
|
limit: u64,
|
||
|
cursor: Option<&str>,
|
||
|
sort_order: SortOrder,
|
||
|
) -> Result<Vec<Author>, DataStoreError> {
|
||
|
Author::authors(&self.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> {
|
||
|
Book::author_books(&self.conn, author_id, limit, cursor, sort_order)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn books() {
|
||
|
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||
|
let books = c.books(10, None, SortOrder::ASC).unwrap();
|
||
|
assert_eq!(books.len(), 4);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn authors() {
|
||
|
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||
|
let authors = c.authors(10, None, SortOrder::ASC).unwrap();
|
||
|
assert_eq!(authors.len(), 3);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn author_books() {
|
||
|
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||
|
let books = c.author_books(1, 10, None, SortOrder::ASC).unwrap();
|
||
|
assert_eq!(books.len(), 2);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn pagination() {
|
||
|
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||
|
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");
|
||
|
}
|
||
|
}
|