0 { continue }
b, err := migrationsFS.ReadFile("migrations/" + name)
if err != nil { return fmt.Errorf("read %s: %w", name, err) }
if _, err := d.Exec(string(b)); err != nil { return fmt.Errorf("apply %s: %w", name, err) }
if _, err := d.Exec(`INSERT INTO schema_migrations (name) VALUES (?)`, name); err != nil {
return fmt.Errorf("record %s: %w", name, err)
}
}
return nil
}
''',
"internal/db/migrations/00001_init.sql": '''CREATE TABLE IF NOT EXISTS locations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
parent_id INTEGER NULL REFERENCES locations(id) ON DELETE CASCADE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX IF NOT EXISTS locations_name_parent ON locations(name, IFNULL(parent_id, 0));
''',
"internal/models/location.go": '''package models
import (
"database/sql"
"fmt"
)
type Location struct {
ID int64
Name string
ParentID *int64
Depth int
}
type Store struct {
DB *sql.DB
}
func (s *Store) Create(name string, parentID *int64) (*Location, error) {
res, err := s.DB.Exec(`INSERT INTO locations (name, parent_id) VALUES (?, ?)`, name, parentID)
if err != nil { return nil, fmt.Errorf("insert location: %w", err) }
id, err := res.LastInsertId()
if err != nil { return nil, err }
return &Location{ID: id, Name: name, ParentID: parentID, Depth: 0}, nil
}
func (s *Store) Tree() ([]Location, error) {
rows, err := s.DB.Query(`WITH RECURSIVE tree AS (
SELECT id, name, parent_id, 0 AS depth FROM locations WHERE parent_id IS NULL
UNION ALL
SELECT l.id, l.name, l.parent_id, t.depth+1 FROM locations l JOIN tree t ON l.parent_id = t.id
) SELECT id, name, parent_id, depth FROM tree ORDER BY depth, name`)
if err != nil { return nil, fmt.Errorf("tree query: %w", err) }
defer rows.Close()
var out []Location
for rows.Next() {
var l Location
var pid sql.NullInt64
if err := rows.Scan(&l.ID, &l.Name, &pid, &l.Depth); err != nil { return nil, err }
if pid.Valid { l.ParentID = &pid.Int64 }
out = append(out, l)
}
return out, rows.Err()
}
''',
"internal/handlers/handlers.go": '''package handlers
import (
"embed"
"html/template"
"log"
"net/http"
"strconv"
"strings"
"locations/internal/models"
)
//go:embed templates/*.html
var templatesFS embed.FS
type Handlers struct {
store *models.Store
pageTmpl *template.Template
treeTmpl *template.Template
}
func New(store *models.Store) *Handlers {
pageTmpl := template.Must(template.ParseFS(templatesFS, "templates/page.html", "templates/tree.html"))
treeTmpl := template.Must(template.ParseFS(templatesFS, "templates/tree.html"))
return &Handlers{store: store, pageTmpl: pageTmpl, treeTmpl: treeTmpl}
}
func (h *Handlers) Routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /", h.index)
mux.HandleFunc("POST /", h.create)
return mux
}
func (h *Handlers) index(w http.ResponseWriter, r *http.Request) {
locations, err := h.store.Tree()
if err != nil {
log.Printf("tree: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := h.pageTmpl.Execute(w, locations); err != nil {
log.Printf("render page: %v", err)
}
}
func (h *Handlers) create(w http.ResponseWriter, r *http.Request) {
name := strings.TrimSpace(r.FormValue("name"))
if name == "" {
http.Error(w, "name is required", http.StatusBadRequest)
return
}
parentStr := r.FormValue("parent_id")
var parentID *int64
if parentStr != "" {
id, err := strconv.ParseInt(parentStr, 10, 64)
if err != nil {
http.Error(w, "bad parent_id", http.StatusBadRequest)
return
}
parentID = &id
}
if _, err := h.store.Create(name, parentID); err != nil {
log.Printf("create: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
locations, err := h.store.Tree()
if err != nil {
log.Printf("tree after create: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
// Return JUST the tree fragment for the HTMX swap (not a redirect, not a full page).
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
if err := h.treeTmpl.ExecuteTemplate(w, "tree", locations); err != nil {
log.Printf("render tree: %v", err)
}
}
''',
"internal/handlers/handlers_test.go": '''package handlers
import (
"database/sql"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"locations/internal/models"
_ "modernc.org/sqlite"
)
func newTestStore(t *testing.T) (*sql.DB, func()) {
t.Helper()
db, err := sql.Open("sqlite", ":memory:")
if err != nil { t.Fatalf("open: %v", err) }
_, err = db.Exec(`CREATE TABLE locations (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, parent_id INTEGER NULL REFERENCES locations(id), created_at DATETIME DEFAULT CURRENT_TIMESTAMP)`)
if err != nil { db.Close(); t.Fatalf("create table: %v", err) }
return db, func() { db.Close() }
}
func TestCreateRootAndChild(t *testing.T) {
db, cleanup := newTestStore(t)
defer cleanup()
store := &models.Store{DB: db}
h := New(store)
srv := httptest.NewServer(h.Routes())
defer srv.Close()
// POST a root location.
resp, err := http.PostForm(srv.URL+"/", url.Values{"name": {"BuildingA"}, "parent_id": {""}})
if err != nil { t.Fatalf("post root: %v", err) }
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("post root status = %d, want 200", resp.StatusCode)
}
// Get the root's id from the DB.
var rootID int64
if err := db.QueryRow("SELECT id FROM locations WHERE name = 'BuildingA'").Scan(&rootID); err != nil {
t.Fatalf("query root id: %v", err)
}
// POST a child.
resp2, err := http.PostForm(srv.URL+"/", url.Values{"name": {"Floor1"}, "parent_id": {strconv.FormatInt(rootID, 10)}})
if err != nil { t.Fatalf("post child: %v", err) }
defer resp2.Body.Close()
if resp2.StatusCode != http.StatusOK {
t.Fatalf("post child status = %d, want 200", resp2.StatusCode)
}
body, _ := io.ReadAll(resp2.Body)
if !strings.Contains(string(body), "Floor1") {
t.Fatalf("response does not contain child name; got:\\n%s", body)
}
if !strings.Contains(string(body), "BuildingA") {
t.Fatalf("response does not contain root name (tree should include all); got:\\n%s", body)
}
}
''',
"internal/handlers/templates/page.html": '''
Locations
Locations
{{template "tree" .}}
''',
"internal/handlers/templates/tree.html": '''{{define "tree"}}
{{range .}}- {{.Name}}
{{end}}
{{end}}
''',
}
def gates(ws, port=None):
"""Return ({gate_name: bool}, detail)."""
g = {k: False for k in GATES}
if not tools.go_has_real_app(ws.dir):
return g, {"reason": "no real app present (starter stub or missing internal/ packages)"}
bt, d = tools.build_test_gates(ws, TOOLSET, "go test -v ./...", tools.go_tests_passed)
g.update(bt)
g["serves"], checks, probe = tools.go_web_probe(
ws, port, lambda html: '