44 řádky
965 B
Rust
44 řádky
965 B
Rust
|
use actix_web::{get, web::Data, HttpRequest, HttpResponse};
|
||
|
use askama::Template;
|
||
|
|
||
|
use super::tcx::TemplateCtx;
|
||
|
use crate::{
|
||
|
ctx::Ctx,
|
||
|
db::models::{Board, LocalStats, NewsPost},
|
||
|
error::NekrochanError,
|
||
|
filters,
|
||
|
web::template_response,
|
||
|
};
|
||
|
|
||
|
#[derive(Template)]
|
||
|
#[template(path = "index.html")]
|
||
|
|
||
|
struct IndexTemplate {
|
||
|
tcx: TemplateCtx,
|
||
|
news: Option<NewsPost>,
|
||
|
boards: Vec<Board>,
|
||
|
stats: LocalStats,
|
||
|
}
|
||
|
|
||
|
#[get("/")]
|
||
|
pub async fn index(ctx: Data<Ctx>, req: HttpRequest) -> Result<HttpResponse, NekrochanError> {
|
||
|
let tcx = TemplateCtx::new(&ctx, &req).await?;
|
||
|
|
||
|
if tcx.boards.is_empty() {
|
||
|
return Err(NekrochanError::HomePageError);
|
||
|
}
|
||
|
|
||
|
let news = NewsPost::read_latest(&ctx).await?;
|
||
|
let boards = Board::read_all(&ctx).await?;
|
||
|
let stats = LocalStats::read(&ctx).await?;
|
||
|
|
||
|
let template = IndexTemplate {
|
||
|
tcx,
|
||
|
boards,
|
||
|
stats,
|
||
|
news,
|
||
|
};
|
||
|
|
||
|
template_response(&template)
|
||
|
}
|