little-hesinde/calibre-db/src/data/book.rs

110 lines
4.1 KiB
Rust
Raw Normal View History

2024-05-02 18:10:29 +02:00
use rusqlite::{named_params, Connection, Row};
use serde::Serialize;
2024-05-09 11:18:47 +02:00
use time::OffsetDateTime;
2024-05-01 16:21:07 +02:00
use super::{
error::DataStoreError,
pagination::{Pagination, SortOrder},
};
2024-05-02 18:10:29 +02:00
#[derive(Debug, Serialize)]
2024-05-01 16:21:07 +02:00
pub struct Book {
2024-05-06 09:09:40 +02:00
pub id: u64,
2024-05-01 16:21:07 +02:00
pub title: String,
pub sort: String,
2024-05-02 18:10:29 +02:00
pub path: String,
2024-05-09 11:18:47 +02:00
pub uuid: String,
pub last_modified: OffsetDateTime,
pub description: Option<String>,
2024-05-01 16:21:07 +02:00
}
impl Book {
fn from_row(row: &Row<'_>) -> Result<Self, rusqlite::Error> {
Ok(Self {
id: row.get(0)?,
title: row.get(1)?,
sort: row.get(2)?,
2024-05-02 18:10:29 +02:00
path: row.get(3)?,
2024-05-09 11:18:47 +02:00
uuid: row.get(4)?,
last_modified: row.get(5)?,
description: row.get(6)?,
2024-05-01 16:21:07 +02:00
})
}
2024-05-06 09:09:40 +02:00
pub fn multiple(
2024-05-01 16:21:07 +02:00
conn: &Connection,
limit: u64,
cursor: Option<&str>,
2024-05-06 14:42:14 +02:00
sort_order: &SortOrder,
2024-05-06 09:09:40 +02:00
) -> Result<Vec<Self>, DataStoreError> {
2024-05-06 14:42:14 +02:00
let pagination = Pagination::new("sort", cursor, limit, *sort_order);
2024-05-01 16:21:07 +02:00
pagination.paginate(
conn,
2024-05-09 11:18:47 +02:00
"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",
2024-05-01 16:21:07 +02:00
&[],
Self::from_row,
)
}
pub fn author_books(
conn: &Connection,
author_id: u64,
limit: u64,
cursor: Option<&str>,
sort_order: SortOrder,
2024-05-06 09:09:40 +02:00
) -> Result<Vec<Self>, DataStoreError> {
2024-05-01 16:21:07 +02:00
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
pagination.paginate(
conn,
2024-05-09 11:18:47 +02:00
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text FROM books \
2024-05-01 16:21:07 +02:00
INNER JOIN books_authors_link ON books.id = books_authors_link.book \
2024-05-09 11:18:47 +02:00
LEFT JOIN comments ON books.id = comments.book \
2024-05-01 16:21:07 +02:00
WHERE books_authors_link.author = (:author_id) AND",
&[(":author_id", &author_id)],
Self::from_row,
)
}
2024-05-02 18:10:29 +02:00
2024-05-06 16:25:15 +02:00
pub fn series_books(conn: &Connection, id: u64) -> Result<Vec<Book>, DataStoreError> {
let mut stmt = conn.prepare(
2024-05-09 11:18:47 +02:00
"SELECT books.id, books.title, books.sort, books.path, books.uuid, books.last_modified, comments.text FROM series \
2024-05-06 16:25:15 +02:00
INNER JOIN books_series_link ON series.id = books_series_link.series \
INNER JOIN books ON books.id = books_series_link.book \
2024-05-09 11:18:47 +02:00
LEFT JOIN comments ON books.id = comments.book \
2024-05-06 16:25:15 +02:00
WHERE books_series_link.series = (:id) \
ORDER BY books.series_index",
)?;
let params = named_params! { ":id": id };
let iter = stmt.query_map(params, Self::from_row)?;
Ok(iter.filter_map(Result::ok).collect())
}
2024-05-06 09:09:40 +02:00
pub fn recents(conn: &Connection, limit: u64) -> Result<Vec<Self>, DataStoreError> {
2024-05-02 18:10:29 +02:00
let mut stmt = conn.prepare(
2024-05-09 11:18:47 +02:00
"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)"
2024-05-02 18:10:29 +02:00
)?;
let params = named_params! { ":limit": limit };
let iter = stmt.query_map(params, Self::from_row)?;
Ok(iter.filter_map(Result::ok).collect())
}
2024-05-06 09:09:40 +02:00
pub fn scalar_book(conn: &Connection, id: u64) -> Result<Self, DataStoreError> {
2024-05-09 11:18:47 +02:00
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)",
)?;
2024-05-02 18:10:29 +02:00
let params = named_params! { ":id": id };
Ok(stmt.query_row(params, Self::from_row)?)
}
2024-05-06 14:42:14 +02:00
pub fn has_previous_books(conn: &Connection, sort_title: &str) -> Result<bool, DataStoreError> {
2024-05-06 16:25:15 +02:00
Pagination::has_prev_or_more(conn, "books", sort_title, &SortOrder::DESC)
2024-05-06 14:42:14 +02:00
}
pub fn has_more_books(conn: &Connection, sort_title: &str) -> Result<bool, DataStoreError> {
2024-05-06 16:25:15 +02:00
Pagination::has_prev_or_more(conn, "books", sort_title, &SortOrder::ASC)
2024-05-06 14:42:14 +02:00
}
2024-05-01 16:21:07 +02:00
}