refactor: move transaction management from services to API endpoints
- Services now use db.flush() instead of db.commit() for database operations - API endpoints handle transaction commit after service calls - Remove db.rollback() from services (let exception handlers manage this) - Ensures consistent transaction boundaries at API layer This pattern gives API endpoints full control over when to commit, allowing for better error handling and potential multi-operation transactions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
25
app/api/v1/vendor/inventory.py
vendored
25
app/api/v1/vendor/inventory.py
vendored
@@ -36,7 +36,9 @@ def set_inventory(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Set exact inventory quantity (replaces existing)."""
|
||||
return inventory_service.set_inventory(db, current_user.token_vendor_id, inventory)
|
||||
result = inventory_service.set_inventory(db, current_user.token_vendor_id, inventory)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/inventory/adjust", response_model=InventoryResponse)
|
||||
@@ -46,7 +48,9 @@ def adjust_inventory(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Adjust inventory (positive to add, negative to remove)."""
|
||||
return inventory_service.adjust_inventory(db, current_user.token_vendor_id, adjustment)
|
||||
result = inventory_service.adjust_inventory(db, current_user.token_vendor_id, adjustment)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/inventory/reserve", response_model=InventoryResponse)
|
||||
@@ -56,7 +60,9 @@ def reserve_inventory(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Reserve inventory for an order."""
|
||||
return inventory_service.reserve_inventory(db, current_user.token_vendor_id, reservation)
|
||||
result = inventory_service.reserve_inventory(db, current_user.token_vendor_id, reservation)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/inventory/release", response_model=InventoryResponse)
|
||||
@@ -66,7 +72,9 @@ def release_reservation(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Release reserved inventory (cancel order)."""
|
||||
return inventory_service.release_reservation(db, current_user.token_vendor_id, reservation)
|
||||
result = inventory_service.release_reservation(db, current_user.token_vendor_id, reservation)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/inventory/fulfill", response_model=InventoryResponse)
|
||||
@@ -76,7 +84,9 @@ def fulfill_reservation(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Fulfill reservation (complete order, remove from stock)."""
|
||||
return inventory_service.fulfill_reservation(db, current_user.token_vendor_id, reservation)
|
||||
result = inventory_service.fulfill_reservation(db, current_user.token_vendor_id, reservation)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/inventory/product/{product_id}", response_model=ProductInventorySummary)
|
||||
@@ -119,9 +129,11 @@ def update_inventory(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Update inventory entry."""
|
||||
return inventory_service.update_inventory(
|
||||
result = inventory_service.update_inventory(
|
||||
db, current_user.token_vendor_id, inventory_id, inventory_update
|
||||
)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.delete("/inventory/{inventory_id}", response_model=InventoryMessageResponse)
|
||||
@@ -132,4 +144,5 @@ def delete_inventory(
|
||||
):
|
||||
"""Delete inventory entry."""
|
||||
inventory_service.delete_inventory(db, current_user.token_vendor_id, inventory_id)
|
||||
db.commit()
|
||||
return InventoryMessageResponse(message="Inventory deleted successfully")
|
||||
|
||||
Reference in New Issue
Block a user