do not crash if there are no items to paginate
All checks were successful
Build Multiarch Container Image / call-reusable-workflow (push) Successful in 19m41s

This commit is contained in:
Sebastian Hugentobler 2024-06-26 08:45:22 +02:00
parent c9e7566aee
commit ed8b69de13
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
6 changed files with 23 additions and 13 deletions

2
Cargo.lock generated
View File

@ -889,7 +889,7 @@ dependencies = [
[[package]]
name = "little-hesinde"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"calibre-db",
"clap",

View File

@ -1,6 +1,6 @@
[package]
name = "little-hesinde"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
license = { workspace = true }
authors = { workspace = true }

View File

@ -1,16 +1,13 @@
//! Deal with cursor pagination.
use std::fmt::Debug;
use super::error::HandlerError;
use crate::templates::TEMPLATES;
use calibre_db::data::error::DataStoreError;
use poem::{error::InternalServerError, web::Html, IntoResponse, Response};
use serde::Serialize;
use std::fmt::Debug;
use tera::Context;
use crate::templates::TEMPLATES;
use super::error::HandlerError;
/// Render a tera template with paginated items and generate back and forth links.
pub fn render<T: Serialize + Debug, F, S, P, M>(
template: &str,
@ -25,11 +22,18 @@ where
P: Fn(&str) -> Result<bool, DataStoreError>,
M: Fn(&str) -> Result<bool, DataStoreError>,
{
let items = fetcher().map_err(HandlerError::DataError)?;
let mut context = Context::new();
context.insert("nav", template);
let items = fetcher().map_err(HandlerError::DataError)?;
if items.is_empty() {
return Ok(TEMPLATES
.render("empty", &context)
.map_err(InternalServerError)
.map(Html)?
.into_response());
}
// fails already in the sql query if there is nothing returned
let first_item = items.first().unwrap();
let last_item = items.last().unwrap();
@ -42,7 +46,6 @@ where
context.insert("has_more", &has_more);
context.insert("backward_cursor", &backward_cursor);
context.insert("forward_cursor", &forward_cursor);
context.insert("nav", template);
context.insert(template, &items);
Ok(TEMPLATES

View File

@ -71,7 +71,7 @@ pub mod opds {
pub mod templates;
pub const APP_NAME: &str = "little-hesinde";
pub const VERSION: &str = "0.1.4";
pub const VERSION: &str = "0.1.5";
/// Internal marker data in lieu of a proper `Accept` header.
#[derive(Debug, Clone, Copy)]

View File

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

View File

@ -0,0 +1,6 @@
{% extends "base" %}
{% block title %}
{% endblock title %}
{% block content %}
<h2>No items</h2>
{% endblock content %}