feat: location hierarchy CRUD API #106
Reference in New Issue
Block a user
Delete Branch "feat-location-crud"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Add location hierarchy CRUD API (issue #81). The
locationstable already exists from migration 001 — this adds the repository layer, HTTP handlers, routes, and tests.Endpoints
/api/locations?tree={path}for subtree/api/locations/api/locations/{path..}lab/shelf-a/bin-3)/api/locations/{path..}/api/locations/{path..}Files
New:
internal/db/locations.go—LocationRepositorywith List, GetByPath, GetByID, GetChildren, GetTree, Create, Update, Delete, HasInventoryinternal/api/location_handlers.go— HTTP handlers + request/response typesinternal/api/location_handlers_test.go— 10 integration testsModified:
internal/api/handlers.go— WireLocationRepositoryinto Serverinternal/api/routes.go— Register location routes (chi wildcard for multi-segment paths)internal/testutil/testutil.go— Addlocationsto TruncateAllDesign notes
/*) routes to support multi-segment location paths like/api/locations/lab/shelf-a/bin-3Createauto-calculates depth from path segments and resolvesparent_idfrom the parent pathDeletechecks for inventory references before removing, and cascades to child locationsGetTreeuses SQLLIKE path/%for efficient subtree queriesUnblocks #82 (inventory tracking API).
Closes #81
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 #810ca93cedd0to4ef912cf4b