57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
|
use rusqlite::{Connection, Row};
|
||
|
|
||
|
use super::{
|
||
|
error::DataStoreError,
|
||
|
pagination::{Pagination, SortOrder},
|
||
|
};
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct Book {
|
||
|
pub id: i32,
|
||
|
pub title: String,
|
||
|
pub sort: String,
|
||
|
}
|
||
|
|
||
|
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)?,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
pub fn books(
|
||
|
conn: &Connection,
|
||
|
limit: u64,
|
||
|
cursor: Option<&str>,
|
||
|
sort_order: SortOrder,
|
||
|
) -> Result<Vec<Book>, DataStoreError> {
|
||
|
let pagination = Pagination::new("sort", cursor, limit, sort_order);
|
||
|
pagination.paginate(
|
||
|
conn,
|
||
|
"SELECT id, title, sort FROM books",
|
||
|
&[],
|
||
|
Self::from_row,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
pub fn author_books(
|
||
|
conn: &Connection,
|
||
|
author_id: u64,
|
||
|
limit: u64,
|
||
|
cursor: Option<&str>,
|
||
|
sort_order: SortOrder,
|
||
|
) -> Result<Vec<Book>, DataStoreError> {
|
||
|
let pagination = Pagination::new("books.sort", cursor, limit, sort_order);
|
||
|
pagination.paginate(
|
||
|
conn,
|
||
|
"SELECT books.id, books.title, books.sort 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,
|
||
|
)
|
||
|
}
|
||
|
}
|