paginate books

This commit is contained in:
Sebastian Hugentobler 2024-05-06 14:42:14 +02:00
parent 6d949bb21e
commit 6a79f0c1ed
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
10 changed files with 99 additions and 13 deletions

View file

@ -33,7 +33,7 @@ pub async fn handler(
context.insert("books", &books);
TEMPLATES
.render("books", &context)
.render("book_list", &context)
.map_err(InternalServerError)
.map(Html)
}

View file

@ -1,10 +1,44 @@
use std::sync::Arc;
use poem::{handler, web::Data};
use calibre_db::data::pagination::SortOrder;
use poem::{
handler,
web::{Data, Html, Path},
};
use crate::app_state::AppState;
use crate::{app_state::AppState, data::book::Book};
use super::paginated;
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<String, poem::Error> {
Ok("books".to_string())
pub async fn handler_init(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::Error> {
books(&state, None, &SortOrder::ASC)
}
#[handler]
pub async fn handler(
Path((cursor, sort_order)): Path<(String, SortOrder)>,
state: Data<&Arc<AppState>>,
) -> Result<Html<String>, poem::Error> {
books(&state, Some(&cursor), &sort_order)
}
fn books(
state: &Arc<AppState>,
cursor: Option<&str>,
sort_order: &SortOrder,
) -> Result<Html<String>, poem::Error> {
paginated::render(
"books",
|| {
state.calibre.books(25, cursor, sort_order).map(|x| {
x.iter()
.filter_map(|y| Book::full_book(y, &state))
.collect()
})
},
|book| book.sort.clone(),
|cursor| state.calibre.has_previous_books(cursor),
|cursor| state.calibre.has_more_books(cursor),
)
}

View file

@ -1,3 +1,5 @@
use std::fmt::Debug;
use calibre_db::data::error::DataStoreError;
use poem::{error::InternalServerError, web::Html};
use serde::Serialize;
@ -7,7 +9,7 @@ use crate::templates::TEMPLATES;
use super::error::SqliteError;
pub fn render<T: Serialize, F, S, P, M>(
pub fn render<T: Serialize + Debug, F, S, P, M>(
template: &str,
fetcher: F,
sort_field: S,
@ -39,6 +41,7 @@ where
context.insert("forward_cursor", &forward_cursor);
context.insert("nav", template);
context.insert(template, &items);
TEMPLATES
.render(template, &context)
.map_err(InternalServerError)

View file

@ -24,7 +24,7 @@ pub async fn handler(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::
context.insert("nav", "recent");
context.insert("books", &recent_books);
TEMPLATES
.render("books", &context)
.render("book_list", &context)
.map_err(InternalServerError)
.map(Html)
}

View file

@ -49,7 +49,8 @@ async fn main() -> Result<(), std::io::Error> {
let app = Route::new()
.at("/", get(handlers::recents::handler))
.at("/books", get(handlers::books::handler))
.at("/books", get(handlers::books::handler_init))
.at("/books/:cursor/:sort_order", get(handlers::books::handler))
.at("/authors", get(handlers::authors::handler_init))
.at("/authors/:id", get(handlers::author::handler))
.at(

View file

@ -7,6 +7,7 @@ pub static TEMPLATES: Lazy<Tera> = Lazy::new(|| {
("base", include_str!("../templates/base.html")),
("book_card", include_str!("../templates/book_card.html")),
("authors", include_str!("../templates/authors.html")),
("book_list", include_str!("../templates/book_list.html")),
("books", include_str!("../templates/books.html")),
])
.expect("failed to parse tera templates");