102 lines
3.5 KiB
Python
102 lines
3.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.crud import audit as audit_crud
|
|
from app.crud import part_colour as part_colour_crud
|
|
from app.deps import require_permission
|
|
from app.models.user import User
|
|
from app.schemas.part_colour import PartColourCreate, PartColourRead, PartColourUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=list[PartColourRead])
|
|
async def list_part_colours(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("part_colours:read")),
|
|
):
|
|
return await part_colour_crud.list_all(db, skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("", response_model=PartColourRead, status_code=status.HTTP_201_CREATED)
|
|
async def create_part_colour(
|
|
payload: PartColourCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
actor: User = Depends(require_permission("part_colours:write")),
|
|
):
|
|
if await part_colour_crud.get_by_name(db, payload.name):
|
|
raise HTTPException(status_code=409, detail="Part colour already exists")
|
|
pc = await part_colour_crud.create(db, name=payload.name)
|
|
await audit_crud.log(
|
|
db,
|
|
actor=actor,
|
|
action="create",
|
|
resource_type="PartColour",
|
|
resource_id=pc.id,
|
|
description=audit_crud.describe_create("PartColour", pc.id, pc.name),
|
|
)
|
|
return pc
|
|
|
|
|
|
@router.get("/{part_colour_id}", response_model=PartColourRead)
|
|
async def get_part_colour(
|
|
part_colour_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("part_colours:read")),
|
|
):
|
|
pc = await part_colour_crud.get(db, part_colour_id)
|
|
if pc is None:
|
|
raise HTTPException(status_code=404, detail="Part colour not found")
|
|
return pc
|
|
|
|
|
|
@router.patch("/{part_colour_id}", response_model=PartColourRead)
|
|
async def update_part_colour(
|
|
part_colour_id: int,
|
|
payload: PartColourUpdate,
|
|
db: AsyncSession = Depends(get_db),
|
|
actor: User = Depends(require_permission("part_colours:write")),
|
|
):
|
|
pc = await part_colour_crud.get(db, part_colour_id)
|
|
if pc is None:
|
|
raise HTTPException(status_code=404, detail="Part colour not found")
|
|
changes = payload.model_dump(exclude_unset=True)
|
|
if "name" in changes:
|
|
existing = await part_colour_crud.get_by_name(db, changes["name"])
|
|
if existing is not None and existing.id != pc.id:
|
|
raise HTTPException(status_code=409, detail="Part colour already exists")
|
|
pc = await part_colour_crud.update(db, pc, changes)
|
|
await audit_crud.log(
|
|
db,
|
|
actor=actor,
|
|
action="update",
|
|
resource_type="PartColour",
|
|
resource_id=pc.id,
|
|
description=audit_crud.describe_update("PartColour", pc.id, changes),
|
|
)
|
|
return pc
|
|
|
|
|
|
@router.delete("/{part_colour_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_part_colour(
|
|
part_colour_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
actor: User = Depends(require_permission("part_colours:delete")),
|
|
):
|
|
pc = await part_colour_crud.get(db, part_colour_id)
|
|
if pc is None:
|
|
raise HTTPException(status_code=404, detail="Part colour not found")
|
|
name = pc.name
|
|
await part_colour_crud.delete(db, pc)
|
|
await audit_crud.log(
|
|
db,
|
|
actor=actor,
|
|
action="delete",
|
|
resource_type="PartColour",
|
|
resource_id=part_colour_id,
|
|
description=audit_crud.describe_delete("PartColour", part_colour_id, name),
|
|
)
|