134 lines
4.3 KiB
Rust
134 lines
4.3 KiB
Rust
//! Series data.
|
|
|
|
use rusqlite::{Connection, Row, named_params};
|
|
use serde::Serialize;
|
|
|
|
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
|
use snafu::{ResultExt, Snafu};
|
|
|
|
/// Series in calibre.
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct Series {
|
|
/// Id in database.
|
|
pub id: u64,
|
|
/// Series name.
|
|
pub name: String,
|
|
/// Series name for sorting.
|
|
pub sort: String,
|
|
}
|
|
|
|
#[derive(Debug, Snafu)]
|
|
#[snafu(display("Failed to fetch multiple series."))]
|
|
pub struct MultiplSeriesError {
|
|
source: PaginationError,
|
|
}
|
|
|
|
#[derive(Debug, Snafu)]
|
|
pub enum SeriesBooksError {
|
|
#[snafu(display("Failed to prepare statement."))]
|
|
PrepareSeriesBooks { source: rusqlite::Error },
|
|
#[snafu(display("Failed to execute statement."))]
|
|
ExecuteSeriesBooks { source: rusqlite::Error },
|
|
}
|
|
|
|
#[derive(Debug, Snafu)]
|
|
pub enum ScalarSeriesError {
|
|
#[snafu(display("Failed to prepare statement."))]
|
|
PrepareScalarSeries { source: rusqlite::Error },
|
|
#[snafu(display("Failed to execute statement."))]
|
|
ExecuteScalarSeries { source: rusqlite::Error },
|
|
}
|
|
|
|
#[derive(Debug, Snafu)]
|
|
#[snafu(display("Failed to check for previous series."))]
|
|
pub struct PreviousSeriesError {
|
|
source: HasPrevOrMoreError,
|
|
}
|
|
|
|
#[derive(Debug, Snafu)]
|
|
#[snafu(display("Failed to check for more series."))]
|
|
pub struct MoreSeriesError {
|
|
source: HasPrevOrMoreError,
|
|
}
|
|
|
|
impl Series {
|
|
fn from_row(row: &Row<'_>) -> Result<Self, rusqlite::Error> {
|
|
Ok(Self {
|
|
id: row.get(0)?,
|
|
name: row.get(1)?,
|
|
sort: row.get(2)?,
|
|
})
|
|
}
|
|
|
|
/// Fetch series 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<Vec<Self>, MultiplSeriesError> {
|
|
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
|
|
pagination
|
|
.paginate(
|
|
conn,
|
|
"SELECT id, name, sort FROM series",
|
|
&[],
|
|
Self::from_row,
|
|
)
|
|
.context(MultiplSeriesSnafu)
|
|
}
|
|
|
|
/// Fetch a single series with id `id`.
|
|
pub fn scalar_series(conn: &Connection, id: u64) -> Result<Self, ScalarSeriesError> {
|
|
let mut stmt = conn
|
|
.prepare("SELECT id, name, sort FROM series WHERE id = (:id)")
|
|
.context(PrepareScalarSeriesSnafu)?;
|
|
let params = named_params! { ":id": id };
|
|
stmt.query_row(params, Self::from_row)
|
|
.context(ExecuteScalarSeriesSnafu)
|
|
}
|
|
|
|
/// Get the series a book with id `id` is in, as well as the book's position within the series.
|
|
pub fn book_series(
|
|
conn: &Connection,
|
|
book_id: u64,
|
|
) -> Result<Option<(Self, f64)>, SeriesBooksError> {
|
|
let mut stmt = conn
|
|
.prepare(
|
|
"SELECT series.id, series.name, series.sort, books.series_index FROM series \
|
|
INNER JOIN books_series_link ON series.id = books_series_link.series \
|
|
INNER JOIN books ON books.id = books_series_link.book \
|
|
WHERE books_series_link.book = (:id)",
|
|
)
|
|
.context(PrepareSeriesBooksSnafu)?;
|
|
let params = named_params! { ":id": book_id };
|
|
|
|
let from_row = |row: &Row<'_>| {
|
|
let series = Self::from_row(row)?;
|
|
let series_idx = row.get(3)?;
|
|
Ok((series, series_idx))
|
|
};
|
|
|
|
match stmt.query_row(params, from_row) {
|
|
Ok(series) => Ok(Some(series)),
|
|
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
|
|
Err(e) => Err(e).context(ExecuteSeriesBooksSnafu),
|
|
}
|
|
}
|
|
|
|
/// Check if there are more series before the specified cursor.
|
|
pub fn has_previous_series(
|
|
conn: &Connection,
|
|
sort_name: &str,
|
|
) -> Result<bool, PreviousSeriesError> {
|
|
Pagination::has_prev_or_more(conn, "series", sort_name, &SortOrder::DESC)
|
|
.context(PreviousSeriesSnafu)
|
|
}
|
|
|
|
/// Check if there are more series after the specified cursor.
|
|
pub fn has_more_series(conn: &Connection, sort_name: &str) -> Result<bool, MoreSeriesError> {
|
|
Pagination::has_prev_or_more(conn, "series", sort_name, &SortOrder::ASC)
|
|
.context(MoreSeriesSnafu)
|
|
}
|
|
}
|