basic recents view
This commit is contained in:
parent
65e17fc55b
commit
687c33829f
20 changed files with 2156 additions and 20 deletions
|
@ -4,5 +4,8 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
r2d2 = "0.8.10"
|
||||
r2d2_sqlite = "0.24.0"
|
||||
rusqlite = { version = "0.31.0", features = ["bundled"] }
|
||||
thiserror = "1.0.59"
|
||||
serde = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
use rusqlite::Connection;
|
||||
use std::path::Path;
|
||||
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
use crate::data::{author::Author, book::Book, error::DataStoreError, pagination::SortOrder};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Calibre {
|
||||
conn: Connection,
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
impl Calibre {
|
||||
pub fn load(url: &str) -> Result<Self, DataStoreError> {
|
||||
let conn = Connection::open(url)?;
|
||||
Ok(Self { conn })
|
||||
pub fn load(path: &Path) -> Result<Self, DataStoreError> {
|
||||
let manager = SqliteConnectionManager::file(path);
|
||||
let pool = r2d2::Pool::new(manager)?;
|
||||
|
||||
Ok(Self { pool })
|
||||
}
|
||||
|
||||
pub fn books(
|
||||
|
@ -18,7 +24,8 @@ impl Calibre {
|
|||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
Book::books(&self.conn, limit, cursor, sort_order)
|
||||
let conn = self.pool.get()?;
|
||||
Book::books(&conn, limit, cursor, sort_order)
|
||||
}
|
||||
|
||||
pub fn authors(
|
||||
|
@ -27,7 +34,8 @@ impl Calibre {
|
|||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Author>, DataStoreError> {
|
||||
Author::authors(&self.conn, limit, cursor, sort_order)
|
||||
let conn = self.pool.get()?;
|
||||
Author::authors(&conn, limit, cursor, sort_order)
|
||||
}
|
||||
|
||||
pub fn author_books(
|
||||
|
@ -37,7 +45,18 @@ impl Calibre {
|
|||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
Book::author_books(&self.conn, author_id, limit, cursor, sort_order)
|
||||
let conn = self.pool.get()?;
|
||||
Book::author_books(&conn, author_id, limit, cursor, sort_order)
|
||||
}
|
||||
|
||||
pub fn recent_books(&self, limit: u64) -> Result<Vec<Book>, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Book::recents(&conn, limit)
|
||||
}
|
||||
|
||||
pub fn scalar_book(&self, id: u64) -> Result<Book, DataStoreError> {
|
||||
let conn = self.pool.get()?;
|
||||
Book::scalar_book(&conn, id)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,30 +64,34 @@ impl Calibre {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn init_calibre() -> Calibre {
|
||||
Calibre::load(Path::new("./testdata/metadata.db")).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn books() {
|
||||
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||||
let c = init_calibre();
|
||||
let books = c.books(10, None, SortOrder::ASC).unwrap();
|
||||
assert_eq!(books.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authors() {
|
||||
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||||
let c = init_calibre();
|
||||
let authors = c.authors(10, None, SortOrder::ASC).unwrap();
|
||||
assert_eq!(authors.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn author_books() {
|
||||
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||||
let c = init_calibre();
|
||||
let books = c.author_books(1, 10, None, SortOrder::ASC).unwrap();
|
||||
assert_eq!(books.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pagination() {
|
||||
let c = Calibre::load("./testdata/metadata.db").unwrap();
|
||||
let c = init_calibre();
|
||||
let authors = c.authors(1, None, SortOrder::ASC).unwrap();
|
||||
assert_eq!(authors.len(), 1);
|
||||
assert_eq!(authors[0].name, "Kevin R. Grazier");
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
use rusqlite::{Connection, Row};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Author {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
use rusqlite::{Connection, Row};
|
||||
use rusqlite::{named_params, Connection, Row};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{
|
||||
error::DataStoreError,
|
||||
pagination::{Pagination, SortOrder},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Book {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub sort: String,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
impl Book {
|
||||
|
@ -18,6 +20,7 @@ impl Book {
|
|||
id: row.get(0)?,
|
||||
title: row.get(1)?,
|
||||
sort: row.get(2)?,
|
||||
path: row.get(3)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -30,7 +33,7 @@ impl Book {
|
|||
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT id, title, sort FROM books",
|
||||
"SELECT id, title, sort, path FROM books",
|
||||
&[],
|
||||
Self::from_row,
|
||||
)
|
||||
|
@ -46,11 +49,26 @@ impl Book {
|
|||
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
|
||||
pagination.paginate(
|
||||
conn,
|
||||
"SELECT books.id, books.title, books.sort FROM books \
|
||||
"SELECT books.id, books.title, books.sort, books.path FROM books \
|
||||
INNER JOIN books_authors_link ON books.id = books_authors_link.book \
|
||||
WHERE books_authors_link.author = (:author_id) AND",
|
||||
&[(":author_id", &author_id)],
|
||||
Self::from_row,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn recents(conn: &Connection, limit: u64) -> Result<Vec<Book>, DataStoreError> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT id, title, sort, path FROM books ORDER BY timestamp DESC LIMIT (:limit)",
|
||||
)?;
|
||||
let params = named_params! { ":limit": limit };
|
||||
let iter = stmt.query_map(params, Self::from_row)?;
|
||||
Ok(iter.filter_map(Result::ok).collect())
|
||||
}
|
||||
|
||||
pub fn scalar_book(conn: &Connection, id: u64) -> Result<Book, 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)?)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,4 +4,6 @@ use thiserror::Error;
|
|||
pub enum DataStoreError {
|
||||
#[error("sqlite error")]
|
||||
SqliteError(#[from] rusqlite::Error),
|
||||
#[error("connection error")]
|
||||
ConnectionError(#[from] r2d2::Error),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue