Adding ability to update / patch by sku, adding uniqueness constraint violation checks

This commit is contained in:
Ian Roddis committed 2026-09-21 10:43:36 -03:00
1 parent af5343da0c
commit d8db87c13e
13 files changed
+541 -13

No files matched your search

+24 -10
View File
@@ -20,6 +20,15 @@ from app.services import augmented_part_sync
router = APIRouter()
async def _get_augmented_part_by_identifier(db: AsyncSession, identifier: str):
"""Resolve an AugmentedPart by numeric id, falling back to sku."""
if identifier.isdigit():
ap = await augmented_part_crud.get(db, int(identifier))
if ap is not None:
return ap
return await augmented_part_crud.get_by_sku(db, identifier)
@router.get("", response_model=list[AugmentedPartRead])
async def list_augmented_parts(
skip: int = 0,
@@ -39,6 +48,8 @@ async def create_augmented_part(
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("augmented_parts:write")),
):
if payload.sku and await augmented_part_crud.get_by_sku(db, payload.sku):
raise HTTPException(status_code=409, detail="SKU already in use")
ap = await augmented_part_crud.create(
db,
sku=payload.sku,
@@ -94,17 +105,21 @@ async def get_augmented_part(
return ap
@router.patch("/{augmented_part_id}", response_model=AugmentedPartRead)
@router.patch("/{identifier}", response_model=AugmentedPartRead)
async def update_augmented_part(
augmented_part_id: int,
identifier: str,
payload: AugmentedPartUpdate,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("augmented_parts:write")),
):
ap = await augmented_part_crud.get(db, augmented_part_id)
ap = await _get_augmented_part_by_identifier(db, identifier)
if ap is None:
raise HTTPException(status_code=404, detail="Augmented part not found")
changes = payload.model_dump(exclude_unset=True)
if changes.get("sku"):
existing = await augmented_part_crud.get_by_sku(db, changes["sku"])
if existing is not None and existing.id != ap.id:
raise HTTPException(status_code=409, detail="SKU already in use")
ap = await augmented_part_crud.update(db, ap, changes)
await audit_crud.log(
db,
@@ -117,15 +132,16 @@ async def update_augmented_part(
return ap
@router.delete("/{augmented_part_id}", status_code=status.HTTP_204_NO_CONTENT)
@router.delete("/{identifier}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_augmented_part(
augmented_part_id: int,
identifier: str,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("augmented_parts:delete")),
):
ap = await augmented_part_crud.get(db, augmented_part_id)
ap = await _get_augmented_part_by_identifier(db, identifier)
if ap is None:
raise HTTPException(status_code=404, detail="Augmented part not found")
resolved_id = ap.id
label = ap.sku or ap.description
await augmented_part_crud.delete(db, ap)
await audit_crud.log(
@@ -133,10 +149,8 @@ async def delete_augmented_part(
actor=actor,
action="delete",
resource_type="AugmentedPart",
resource_id=augmented_part_id,
description=audit_crud.describe_delete(
"AugmentedPart", augmented_part_id, label
),
resource_id=resolved_id,
description=audit_crud.describe_delete("AugmentedPart", resolved_id, label),
)