little-hesinde/rusty-library/src/handlers/recents.rs

34 lines
867 B
Rust
Raw Normal View History

2024-05-02 16:10:29 +00:00
use std::sync::Arc;
use poem::{
error::InternalServerError,
handler,
web::{Data, Html},
};
use tera::Context;
2024-05-06 07:09:40 +00:00
use crate::{
2024-05-09 06:39:46 +00:00
app_state::AppState, data::book::Book, handlers::error::HandlerError, templates::TEMPLATES,
2024-05-06 07:09:40 +00:00
};
2024-05-02 16:10:29 +00:00
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::Error> {
2024-05-09 06:39:46 +00:00
let recent_books = state
.calibre
.recent_books(25)
.map_err(HandlerError::DataError)?;
2024-05-06 07:09:40 +00:00
let recent_books = recent_books
.iter()
2024-05-06 08:40:34 +00:00
.filter_map(|x| Book::full_book(x, &state))
2024-05-06 07:09:40 +00:00
.collect::<Vec<Book>>();
2024-05-02 16:10:29 +00:00
let mut context = Context::new();
2024-05-06 11:51:49 +00:00
context.insert("title", "Recent Books");
context.insert("nav", "recent");
2024-05-02 16:10:29 +00:00
context.insert("books", &recent_books);
TEMPLATES
2024-05-06 12:42:14 +00:00
.render("book_list", &context)
2024-05-02 16:10:29 +00:00
.map_err(InternalServerError)
.map(Html)
}