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

38 lines
1019 B
Rust

//! Handle requests for a single series.
use std::sync::Arc;
use poem::{
handler,
web::{Data, Path},
Response,
};
use crate::{app_state::AppState, data::book::Book, handlers::error::HandlerError, Accept};
/// Handle a request for a series with `id` and decide whether to render to html or OPDS.
#[handler]
pub async fn handler(
id: Path<u64>,
accept: Data<&Accept>,
state: Data<&Arc<AppState>>,
) -> Result<Response, poem::Error> {
let series = state
.calibre
.scalar_series(*id)
.map_err(HandlerError::DataError)?;
let books = state
.calibre
.series_books(*id)
.map_err(HandlerError::DataError)?;
let books = books
.iter()
.filter_map(|x| Book::full_book(x, &state))
.collect::<Vec<Book>>();
match accept.0 {
Accept::Html => crate::handlers::html::series_single::handler(series, books).await,
Accept::Opds => crate::handlers::opds::series_single::handler(series, books).await,
}
}