ocre-cli 0.2.0

Command-line tool for Ocre: create, generate, migrate, run and deploy apps.
//! <%= human_plural %> pages (HTML). Generated by `<%= command %>`.
//! Queries and rules live in the model, `crate::models::<%= singular %>`.
//! Add actions (new, create, edit...) as you need them; `ocre g scaffold` shows
//! complete ones.

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))
}

/// Paths of the <%= human_singular | lower %> pages, for links (`{{ paths::show(<%= singular %>.<%= key %>) }}`) and redirects.
#[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()? })
}