paginated authors
This commit is contained in:
parent
352d4e0a7a
commit
ead3672570
12 changed files with 175 additions and 23 deletions
|
@ -1,10 +1,34 @@
|
|||
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, handlers::paginated};
|
||||
|
||||
#[handler]
|
||||
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<String, poem::Error> {
|
||||
Ok("authors".to_string())
|
||||
pub async fn handler_init(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::Error> {
|
||||
paginated::render(
|
||||
"authors",
|
||||
|| state.calibre.authors(25, None, &SortOrder::ASC),
|
||||
|author| author.sort.clone(),
|
||||
|cursor| state.calibre.has_previous_authors(cursor),
|
||||
|cursor| state.calibre.has_more_authors(cursor),
|
||||
)
|
||||
}
|
||||
|
||||
#[handler]
|
||||
pub async fn handler(
|
||||
Path((cursor, sort_order)): Path<(String, SortOrder)>,
|
||||
state: Data<&Arc<AppState>>,
|
||||
) -> Result<Html<String>, poem::Error> {
|
||||
paginated::render(
|
||||
"authors",
|
||||
|| state.calibre.authors(25, Some(&cursor), &sort_order),
|
||||
|author| author.sort.clone(),
|
||||
|cursor| state.calibre.has_previous_authors(cursor),
|
||||
|cursor| state.calibre.has_more_authors(cursor),
|
||||
)
|
||||
}
|
||||
|
|
46
rusty-library/src/handlers/paginated.rs
Normal file
46
rusty-library/src/handlers/paginated.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use calibre_db::data::error::DataStoreError;
|
||||
use poem::{error::InternalServerError, web::Html};
|
||||
use serde::Serialize;
|
||||
use tera::Context;
|
||||
|
||||
use crate::templates::TEMPLATES;
|
||||
|
||||
use super::error::SqliteError;
|
||||
|
||||
pub fn render<T: Serialize, F, S, P, M>(
|
||||
template: &str,
|
||||
fetcher: F,
|
||||
sort_field: S,
|
||||
has_previous: P,
|
||||
has_more: M,
|
||||
) -> Result<Html<String>, poem::Error>
|
||||
where
|
||||
F: Fn() -> Result<Vec<T>, DataStoreError>,
|
||||
S: Fn(&T) -> String,
|
||||
P: Fn(&str) -> Result<bool, DataStoreError>,
|
||||
M: Fn(&str) -> Result<bool, DataStoreError>,
|
||||
{
|
||||
let items = fetcher().map_err(SqliteError)?;
|
||||
|
||||
let mut context = Context::new();
|
||||
|
||||
// fails already in the sql query if there is nothing returned
|
||||
let first_item = items.first().unwrap();
|
||||
let last_item = items.last().unwrap();
|
||||
|
||||
let (backward_cursor, forward_cursor) = (sort_field(first_item), sort_field(last_item));
|
||||
|
||||
let has_previous = has_previous(&backward_cursor).map_err(SqliteError)?;
|
||||
let has_more = has_more(&forward_cursor).map_err(SqliteError)?;
|
||||
|
||||
context.insert("has_previous", &has_previous);
|
||||
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);
|
||||
TEMPLATES
|
||||
.render(template, &context)
|
||||
.map_err(InternalServerError)
|
||||
.map(Html)
|
||||
}
|
|
@ -13,13 +13,15 @@ use crate::{
|
|||
|
||||
#[handler]
|
||||
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::Error> {
|
||||
let recent_books = state.calibre.recent_books(50).map_err(SqliteError)?;
|
||||
let recent_books = state.calibre.recent_books(25).map_err(SqliteError)?;
|
||||
let recent_books = recent_books
|
||||
.iter()
|
||||
.filter_map(|x| Book::full_book(x, &state))
|
||||
.collect::<Vec<Book>>();
|
||||
|
||||
let mut context = Context::new();
|
||||
context.insert("title", "Recent Books");
|
||||
context.insert("nav", "recent");
|
||||
context.insert("books", &recent_books);
|
||||
TEMPLATES
|
||||
.render("recents", &context)
|
||||
|
|
|
@ -23,6 +23,7 @@ mod handlers {
|
|||
pub mod cover;
|
||||
pub mod download;
|
||||
pub mod error;
|
||||
pub mod paginated;
|
||||
pub mod recents;
|
||||
pub mod series;
|
||||
}
|
||||
|
@ -48,7 +49,11 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
let app = Route::new()
|
||||
.at("/", get(handlers::recents::handler))
|
||||
.at("/books", get(handlers::books::handler))
|
||||
.at("/authors", get(handlers::authors::handler))
|
||||
.at("/authors", get(handlers::authors::handler_init))
|
||||
.at(
|
||||
"/authors/:cursor/:sort_order",
|
||||
get(handlers::authors::handler),
|
||||
)
|
||||
.at("/series", get(handlers::series::handler))
|
||||
.at("/cover/:id", get(handlers::cover::handler))
|
||||
.at("/book/:id/:format", get(handlers::download::handler))
|
||||
|
|
|
@ -6,6 +6,7 @@ pub static TEMPLATES: Lazy<Tera> = Lazy::new(|| {
|
|||
tera.add_raw_templates(vec![
|
||||
("base", include_str!("../templates/base.html")),
|
||||
("book_card", include_str!("../templates/book_card.html")),
|
||||
("authors", include_str!("../templates/authors.html")),
|
||||
("recents", include_str!("../templates/recents.html")),
|
||||
])
|
||||
.expect("failed to parse tera templates");
|
||||
|
|
|
@ -3,6 +3,21 @@
|
|||
overflow: scroll;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
header.fixed {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 1000;
|
||||
background: var(--pico-background-color)
|
||||
}
|
||||
|
||||
nav ul li {
|
||||
padding-top: 0rem;
|
||||
padding-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.nav-active {
|
||||
border-bottom: solid var(--pico-primary-underline);
|
||||
}
|
||||
|
||||
.book-card hgroup {
|
||||
margin-bottom: 0;
|
||||
|
|
19
rusty-library/templates/authors.html
Normal file
19
rusty-library/templates/authors.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{% extends "base" %}
|
||||
{% block title %}
|
||||
{% if has_previous %}
|
||||
<a href="/authors/{{ backward_cursor }}/DESC">← back</a>
|
||||
{% endif %}
|
||||
{% if has_previous and has_more %}|{% endif%}
|
||||
|
||||
{% if has_more %}
|
||||
<a href="/authors/{{ forward_cursor }}/ASC">more →</a>
|
||||
{% endif %}
|
||||
{% endblock title %}
|
||||
|
||||
{% block content %}
|
||||
<div class="grid-container">
|
||||
{% for author in authors %}
|
||||
<article>{{ author.name }}</article>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock content %}
|
|
@ -9,18 +9,20 @@
|
|||
<title>Rusty Library</title>
|
||||
</head>
|
||||
<body>
|
||||
<main class="container">
|
||||
<header class="container fixed">
|
||||
<nav>
|
||||
<ul>
|
||||
<li><strong>Library</strong></li>
|
||||
<li>{% block title %}<strong>{{ title }}</strong>{% endblock title %}</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><a href="/">Recent</a></li>
|
||||
<li><a href="/authors">Authors</a></li>
|
||||
<li><a href="/books">Books</a></li>
|
||||
<li><a href="/series">Series</a></li>
|
||||
<li {% if nav == "recent" %}class = "nav-active"{% endif %}><a href="/">Recent</a></li>
|
||||
<li {% if nav == "authors" %}class = "nav-active"{% endif %}><a href="/authors">Authors</a></li>
|
||||
<li {% if nav == "books" %}class = "nav-active"{% endif %}><a href="/books">Books</a></li>
|
||||
<li {% if nav == "series" %}class = "nav-active"{% endif %}><a href="/series">Series</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
<main class="container">
|
||||
{% block content %}{% endblock content %}
|
||||
</main>
|
||||
</body>
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
{% extends "base" %}
|
||||
{% block content %}
|
||||
<h1>Recent Books</h1>
|
||||
<div class="grid-container">
|
||||
{% for book in books %}
|
||||
{% include "book_card" %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue