2024-05-10 14:25:18 +02:00
|
|
|
//! Book 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:47:56 +02:00
|
|
|
use snafu::{ResultExt, Snafu};
|
2024-05-09 11:18:47 +02:00
|
|
|
use time::OffsetDateTime;
|
2024-05-01 16:21:07 +02:00
|
|
|
|
2025-07-02 21:09:37 +02:00
|
|
|
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
2024-05-01 16:21:07 +02:00
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Book in calibre.
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
2024-05-01 16:21:07 +02:00
|
|
|
pub struct Book {
|
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
|
|
|
/// Book title.
|
2024-05-01 16:21:07 +02:00
|
|
|
pub title: String,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Book title for sorting.
|
2024-05-01 16:21:07 +02:00
|
|
|
pub sort: String,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Folder of the book within the calibre library.
|
2024-05-02 18:10:29 +02:00
|
|
|
pub path: String,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Uuid of the book.
|
2024-05-09 11:18:47 +02:00
|
|
|
pub uuid: String,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// When was the book last modified.
|
2024-05-09 11:18:47 +02:00
|
|
|
pub last_modified: OffsetDateTime,
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Optional description.
|
2024-05-09 11:18:47 +02:00
|
|
|
pub description: Option<String>,
|
2024-05-01 16:21:07 +02:00
|
|
|
}
|
|
|
|
|
2025-07-02 21:09:37 +02:00
|
|
|
#[derive(Debug, Snafu)]
|
|
|
|
#[snafu(display("Failed to fetch multiple books."))]
|
|
|
|
pub struct MultipleBooksError {
|
|
|
|
source: PaginationError,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Snafu)]
|
|
|
|
#[snafu(display("Failed to fetch author's books."))]
|
|
|
|
pub struct AuthorBooksError {
|
|
|
|
source: PaginationError,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Snafu)]
|
2025-07-02 21:57:44 +02:00
|
|
|
/// Errors that can occur when fetching a series' books.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub enum SeriesBookError {
|
2025-07-02 21:57:44 +02:00
|
|
|
/// A failure to prepare the SQL statement.
|
2025-07-02 21:09:37 +02:00
|
|
|
#[snafu(display("Failed to prepare statement."))]
|
|
|
|
PrepareSeriesBook { source: rusqlite::Error },
|
2025-07-02 21:57:44 +02:00
|
|
|
/// A failure to execute the SQL statement.
|
2025-07-02 21:09:37 +02:00
|
|
|
#[snafu(display("Failed to execute statement."))]
|
|
|
|
ExecuteSeriesBook { source: rusqlite::Error },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Snafu)]
|
2025-07-02 21:57:44 +02:00
|
|
|
/// Errors that can occur when fetching recent books.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub enum RecentBooksError {
|
2025-07-02 21:57:44 +02:00
|
|
|
/// A failure to prepare the SQL statement.
|
2025-07-02 21:09:37 +02:00
|
|
|
#[snafu(display("Failed to prepare statement."))]
|
|
|
|
PrepareRecentBooks { source: rusqlite::Error },
|
2025-07-02 21:57:44 +02:00
|
|
|
/// A failure to execute the SQL statement.
|
2025-07-02 21:09:37 +02:00
|
|
|
#[snafu(display("Failed to execute statement."))]
|
|
|
|
ExecuteRecentBooks { source: rusqlite::Error },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Snafu)]
|
2025-07-02 21:57:44 +02:00
|
|
|
/// Errors that can occur when fetching a single book.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub enum ScalarBookError {
|
2025-07-02 21:57:44 +02:00
|
|
|
/// A failure to prepare the SQL statement.
|
2025-07-02 21:09:37 +02:00
|
|
|
#[snafu(display("Failed to prepare statement."))]
|
|
|
|
PrepareScalarBook { source: rusqlite::Error },
|
2025-07-02 21:57:44 +02:00
|
|
|
/// A failure to execute the SQL statement.
|
2025-07-02 21:09:37 +02:00
|
|
|
#[snafu(display("Failed to execute statement."))]
|
|
|
|
ExecuteScalarBook { source: rusqlite::Error },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Snafu)]
|
|
|
|
#[snafu(display("Failed to check for previous books."))]
|
|
|
|
pub struct PreviousBooksError {
|
|
|
|
source: HasPrevOrMoreError,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Snafu)]
|
|
|
|
#[snafu(display("Failed to check for more books."))]
|
|
|
|
pub struct MoreBooksError {
|
|
|
|
source: HasPrevOrMoreError,
|
|
|
|
}
|
|
|
|
|
2024-05-01 16:21:07 +02:00
|
|
|
impl Book {
|
2025-07-02 21:57:44 +02:00
|
|
|
/// Create a book from a database row.
|
2024-05-01 16:21:07 +02:00
|
|
|
fn from_row(row: &Row<'_>) -> Result<Self, rusqlite::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
id: row.get(0)?,
|
|
|
|
title: row.get(1)?,
|
|
|
|
sort: row.get(2)?,
|
2024-05-02 18:10:29 +02:00
|
|
|
path: row.get(3)?,
|
2024-05-09 11:18:47 +02:00
|
|
|
uuid: row.get(4)?,
|
|
|
|
last_modified: row.get(5)?,
|
|
|
|
description: row.get(6)?,
|
2024-05-01 16:21:07 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Fetch book 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 14:42:14 +02:00
|
|
|
sort_order: &SortOrder,
|
2025-07-02 21:09:37 +02:00
|
|
|
) -> Result<Vec<Self>, MultipleBooksError> {
|
2024-05-06 14:42:14 +02:00
|
|
|
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
|
2024-05-01 16:21:07 +02:00
|
|
|
pagination.paginate(
|
|
|
|
conn,
|
2024-05-09 11:18:47 +02:00
|
|
|
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text \
|
|
|
|
FROM books LEFT JOIN comments ON books.id = comments.book",
|
2024-05-01 16:21:07 +02:00
|
|
|
&[],
|
|
|
|
Self::from_row,
|
2025-07-02 21:09:37 +02:00
|
|
|
).context(MultipleBooksSnafu)
|
2024-05-01 16:21:07 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Fetch books for an author specified by `author_id`, paginate the books by starting at `cursor`,
|
|
|
|
/// fetching up to an amount of `limit` and ordering by `sort_order`.
|
2024-05-01 16:21:07 +02:00
|
|
|
pub fn author_books(
|
|
|
|
conn: &Connection,
|
|
|
|
author_id: u64,
|
|
|
|
limit: u64,
|
|
|
|
cursor: Option<&str>,
|
|
|
|
sort_order: SortOrder,
|
2025-07-02 21:09:37 +02:00
|
|
|
) -> Result<Vec<Self>, AuthorBooksError> {
|
2024-05-01 16:21:07 +02:00
|
|
|
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
|
|
|
|
pagination.paginate(
|
|
|
|
conn,
|
2024-05-09 11:18:47 +02:00
|
|
|
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text FROM books \
|
2024-05-01 16:21:07 +02:00
|
|
|
INNER JOIN books_authors_link ON books.id = books_authors_link.book \
|
2024-05-09 11:18:47 +02:00
|
|
|
LEFT JOIN comments ON books.id = comments.book \
|
2024-05-01 16:21:07 +02:00
|
|
|
WHERE books_authors_link.author = (:author_id) AND",
|
|
|
|
&[(":author_id", &author_id)],
|
|
|
|
Self::from_row,
|
2025-07-02 21:09:37 +02:00
|
|
|
).context(AuthorBooksSnafu)
|
2024-05-01 16:21:07 +02:00
|
|
|
}
|
2024-05-02 18:10:29 +02:00
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Get all books belonging to the series with id `id`.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub fn series_books(conn: &Connection, id: u64) -> Result<Vec<Book>, SeriesBookError> {
|
2024-05-06 16:25:15 +02:00
|
|
|
let mut stmt = conn.prepare(
|
2024-05-09 11:18:47 +02:00
|
|
|
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text FROM series \
|
2024-05-06 16:25:15 +02:00
|
|
|
INNER JOIN books_series_link ON series.id = books_series_link.series \
|
|
|
|
INNER JOIN books ON books.id = books_series_link.book \
|
2024-05-09 11:18:47 +02:00
|
|
|
LEFT JOIN comments ON books.id = comments.book \
|
2024-05-06 16:25:15 +02:00
|
|
|
WHERE books_series_link.series = (:id) \
|
|
|
|
ORDER BY books.series_index",
|
2025-07-02 21:09:37 +02:00
|
|
|
).context(PrepareSeriesBookSnafu)?;
|
2024-05-06 16:25:15 +02:00
|
|
|
let params = named_params! { ":id": id };
|
2025-07-02 21:09:37 +02:00
|
|
|
let iter = stmt
|
|
|
|
.query_map(params, Self::from_row)
|
|
|
|
.context(ExecuteSeriesBookSnafu)?;
|
2024-05-06 16:25:15 +02:00
|
|
|
Ok(iter.filter_map(Result::ok).collect())
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Get recent books up to a limit of `limit`.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub fn recents(conn: &Connection, limit: u64) -> Result<Vec<Self>, RecentBooksError> {
|
2024-05-02 18:10:29 +02:00
|
|
|
let mut stmt = conn.prepare(
|
2024-05-09 11:18:47 +02:00
|
|
|
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text \
|
|
|
|
FROM books LEFT JOIN comments ON books.id = comments.book ORDER BY books.timestamp DESC LIMIT (:limit)"
|
2025-07-02 21:09:37 +02:00
|
|
|
).context(PrepareRecentBooksSnafu)?;
|
2024-05-02 18:10:29 +02:00
|
|
|
let params = named_params! { ":limit": limit };
|
2025-07-02 21:09:37 +02:00
|
|
|
let iter = stmt
|
|
|
|
.query_map(params, Self::from_row)
|
|
|
|
.context(ExecuteRecentBooksSnafu)?;
|
2024-05-02 18:10:29 +02:00
|
|
|
Ok(iter.filter_map(Result::ok).collect())
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Get a single book, specified `id`.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub fn scalar_book(conn: &Connection, id: u64) -> Result<Self, ScalarBookError> {
|
2024-05-09 11:18:47 +02:00
|
|
|
let mut stmt = conn.prepare(
|
|
|
|
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text \
|
|
|
|
FROM books LEFT JOIN comments WHERE books.id = (:id)",
|
2025-07-02 21:09:37 +02:00
|
|
|
).context(PrepareScalarBookSnafu)?;
|
2024-05-02 18:10:29 +02:00
|
|
|
let params = named_params! { ":id": id };
|
2025-07-02 21:09:37 +02:00
|
|
|
stmt.query_row(params, Self::from_row)
|
|
|
|
.context(ExecuteScalarBookSnafu)
|
2024-05-02 18:10:29 +02:00
|
|
|
}
|
2024-05-06 14:42:14 +02:00
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Check if there are more books before the specified cursor.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub fn has_previous_books(
|
|
|
|
conn: &Connection,
|
|
|
|
sort_title: &str,
|
|
|
|
) -> Result<bool, PreviousBooksError> {
|
2024-05-06 16:25:15 +02:00
|
|
|
Pagination::has_prev_or_more(conn, "books", sort_title, &SortOrder::DESC)
|
2025-07-02 21:09:37 +02:00
|
|
|
.context(PreviousBooksSnafu)
|
2024-05-06 14:42:14 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 14:25:18 +02:00
|
|
|
/// Check if there are more books after the specified cursor.
|
2025-07-02 21:09:37 +02:00
|
|
|
pub fn has_more_books(conn: &Connection, sort_title: &str) -> Result<bool, MoreBooksError> {
|
2024-05-06 16:25:15 +02:00
|
|
|
Pagination::has_prev_or_more(conn, "books", sort_title, &SortOrder::ASC)
|
2025-07-02 21:09:37 +02:00
|
|
|
.context(MoreBooksSnafu)
|
2024-05-06 14:42:14 +02:00
|
|
|
}
|
2024-05-01 16:21:07 +02:00
|
|
|
}
|