crud

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

The reference CRUD application

The MVP acceptance demonstration (spec task 67): a TodoMVC-style todo manager with create, read, update (inline title edit and done-toggle), delete, active/completed filters, toggle-all, and clear-completed — every htmx interaction a partial update, with zero hand-written htmx glue. TestZeroHandWrittenGlue enforces that claim mechanically. The filter buttons reuse the bound /todos route with hx-vals; the bulk operations are their own bound routes announcing themselves through one merged todos-bulk-changed event. The store is capped (100 todos, 200-character titles) because the app also runs as a public live demo on the documentation site.

go run ./cmd      # serves http://localhost:8080
go test .         # end-to-end suite incl. the break-the-build proofs

Where each requirement lives

Requirement Demonstration
FR-020/021 — route-aware bindings crud.ghtmx: hx-post={ CreateTodo } / hx-get={ ListTodos } / hx-get={ TodoStats } (symbol bindings, paths folded statically) and ghtmxgen.EditTodo/ToggleTodo/RenameTodo/DeleteTodo(t.ID) (typed constructors with escaped parameters)
FR-031 — compile-time fragments todoList (page-embedded and standalone byte-identically — asserted by TestListModeSelection), nested todoRow, handler-only todoEditRow, statsPanel
FR-035 — adapter mode selection ListTodos via nethttp.WithPage: browsers get the page, htmx gets the bare list
FR-037 — event contract event TodoCreated/TodoToggled/TodoDeleted/TodosBulkChanged declarations; handlers emit only ghtmxgen.Emit*; the stats panel listens with hx-trigger="todo-created from:body, …"

The break-the-build guarantee

TestRouteChangesBreakTheBuild copies the app into a scratch module and proves the acceptance criterion three ways: deleting a bound route fails generation with GHTMX-E0101 naming the binding; renaming a handler (registration included) fails generation the same way; and renaming a symbol-bound handler after generation breaks go build immediately through the generated blank-identifier pin. Constructor bindings break at the next ghtmx generate, which watch mode and CI keep in the loop.

Known-informational diagnostics

Generation reports one warning by design: GHTMX-W0104 for the GET / page route (a full-page route is navigated to, not bound). The handler-only todoEditRow fragment generates warning-free — the reachability check counts the handler's todoEditRowFragment(...) call as rendering.

Documentation

Overview

The task-67 reference CRUD application: the MVP acceptance demonstration. Handlers render fragments through the nethttp adapter (FR-035 opt-in mode selection), emit contract events through the generated ghtmxgen emitters (FR-037), and the templates bind every URL through route-aware bindings (FR-020/FR-021) — zero hand-written htmx glue.

Renaming or deleting any route breaks the build at every binding site; main_test.go proves it. Symbol bindings pin the handler name in the committed generated code, so a rename breaks `go build` immediately; constructor bindings break at the next `ghtmx generate` (which watch and CI keep in the loop).

Index

Constants

This section is empty.

Variables

View Source
var StoreSelector func(*http.Request) *Store

StoreSelector lets an embedder scope the demo's state per request — the docs site keys it by a visitor cookie so no two visitors share a list. Nil (the default, and the reference wiring) means the single shared package store. Install it before serving; handlers read it on every request.

Functions

func ClearCompleted

func ClearCompleted(w http.ResponseWriter, r *http.Request)

ClearCompleted deletes every done todo and returns the refreshed list.

func CreateTodo

func CreateTodo(w http.ResponseWriter, r *http.Request)

CreateTodo adds a todo, announces it through the event contract, and returns the refreshed list fragment.

func DeleteTodo

func DeleteTodo(w http.ResponseWriter, r *http.Request)

DeleteTodo removes a todo; the empty response with an outerHTML swap removes the row client-side.

func EditTodo

func EditTodo(w http.ResponseWriter, r *http.Request)

EditTodo swaps a row for its inline edit form.

func Index

func Index(w http.ResponseWriter, r *http.Request)

Index serves the full page.

func ListTodos

func ListTodos(w http.ResponseWriter, r *http.Request)

ListTodos is the FR-035 demonstration: a browser navigating to /todos gets the full page, an htmx refresh gets the bare list — the adapter selects the mode from the request. The filter buttons call the same bound route with hx-vals, so the view narrows without any hand-written URL.

func RenameTodo

func RenameTodo(w http.ResponseWriter, r *http.Request)

RenameTodo saves an edited title and swaps the row back.

func Routes

func Routes() *http.ServeMux

Routes builds the example's router; the official docs site mounts it as a live demo.

func TodoStats

func TodoStats(w http.ResponseWriter, r *http.Request)

TodoStats renders the stats panel fragment.

func ToggleAllTodos

func ToggleAllTodos(w http.ResponseWriter, r *http.Request)

ToggleAllTodos flips the whole list at once and announces the bulk change through one merged event.

func ToggleTodo

func ToggleTodo(w http.ResponseWriter, r *http.Request)

ToggleTodo flips a todo's done state and returns its refreshed row.

Types

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is an in-memory todo store with deterministic IDs.

func NewStore

func NewStore() *Store

NewStore builds an empty todo store. Embedders that need isolated instances (the docs site scopes one per visitor) construct their own and route to them through StoreSelector.

type Todo

type Todo struct {
	ID    string
	Title string
	Done  bool
}

Todo is the row model.

Directories

Path Synopsis
Command crud serves the crud example standalone.
Command crud serves the crud example standalone.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL