//! Author data. use rusqlite::{Connection, Row, named_params}; use serde::Serialize; use snafu::{ResultExt, Snafu}; use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder}; /// Author in calibre. #[derive(Debug, Clone, Serialize)] pub struct Author { /// Id in database. pub id: u64, /// Full name. pub name: String, /// Full name for sorting. pub sort: String, } #[derive(Debug, Snafu)] #[snafu(display("Failed to fetch multiple authors."))] pub struct MultipleAuthorsError { source: PaginationError, } #[derive(Debug, Snafu)] pub enum BookAuthorError { #[snafu(display("Failed to prepare statement."))] PrepareBookAuthor { source: rusqlite::Error }, #[snafu(display("Failed to execute statement."))] ExecuteBookAuthor { source: rusqlite::Error }, } #[derive(Debug, Snafu)] pub enum ScalarAuthorError { #[snafu(display("Failed to prepare statement."))] PrepareScalarAuthor { source: rusqlite::Error }, #[snafu(display("Failed to execute statement."))] ExecuteScalarAuthor { source: rusqlite::Error }, } #[derive(Debug, Snafu)] #[snafu(display("Failed to check for previous authors."))] pub struct PreviousAuthorsError { source: HasPrevOrMoreError, } #[derive(Debug, Snafu)] #[snafu(display("Failed to check for more authors."))] pub struct MoreAuthorsError { source: HasPrevOrMoreError, } impl Author { fn from_row(row: &Row<'_>) -> Result { Ok(Self { id: row.get(0)?, name: row.get(1)?, sort: row.get(2)?, }) } /// Fetch author data from calibre, starting at `cursor`, fetching up to an amount of `limit` and /// ordering by `sort_order`. pub fn multiple( conn: &Connection, limit: u64, cursor: Option<&str>, sort_order: &SortOrder, ) -> Result, MultipleAuthorsError> { let pagination = Pagination::new("sort", cursor, limit, *sort_order); pagination .paginate( conn, "SELECT id, name, sort FROM authors", &[], Self::from_row, ) .context(MultipleAuthorsSnafu) } /// Get the author to a book with id `id`. pub fn book_author(conn: &Connection, id: u64) -> Result { let mut stmt = conn .prepare( "SELECT authors.id, authors.name, authors.sort FROM authors \ INNER JOIN books_authors_link ON authors.id = books_authors_link.author \ WHERE books_authors_link.book = (:id)", ) .context(PrepareBookAuthorSnafu)?; let params = named_params! { ":id": id }; stmt.query_row(params, Self::from_row) .context(ExecuteBookAuthorSnafu) } /// Fetch a single author with id `id`. pub fn scalar_author(conn: &Connection, id: u64) -> Result { let mut stmt = conn .prepare("SELECT id, name, sort FROM authors WHERE id = (:id)") .context(PrepareScalarAuthorSnafu)?; let params = named_params! { ":id": id }; stmt.query_row(params, Self::from_row) .context(ExecuteScalarAuthorSnafu) } /// Check if there are more authors before the specified cursor. pub fn has_previous_authors( conn: &Connection, sort_name: &str, ) -> Result { Pagination::has_prev_or_more(conn, "authors", sort_name, &SortOrder::DESC) .context(PreviousAuthorsSnafu) } /// Check if there are more authors after the specified cursor. pub fn has_more_authors(conn: &Connection, sort_name: &str) -> Result { Pagination::has_prev_or_more(conn, "authors", sort_name, &SortOrder::ASC) .context(MoreAuthorsSnafu) } }