vnj/src/http/novels/list.rs
2024-11-17 18:34:33 +03:00

50 lines
1.1 KiB
Rust

use std::collections::HashMap;
use axum::routing::post;
use serde::Serialize;
use crate::{
result::{ApiResult, FromJson},
schemas::novel::FullNovel,
state::{Router, State},
};
#[derive(Debug, serde::Deserialize)]
struct Args {
pub start_from: Option<jiff::Zoned>,
}
#[derive(Debug, Serialize, Default)]
struct Output {
pub novels: HashMap<String, FullNovel>,
pub errors: Vec<String>,
}
async fn list(state: State, FromJson(args): FromJson<Args>) -> ApiResult<Output> {
let mut out = Output::default();
let novels = state.app.read().enumerate_novels();
for (slug, entry) in novels {
let novel = match entry.get_full_info() {
Ok(n) => n,
Err(e) => {
out.errors.push(e.to_string());
continue;
}
};
if let Some(ref start_from) = args.start_from {
if &novel.data.post_at < start_from {
continue;
}
}
out.novels.insert(slug, novel);
}
Ok(out.into())
}
pub fn make() -> Router {
Router::new().route("/", post(list))
}