//! {{controller_title}} controller — HTTP layer only.
//!
//! Per the RustF layering rule (Base Model -> Model -> Module -> Controller),
//! controllers stay thin: routing, flash, redirects, view/JSON responses.
//! Business logic and model access live in `crate::modules::{{controller_name}}_service`.
//! If you find yourself about to call `Model::query()` from here, stop and
//! create the service instead.
//!
//! VIEW DATA — two channels, don't mix them up:
//! * `ctx.view(tpl, model)` -> `@{M...}`. ONE simple object: the record or
//! form the page is about. A page with no single subject passes `json!({})`.
//! * `ctx.repository_set(k, v)` -> `@{R.k}`. Everything else: title, csrf,
//! collections, nav, counters. Set each key as the data becomes
//! available — never assemble one big `json!` and dump it into the model.
use rustf::prelude::*;
pub fn install() -> Vec<Route> {
routes![
GET "/{{route_prefix}}" => index,{{#if crud}}
GET "/{{route_prefix}}/list" => list,
GET "/{{route_prefix}}/{id}" => show,
POST "/{{route_prefix}}" => create,
PUT "/{{route_prefix}}/{id}" => update,
DELETE "/{{route_prefix}}/{id}" => delete,{{/if}}{{#if routes}}
GET "/{{route_prefix}}/about" => about,
POST "/{{route_prefix}}/action" => action,{{/if}}
]
}
async fn index(ctx: &mut Context) -> rustf::Result<()> {
// R: one key per piece of data, set as it becomes available.
ctx.repository_set("title", "{{controller_title}}");
ctx.repository_set("message", "Welcome to the {{controller_name}} controller");
// M: no single record on this page.
ctx.view("{{route_prefix}}/index", json!({}))
}
{{#if crud}}
async fn list(ctx: &mut Context) -> rustf::Result<()> {
// TODO: call crate::modules::{{controller_name}}_service::list_all().await?
// Then push to repository / respond with JSON.
let items = vec![
json!({"id": 1, "name": "Item 1"}),
json!({"id": 2, "name": "Item 2"}),
];
ctx.json(json!({ "success": true, "data": items }))
}
async fn show(ctx: &mut Context) -> rustf::Result<()> {
let id = ctx.param_as::<i64>("id")?;
// TODO: call crate::modules::{{controller_name}}_service::find(id).await?
let item = json!({ "id": id, "name": format!("Item {}", id) });
ctx.json(json!({ "success": true, "data": item }))
}
async fn create(ctx: &mut Context) -> rustf::Result<()> {
let body: serde_json::Value = ctx.body_json()?;
// TODO: call crate::modules::{{controller_name}}_service::create(body).await?
ctx.json(json!({ "success": true, "data": body }))
}
async fn update(ctx: &mut Context) -> rustf::Result<()> {
let id = ctx.param_as::<i64>("id")?;
let body: serde_json::Value = ctx.body_json()?;
// TODO: call crate::modules::{{controller_name}}_service::update(id, body).await?
ctx.json(json!({ "success": true, "id": id, "data": body }))
}
async fn delete(ctx: &mut Context) -> rustf::Result<()> {
let id = ctx.param_as::<i64>("id")?;
// TODO: call crate::modules::{{controller_name}}_service::delete(id).await?
ctx.json(json!({ "success": true, "id": id }))
}
{{/if}}
{{#if routes}}
async fn about(ctx: &mut Context) -> rustf::Result<()> {
ctx.repository_set("title", "About {{controller_title}}");
ctx.repository_set("description", "This controller manages {{description}}");
ctx.view("{{route_prefix}}/about", json!({}))
}
async fn action(ctx: &mut Context) -> rustf::Result<()> {
let body: serde_json::Value = ctx.body_json()?;
// TODO: call crate::modules::{{controller_name}}_service::action(body).await?
ctx.json(json!({ "success": true, "data": body }))
}
{{/if}}