writeonce/docs/examples/shop/orders/controller.wo
shoney.arickathil ffd791d05b refactor(porch): name the web framework porch, fix the wo.toml identifier claim
- docs/examples/writeonce-serve -> docs/examples/porch (git mv, history kept);
  `[deps]` key and import are now `porch` / `porch/http` / `porch/router`
- name history preserved on the library README, not rewritten into dated
  records: writeonce-framework -> writeonce-serve (08-25) -> porch (08-26).
  Stories, specs, plans and the audit reports keep the older name by the
  repo's own convention; only live docs and every path link were rewritten
- left alone deliberately: `internal/serve.wo`, `pub fn serve`, `serve_conn`,
  `app.serve(...)` — those are functions, not the module name
- web-app/wo.toml comment corrected: it claimed hyphens are not identifier
  characters and named a key this file never used. lexer.ml's `is_ident_cont`
  DOES accept `-` (an internal dash is part of the identifier, which is why
  binary minus needs spaces), so a hyphenated key would be legal too
- site now teaches the name: package card, the two-deps chapter and the
  handlers-are-classes chapter say `porch`; site-accept asserted the old
  /packages/serve route and caught the rename, as a gate should
- gates: web-app 46/0, site 21/0, deps-accept 8/0, oop-e2e 116/0,
  linkcheck 0 broken / 0 anchors; porch typechecks entry-less as kind=library

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-26 19:36:34 +02:00

53 lines
1.9 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 porch/http
use layout
use time
pub class CreateOrder {
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 };
let shell = AppShell { title: "shop — order placed", content: page.render() };
return ok_html(shell.render());
}
}
pub class ListOrders {
fn handle(req: Req) -> Resp {
let rows: multi Component = [];
for o in from x in Order select x {
push(rows, OrderRow { name: o.product.name, qty: o.qty, total: o.total, status: o.status });
}
let page = OrdersPage { rows: rows };
let shell = AppShell { title: "shop — orders", content: page.render() };
return ok_html(shell.render());
}
}