2024-05-10 14:25:18 +02:00
|
|
|
//! Author data.
|
|
|
|
|
2025-07-02 21:09:37 +02:00
|
|
|
use rusqlite::{Connection, Row, named_params};
|
2024-05-02 18:10:29 +02:00
|
|
|
use serde::Serialize;
|
2025-07-02 21:09:37 +02:00
|
|
|
use snafu::{ResultExt, Snafu};
|
2024-05-01 16:21:07 +02:00
|
|
|
|
2025-07-02 21:47:56 +02:00
|
|
|
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Author in calibre.
|
2024-05-06 20:54:58 +02:00
|
|
|
#[derive(Debug, Clone, Serialize)]
|
2024-05-01 16:21:07 +02:00
|
|
|
pub struct Author {
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Id in database.
|
2024-05-06 09:09:40 +02:00
|
|
|
pub id: u64,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Full name.
|
2024-05-01 16:21:07 +02:00
|
|
|
pub name: String,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Full name for sorting.
|
2024-05-01 16:21:07 +02:00
|
|
|
pub sort: String,
|
|
|
|
}
|
|
|
|
|
2025-07-02 21:09:37 +02:00
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
2024-05-01 16:21:07 +02:00
|
|
|
impl Author {
|
|
|
|
fn from_row(row: &Row<'_>) -> Result<Self, rusqlite::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
id: row.get(0)?,
|
|
|
|
name: row.get(1)?,
|
|
|
|
sort: row.get(2)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Fetch author data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
|
|
|
|
/// ordering by `sort_order`.
|
2024-05-06 09:09:40 +02:00
|
|
|
pub fn multiple(
|
2024-05-01 16:21:07 +02:00
|
|
|
conn: &Connection,
|
|
|
|
limit: u64,
|
|
|
|
cursor: Option<&str>,
|
2024-05-06 13:51:49 +02:00
|
|
|
sort_order: &SortOrder,
|
2025-07-02 21:09:37 +02:00
|
|
|
) -> Result<Vec<Self>, MultipleAuthorsError> {
|
2024-05-06 13:51:49 +02:00
|
|
|
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
|
2025-07-02 21:09:37 +02:00
|
|
|
pagination
|
|
|
|
.paginate(
|
|
|
|
conn,
|
|
|
|
"SELECT id, name, sort FROM authors",
|
|
|
|
&[],
|
|
|
|
Self::from_row,
|
|
|
|
)
|
|
|
|
.context(MultipleAuthorsSnafu)
|
2024-05-01 16:21:07 +02:00
|
|
|
}
|
2024-05-06 09:09:40 +02:00
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Get the author to a book with id `id`.
|
2025-07-02 21:09:37 +02:00
|
|
|
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 \
|
2024-05-06 09:09:40 +02:00
|
|
|
INNER JOIN books_authors_link ON authors.id = books_authors_link.author \
|
|
|
|
WHERE books_authors_link.book = (:id)",
|
2025-07-02 21:09:37 +02:00
|
|
|
)
|
|
|
|
.context(PrepareBookAuthorSnafu)?;
|
2024-05-06 09:09:40 +02:00
|
|
|
let params = named_params! { ":id": id };
|
2025-07-02 21:09:37 +02:00
|
|
|
stmt.query_row(params, Self::from_row)
|
|
|
|
.context(ExecuteBookAuthorSnafu)
|
2024-05-06 09:09:40 +02:00
|
|
|
}
|
2024-05-06 13:51:49 +02:00
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Fetch a single author with id `id`.
|
2025-07-02 21:09:37 +02:00
|
|
|
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)?;
|
2024-05-06 14:17:25 +02:00
|
|
|
let params = named_params! { ":id": id };
|
2025-07-02 21:09:37 +02:00
|
|
|
stmt.query_row(params, Self::from_row)
|
|
|
|
.context(ExecuteScalarAuthorSnafu)
|
2024-05-06 14:17:25 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Check if there are more authors before the specified cursor.
|
2024-05-06 13:51:49 +02:00
|
|
|
pub fn has_previous_authors(
|
|
|
|
conn: &Connection,
|
|
|
|
sort_name: &str,
|
2025-07-02 21:09:37 +02:00
|
|
|
) -> Result<bool, PreviousAuthorsError> {
|
2024-05-06 16:25:15 +02:00
|
|
|
Pagination::has_prev_or_more(conn, "authors", sort_name, &SortOrder::DESC)
|
2025-07-02 21:09:37 +02:00
|
|
|
.context(PreviousAuthorsSnafu)
|
2024-05-06 13:51:49 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Check if there are more authors after the specified cursor.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub fn has_more_authors(conn: &Connection, sort_name: &str) -> Result<bool, MoreAuthorsError> {
|
2024-05-06 16:25:15 +02:00
|
|
|
Pagination::has_prev_or_more(conn, "authors", sort_name, &SortOrder::ASC)
|
2025-07-02 21:09:37 +02:00
|
|
|
.context(MoreAuthorsSnafu)
|
2024-05-06 13:51:49 +02:00
|
|
|
}
|
2024-05-01 16:21:07 +02:00
|
|
|
}
|