Fix boundary/child count invariant on shelf and book deletion
When deleting a shelf or book, remove the corresponding boundary from the parent's boundary list so len(boundaries) == len(children) - 1 is maintained. Add API-level tests covering first, middle, and last child deletion. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
20
src/api.py
20
src/api.py
@@ -205,11 +205,19 @@ async def update_shelf(shelf_id: str, request: Request) -> dict[str, Any]:
|
||||
@router.delete("/api/shelves/{shelf_id}")
|
||||
async def delete_shelf(shelf_id: str) -> dict[str, Any]:
|
||||
with db.connection() as c:
|
||||
if not db.get_shelf(c, shelf_id):
|
||||
shelf = db.get_shelf(c, shelf_id)
|
||||
if not shelf:
|
||||
raise HTTPException(404, "Shelf not found")
|
||||
photos = db.collect_shelf_photos(c, shelf_id)
|
||||
rank = db.get_shelf_rank(c, shelf_id)
|
||||
cab = db.get_cabinet(c, shelf.cabinet_id)
|
||||
with db.transaction() as c:
|
||||
db.delete_shelf(c, shelf_id)
|
||||
if cab:
|
||||
bounds: list[float] = json.loads(cab.shelf_boundaries) if cab.shelf_boundaries else []
|
||||
if bounds:
|
||||
del bounds[min(rank, len(bounds) - 1)]
|
||||
db.set_cabinet_boundaries(c, cab.id, json.dumps(bounds))
|
||||
for fn in photos:
|
||||
del_photo(fn)
|
||||
return {"ok": True}
|
||||
@@ -293,11 +301,19 @@ async def update_book(book_id: str, request: Request) -> dict[str, Any]:
|
||||
@router.delete("/api/books/{book_id}")
|
||||
async def delete_book(book_id: str) -> dict[str, Any]:
|
||||
with db.connection() as c:
|
||||
if not db.get_book(c, book_id):
|
||||
book = db.get_book(c, book_id)
|
||||
if not book:
|
||||
raise HTTPException(404, "Book not found")
|
||||
fn = db.get_book_photo(c, book_id)
|
||||
rank = db.get_book_rank(c, book_id)
|
||||
shelf = db.get_shelf(c, book.shelf_id)
|
||||
with db.transaction() as c:
|
||||
db.delete_book(c, book_id)
|
||||
if shelf:
|
||||
bounds: list[float] = json.loads(shelf.book_boundaries) if shelf.book_boundaries else []
|
||||
if bounds:
|
||||
del bounds[min(rank, len(bounds) - 1)]
|
||||
db.set_shelf_boundaries(c, shelf.id, json.dumps(bounds))
|
||||
del_photo(fn)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user