writeonce/docs/examples/shop/orders.controller.wo
shoney.arickathil 89df517613 feat(shop): the program template — MVC on disk; story 37 redirected
- docs/examples/shop: types.wo model, per-feature view modules
  (render() classes), root controller files, layout shell/header/
  footer, static assets controller + real style.css, README with
  Angular file map + run + DX referendum notes
- buy flow proven by hand: stock check/decrement, 409s, traversal
  404, css typed, WAL-durable; builds on the UNPATCHED toolchain
- two language gaps recorded, not fixed (per directive): pub+@table
  cannot combine (controllers forced into root module); @view
  projection classes absent
- story 37 REDIRECTED per Vue-SFC review: view.html compiled by woc
  ({{}} auto-escaped, w:if/w:for, typed against view class, no
  runtime engine); render()-as-concatenation failed the referendum;
  forks revised; shop named the acceptance consumer

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-23 23:57:44 +02:00

55 lines
1.8 KiB
Text

-- orders.controller.wo — the buying flow: stock-checked order creation
-- (decrement + insert are each WAL-committed before they acknowledge)
-- and the orders list (ref navigation: o.product.name).
use framework/http
use layout
use orders
use time
pub class CreateOrder {
pad: Int
fn handle(req: Req) -> Resp {
let sku = req.params["sku"];
if sku == nil {
return html_error(404, "No such product", "The order names no product.");
}
let f = form_values(req);
if f == nil {
return html_error(400, "Bad order", "The form did not arrive form-encoded.");
}
let qraw = f["qty"];
if qraw == nil {
return html_error(400, "Bad order", "How many? The qty field is missing.");
}
let qty = parse_int(trim("${qraw}"));
if qty == nil or qty < 1 {
return html_error(400, "Bad order", "qty must be a positive number.");
}
let hits = from p in Product where p.sku == sku take 1 select p;
if len(hits) == 0 {
return html_error(404, "No such product", "Nothing is listed under that sku.");
}
let p = hits[0];
if p.stock < qty {
return html_error(409, "Not enough stock", "Only ${p.stock} left of ${p.name}.");
}
let total = p.price * float(qty);
p.stock = p.stock - qty;
insert Order { product: p, qty: qty, total: total, placed: time.now(), status: "placed" };
let page = OrderOk { name: p.name, qty: qty, total: total };
return ok_html(app_shell("shop — order placed", page.render()));
}
}
pub class ListOrders {
pad: Int
fn handle(req: Req) -> Resp {
let rows = "";
for o in from x in Order select x {
let row = OrderRow { name: o.product.name, qty: o.qty, total: o.total, status: o.status };
rows = rows .. row.render();
}
let page = OrdersPage { rows: rows };
return ok_html(app_shell("shop — orders", page.render()));
}
}