book feed

This commit is contained in:
Sebastian Hugentobler 2024-05-09 11:18:47 +02:00
parent 603c2fbe48
commit 12341d01a6
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
15 changed files with 219 additions and 41 deletions

View file

@ -7,6 +7,7 @@ license = { workspace = true }
[dependencies]
r2d2 = "0.8.10"
r2d2_sqlite = "0.24.0"
rusqlite = { version = "0.31.0", features = ["bundled"] }
rusqlite = { version = "0.31.0", features = ["bundled", "time"] }
serde = { workspace = true }
thiserror = { workspace = true }
time = { workspace = true }

View file

@ -1,5 +1,6 @@
use rusqlite::{named_params, Connection, Row};
use serde::Serialize;
use time::OffsetDateTime;
use super::{
error::DataStoreError,
@ -12,6 +13,9 @@ pub struct Book {
pub title: String,
pub sort: String,
pub path: String,
pub uuid: String,
pub last_modified: OffsetDateTime,
pub description: Option<String>,
}
impl Book {
@ -21,6 +25,9 @@ impl Book {
title: row.get(1)?,
sort: row.get(2)?,
path: row.get(3)?,
uuid: row.get(4)?,
last_modified: row.get(5)?,
description: row.get(6)?,
})
}
@ -33,7 +40,8 @@ impl Book {
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
pagination.paginate(
conn,
"SELECT id, title, sort, path FROM books",
"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",
&[],
Self::from_row,
)
@ -49,8 +57,9 @@ impl Book {
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
pagination.paginate(
conn,
"SELECT books.id, books.title, books.sort, books.path FROM books \
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text FROM books \
INNER JOIN books_authors_link ON books.id = books_authors_link.book \
LEFT JOIN comments ON books.id = comments.book \
WHERE books_authors_link.author = (:author_id) AND",
&[(":author_id", &author_id)],
Self::from_row,
@ -59,9 +68,10 @@ impl Book {
pub fn series_books(conn: &Connection, id: u64) -> Result<Vec<Book>, DataStoreError> {
let mut stmt = conn.prepare(
"SELECT books.id, books.title, books.sort, books.path FROM series \
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text FROM series \
INNER JOIN books_series_link ON series.id = books_series_link.series \
INNER JOIN books ON books.id = books_series_link.book \
LEFT JOIN comments ON books.id = comments.book \
WHERE books_series_link.series = (:id) \
ORDER BY books.series_index",
)?;
@ -72,7 +82,8 @@ impl Book {
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)",
"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)"
)?;
let params = named_params! { ":limit": limit };
let iter = stmt.query_map(params, Self::from_row)?;
@ -80,7 +91,10 @@ impl Book {
}
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 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)",
)?;
let params = named_params! { ":id": id };
Ok(stmt.query_row(params, Self::from_row)?)
}

View file

@ -1,4 +1,5 @@
use thiserror::Error;
use time::error::Parse;
#[derive(Error, Debug)]
#[error("data store error")]
@ -9,6 +10,8 @@ pub enum DataStoreError {
SqliteError(rusqlite::Error),
#[error("connection error")]
ConnectionError(#[from] r2d2::Error),
#[error("failed to parse datetime")]
DateTimeError(#[from] Parse),
}
impl From<rusqlite::Error> for DataStoreError {

View file

@ -6,7 +6,7 @@ use super::{
pagination::{Pagination, SortOrder},
};
#[derive(Debug, Serialize)]
#[derive(Debug, Clone, Serialize)]
pub struct Series {
pub id: u64,
pub name: String,