ocre-cli 0.2.0

Command-line tool for Ocre: create, generate, migrate, run and deploy apps.
//! <%= human_plural %> JSON API. Generated by `<%= command %>`.
//! Queries and rules live in the model, `crate::models::<%= singular %>`.
//! Add actions (create, update, delete) as you need them; `ocre g api` shows
//! complete ones.
//!
//! GET /api/<%= plural %>?limit=&offset=   list, newest first (`Link` header: next/prev pages)
//! GET /api/<%= plural %>/{id}             one

use axum::{
    Router,
    extract::{Path, State},
    routing::get,
};
use ocre::{ApiResult, Ctx, Json, OptionExt, Page, PageLinks};

use crate::models::<%= singular %>::{self, <%= model %>};

pub fn routes() -> Router<Ctx> {
    Router::new()
        .route("/api/<%= plural %>", get(index))
        .route("/api/<%= plural %>/{id}", get(show))
}

async fn index(State(ctx): State<Ctx>, page: Page) -> ApiResult<(PageLinks, Json<Vec<<%= model %>>>)> {
    let <%= plural %> = <%= singular %>::all(&ctx, page).await?;
    Ok((page.links("/api/<%= plural %>", <%= plural %>.len()), Json(<%= plural %>)))
}

async fn show(State(ctx): State<Ctx>, Path(id): Path<i64>) -> ApiResult<Json<<%= model %>>> {
    Ok(Json(<%= singular %>::find(&ctx, id).await?.or_404()?))
}