little-hesinde/cops-web/src/handlers/recents.rs

33 lines
918 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::{
app_state::AppState, data::book::Book, handlers::error::SqliteError, templates::TEMPLATES,
};
2024-05-02 16:10:29 +00:00
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::Error> {
2024-05-06 07:09:40 +00:00
let recent_books = state.calibre.recent_books(50).map_err(SqliteError)?;
let recent_books = recent_books
.iter()
.filter_map(|x| {
let author = state.calibre.book_author(x.id).ok()?;
let series = state.calibre.book_series(x.id).ok()?;
Some(Book::from_db_book(x, series, &author.name))
})
.collect::<Vec<Book>>();
2024-05-02 16:10:29 +00:00
let mut context = Context::new();
context.insert("books", &recent_books);
TEMPLATES
.render("recents", &context)
.map_err(InternalServerError)
.map(Html)
}