proper error handling
This commit is contained in:
parent
687c33829f
commit
ac7b0e7e88
15 changed files with 245 additions and 25 deletions
|
@ -3,7 +3,9 @@ use std::path::Path;
|
|||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
use crate::data::{author::Author, book::Book, error::DataStoreError, pagination::SortOrder};
|
||||
use crate::data::{
|
||||
author::Author, book::Book, error::DataStoreError, pagination::SortOrder, series::Series,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Calibre {
|
||||
|
@ -25,7 +27,7 @@ impl Calibre {
|
|||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Book::books(&conn, limit, cursor, sort_order)
|
||||
Book::multiple(&conn, limit, cursor, sort_order)
|
||||
}
|
||||
|
||||
pub fn authors(
|
||||
|
@ -35,7 +37,7 @@ impl Calibre {
|
|||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Author>, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Author::authors(&conn, limit, cursor, sort_order)
|
||||
Author::multiple(&conn, limit, cursor, sort_order)
|
||||
}
|
||||
|
||||
pub fn author_books(
|
||||
|
@ -58,6 +60,26 @@ impl Calibre {
|
|||
let conn = self.pool.get()?;
|
||||
Book::scalar_book(&conn, id)
|
||||
}
|
||||
|
||||
pub fn book_author(&self, id: u64) -> Result<Author, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Author::book_author(&conn, id)
|
||||
}
|
||||
|
||||
pub fn series(
|
||||
&self,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Series>, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Series::multiple(&conn, limit, cursor, sort_order)
|
||||
}
|
||||
|
||||
pub fn book_series(&self, id: u64) -> Result<Option<(Series, f64)>, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Series::book_series(&conn, id)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use rusqlite::{Connection, Row};
|
||||
use rusqlite::{named_params, Connection, Row};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
|
@ -8,7 +8,7 @@ use super::{
|
|||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Author {
|
||||
pub id: i32,
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
pub sort: String,
|
||||
}
|
||||
|
@ -22,12 +22,12 @@ impl Author {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn authors(
|
||||
pub fn multiple(
|
||||
conn: &Connection,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Author>, DataStoreError> {
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
|
@ -36,4 +36,14 @@ impl Author {
|
|||
Self::from_row,
|
||||
)
|
||||
}
|
||||
|
||||
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 \
|
||||
INNER JOIN books_authors_link ON authors.id = books_authors_link.author \
|
||||
WHERE books_authors_link.book = (:id)",
|
||||
)?;
|
||||
let params = named_params! { ":id": id };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use super::{
|
|||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Book {
|
||||
pub id: i32,
|
||||
pub id: u64,
|
||||
pub title: String,
|
||||
pub sort: String,
|
||||
pub path: String,
|
||||
|
@ -24,12 +24,12 @@ impl Book {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn books(
|
||||
pub fn multiple(
|
||||
conn: &Connection,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
|
@ -45,7 +45,7 @@ impl Book {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
|
@ -57,7 +57,7 @@ impl Book {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn recents(conn: &Connection, limit: u64) -> Result<Vec<Book>, DataStoreError> {
|
||||
pub fn recents(conn: &Connection, limit: u64) -> Result<Vec<Self>, DataStoreError> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT id, title, sort, path FROM books ORDER BY timestamp DESC LIMIT (:limit)",
|
||||
)?;
|
||||
|
@ -66,7 +66,7 @@ impl Book {
|
|||
Ok(iter.filter_map(Result::ok).collect())
|
||||
}
|
||||
|
||||
pub fn scalar_book(conn: &Connection, id: u64) -> Result<Book, DataStoreError> {
|
||||
pub fn scalar_book(conn: &Connection, id: u64) -> Result<Self, DataStoreError> {
|
||||
let mut stmt = conn.prepare("SELECT id, title, sort, path FROM books WHERE id = (:id)")?;
|
||||
let params = named_params! { ":id": id };
|
||||
Ok(stmt.query_row(params, Self::from_row)?)
|
||||
|
|
|
@ -1,9 +1,21 @@
|
|||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("data store error")]
|
||||
pub enum DataStoreError {
|
||||
#[error("no results")]
|
||||
NoResults(rusqlite::Error),
|
||||
#[error("sqlite error")]
|
||||
SqliteError(#[from] rusqlite::Error),
|
||||
SqliteError(rusqlite::Error),
|
||||
#[error("connection error")]
|
||||
ConnectionError(#[from] r2d2::Error),
|
||||
}
|
||||
|
||||
impl From<rusqlite::Error> for DataStoreError {
|
||||
fn from(error: rusqlite::Error) -> Self {
|
||||
match error {
|
||||
rusqlite::Error::QueryReturnedNoRows => DataStoreError::NoResults(error),
|
||||
_ => DataStoreError::SqliteError(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
64
calibre-db/src/data/series.rs
Normal file
64
calibre-db/src/data/series.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use rusqlite::{named_params, Connection, Row};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Series {
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
pub sort: String,
|
||||
}
|
||||
|
||||
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)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn multiple(
|
||||
conn: &Connection,
|
||||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Self>, DataStoreError> {
|
||||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT id, title, sort, path FROM series",
|
||||
&[],
|
||||
Self::from_row,
|
||||
)
|
||||
}
|
||||
|
||||
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 \
|
||||
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)",
|
||||
)?;
|
||||
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(DataStoreError::SqliteError(e)),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,4 +4,5 @@ pub mod data {
|
|||
pub mod book;
|
||||
pub mod error;
|
||||
pub mod pagination;
|
||||
pub mod series;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue