use askama::Template;
use axum::{
Router,
extract::{Path, State},
response::Html,
routing::get,
};
use ocre::{Ctx, OptionExt, Page, Result, render};
use crate::models::<%= singular %>::{self, <%= model %>};
pub fn routes() -> Router<Ctx> {
Router::new()
.route("/<%= plural %>", get(index))
.route("/<%= plural %>/{id}", get(show))
}
#[allow(dead_code)]
pub mod paths {
use std::fmt::Display;
pub fn index() -> &'static str {
"/<%= plural %>"
}
pub fn show(id: impl Display) -> String {
format!("/<%= plural %>/{id}")
}
}
#[derive(Template)]
#[template(path = "<%= plural %>/index.html")]
struct IndexView {
<%= plural %>: Vec<<%= model %>>,
}
#[derive(Template)]
#[template(path = "<%= plural %>/show.html")]
struct ShowView {
<%= singular %>: <%= model %>,
}
async fn index(State(ctx): State<Ctx>, page: Page) -> Result<Html<String>> {
render(&IndexView { <%= plural %>: <%= singular %>::all(&ctx, page).await? })
}
async fn show(State(ctx): State<Ctx>, Path(id): Path<i64>) -> Result<Html<String>> {
render(&ShowView { <%= singular %>: <%= singular %>::find(&ctx, id).await?.or_404()? })
}