WIP
This commit is contained in:
parent
1c95f4391f
commit
b4a0aadef9
73 changed files with 2993 additions and 1632 deletions
|
@ -1,12 +1,10 @@
|
|||
//! Series data.
|
||||
|
||||
use rusqlite::{named_params, Connection, Row};
|
||||
use rusqlite::{Connection, Row, named_params};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
/// Series in calibre.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
@ -19,6 +17,40 @@ pub struct Series {
|
|||
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 {
|
||||
|
@ -35,34 +67,41 @@ impl Series {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
) -> 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,
|
||||
)
|
||||
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, DataStoreError> {
|
||||
let mut stmt = conn.prepare("SELECT id, name, sort FROM series WHERE 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 };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
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)>, DataStoreError> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT series.id, series.name, series.sort, books.series_index FROM series \
|
||||
) -> 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<'_>| {
|
||||
|
@ -74,17 +113,22 @@ impl Series {
|
|||
match stmt.query_row(params, from_row) {
|
||||
Ok(series) => Ok(Some(series)),
|
||||
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
|
||||
Err(e) => Err(DataStoreError::SqliteError(e)),
|
||||
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, DataStoreError> {
|
||||
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, DataStoreError> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue