WIP
This commit is contained in:
parent
1c95f4391f
commit
b4a0aadef9
73 changed files with 2993 additions and 1632 deletions
|
@ -1,12 +1,9 @@
|
|||
//! Author data.
|
||||
|
||||
use rusqlite::{named_params, Connection, Row};
|
||||
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
||||
use rusqlite::{Connection, Row, named_params};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
/// Author in calibre.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
@ -19,6 +16,40 @@ pub struct Author {
|
|||
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<Self, rusqlite::Error> {
|
||||
Ok(Self {
|
||||
|
@ -35,44 +66,54 @@ impl Author {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
) -> Result<Vec<Self>, MultipleAuthorsError> {
|
||||
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT id, name, sort FROM authors",
|
||||
&[],
|
||||
Self::from_row,
|
||||
)
|
||||
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<Self, DataStoreError> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT authors.id, authors.name, authors.sort FROM authors \
|
||||
pub fn book_author(conn: &Connection, id: u64) -> Result<Self, BookAuthorError> {
|
||||
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 };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
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<Self, DataStoreError> {
|
||||
let mut stmt = conn.prepare("SELECT id, name, sort FROM authors WHERE id = (:id)")?;
|
||||
pub fn scalar_author(conn: &Connection, id: u64) -> Result<Self, ScalarAuthorError> {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id, name, sort FROM authors WHERE id = (:id)")
|
||||
.context(PrepareScalarAuthorSnafu)?;
|
||||
let params = named_params! { ":id": id };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
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<bool, DataStoreError> {
|
||||
) -> Result<bool, PreviousAuthorsError> {
|
||||
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<bool, DataStoreError> {
|
||||
pub fn has_more_authors(conn: &Connection, sort_name: &str) -> Result<bool, MoreAuthorsError> {
|
||||
Pagination::has_prev_or_more(conn, "authors", sort_name, &SortOrder::ASC)
|
||||
.context(MoreAuthorsSnafu)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue