feat: location hierarchy CRUD API #106

Merged
forbes merged 1 commits from feat-location-crud into main 2026-02-15 09:16:53 +00:00
Owner

Summary

Add location hierarchy CRUD API (issue #81). The locations table already exists from migration 001 — this adds the repository layer, HTTP handlers, routes, and tests.

Endpoints

Method Path Auth Description
GET /api/locations viewer+ List all locations, or ?tree={path} for subtree
POST /api/locations editor+ Create location (auto-resolves parent_id and depth from path)
GET /api/locations/{path..} viewer+ Get by hierarchical path (e.g., lab/shelf-a/bin-3)
PUT /api/locations/{path..} editor+ Update name, type, metadata
DELETE /api/locations/{path..} editor+ Delete (rejects if inventory records reference it)

Files

New:

  • internal/db/locations.goLocationRepository with List, GetByPath, GetByID, GetChildren, GetTree, Create, Update, Delete, HasInventory
  • internal/api/location_handlers.go — HTTP handlers + request/response types
  • internal/api/location_handlers_test.go — 10 integration tests

Modified:

  • internal/api/handlers.go — Wire LocationRepository into Server
  • internal/api/routes.go — Register location routes (chi wildcard for multi-segment paths)
  • internal/testutil/testutil.go — Add locations to TruncateAll

Design notes

  • Uses chi wildcard (/*) routes to support multi-segment location paths like /api/locations/lab/shelf-a/bin-3
  • Create auto-calculates depth from path segments and resolves parent_id from the parent path
  • Delete checks for inventory references before removing, and cascades to child locations
  • GetTree uses SQL LIKE path/% for efficient subtree queries

Unblocks #82 (inventory tracking API).

Closes #81

## Summary Add location hierarchy CRUD API (issue #81). The `locations` table already exists from migration 001 — this adds the repository layer, HTTP handlers, routes, and tests. ### Endpoints | Method | Path | Auth | Description | |--------|------|------|-------------| | GET | `/api/locations` | viewer+ | List all locations, or `?tree={path}` for subtree | | POST | `/api/locations` | editor+ | Create location (auto-resolves parent_id and depth from path) | | GET | `/api/locations/{path..}` | viewer+ | Get by hierarchical path (e.g., `lab/shelf-a/bin-3`) | | PUT | `/api/locations/{path..}` | editor+ | Update name, type, metadata | | DELETE | `/api/locations/{path..}` | editor+ | Delete (rejects if inventory records reference it) | ### Files **New:** - `internal/db/locations.go` — `LocationRepository` with List, GetByPath, GetByID, GetChildren, GetTree, Create, Update, Delete, HasInventory - `internal/api/location_handlers.go` — HTTP handlers + request/response types - `internal/api/location_handlers_test.go` — 10 integration tests **Modified:** - `internal/api/handlers.go` — Wire `LocationRepository` into Server - `internal/api/routes.go` — Register location routes (chi wildcard for multi-segment paths) - `internal/testutil/testutil.go` — Add `locations` to TruncateAll ### Design notes - Uses chi wildcard (`/*`) routes to support multi-segment location paths like `/api/locations/lab/shelf-a/bin-3` - `Create` auto-calculates depth from path segments and resolves `parent_id` from the parent path - `Delete` checks for inventory references before removing, and cascades to child locations - `GetTree` uses SQL `LIKE path/%` for efficient subtree queries Unblocks #82 (inventory tracking API). Closes #81
forbes added 1 commit 2026-02-15 09:09:57 +00:00
Add LocationRepository with CRUD operations, hierarchy traversal
(children, subtree by path prefix), and inventory-safe deletion.

Endpoints:
  GET    /api/locations          — list all or ?tree={path} for subtree
  POST   /api/locations          — create (auto-resolves parent_id, depth)
  GET    /api/locations/{path..} — get by hierarchical path
  PUT    /api/locations/{path..} — update name, type, metadata
  DELETE /api/locations/{path..} — delete (rejects if inventory exists)

Uses chi wildcard routes to support multi-segment paths like
/api/locations/lab/shelf-a/bin-3.

Includes 10 handler integration tests covering CRUD, nesting,
validation, duplicates, tree queries, and delete-not-found.

Closes #81
forbes force-pushed feat-location-crud from 0ca93cedd0 to 4ef912cf4b 2026-02-15 09:16:07 +00:00 Compare
forbes merged commit 71603bb6d7 into main 2026-02-15 09:16:53 +00:00
forbes deleted branch feat-location-crud 2026-02-15 09:16:53 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: kindred/silo#106