103 lines
3.6 KiB
Python
103 lines
3.6 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 clamp_connection as clamp_connection_crud
|
||
from app.deps import require_permission
|
||
from app.models.user import User
|
||
from app.schemas.clamp_connection import (
|
||
ClampConnectionCreate,
|
||
ClampConnectionRead,
|
||
ClampConnectionUpdate,
|
||
)
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
@router.get("", response_model=list[ClampConnectionRead])
|
||
async def list_clamp_connections(
|
||
skip: int = 0,
|
||
limit: int = 100,
|
||
db: AsyncSession = Depends(get_db),
|
||
_: User = Depends(require_permission("clamp_connections:read")),
|
||
):
|
||
return await clamp_connection_crud.list_all(db, skip=skip, limit=limit)
|
||
|
||
|
||
@router.post("", response_model=ClampConnectionRead, status_code=status.HTTP_201_CREATED)
|
||
async def create_clamp_connection(
|
||
payload: ClampConnectionCreate,
|
||
db: AsyncSession = Depends(get_db),
|
||
actor: User = Depends(require_permission("clamp_connections:write")),
|
||
):
|
||
cc = await clamp_connection_crud.create(
|
||
db,
|
||
min_mm=payload.min_mm,
|
||
max_mm=payload.max_mm,
|
||
min_in=payload.min_in,
|
||
max_in=payload.max_in,
|
||
)
|
||
await audit_crud.log(
|
||
db, actor=actor, action="create", resource_type="ClampConnection",
|
||
resource_id=cc.id,
|
||
description=audit_crud.describe_create(
|
||
"ClampConnection", cc.id,
|
||
f"{cc.min_mm}–{cc.max_mm} mm" if cc.min_mm is not None else f"{cc.min_in}–{cc.max_in} in",
|
||
),
|
||
)
|
||
return cc
|
||
|
||
|
||
@router.get("/{clamp_connection_id}", response_model=ClampConnectionRead)
|
||
async def get_clamp_connection(
|
||
clamp_connection_id: int,
|
||
db: AsyncSession = Depends(get_db),
|
||
_: User = Depends(require_permission("clamp_connections:read")),
|
||
):
|
||
cc = await clamp_connection_crud.get(db, clamp_connection_id)
|
||
if cc is None:
|
||
raise HTTPException(status_code=404, detail="Clamp connection not found")
|
||
return cc
|
||
|
||
|
||
@router.patch("/{clamp_connection_id}", response_model=ClampConnectionRead)
|
||
async def update_clamp_connection(
|
||
clamp_connection_id: int,
|
||
payload: ClampConnectionUpdate,
|
||
db: AsyncSession = Depends(get_db),
|
||
actor: User = Depends(require_permission("clamp_connections:write")),
|
||
):
|
||
cc = await clamp_connection_crud.get(db, clamp_connection_id)
|
||
if cc is None:
|
||
raise HTTPException(status_code=404, detail="Clamp connection not found")
|
||
changes = payload.model_dump(exclude_unset=True)
|
||
cc = await clamp_connection_crud.update(db, cc, changes)
|
||
await audit_crud.log(
|
||
db, actor=actor, action="update", resource_type="ClampConnection",
|
||
resource_id=cc.id,
|
||
description=audit_crud.describe_update("ClampConnection", cc.id, changes),
|
||
)
|
||
return cc
|
||
|
||
|
||
@router.delete("/{clamp_connection_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||
async def delete_clamp_connection(
|
||
clamp_connection_id: int,
|
||
db: AsyncSession = Depends(get_db),
|
||
actor: User = Depends(require_permission("clamp_connections:delete")),
|
||
):
|
||
cc = await clamp_connection_crud.get(db, clamp_connection_id)
|
||
if cc is None:
|
||
raise HTTPException(status_code=404, detail="Clamp connection not found")
|
||
label = (
|
||
f"{cc.min_mm}–{cc.max_mm} mm" if cc.min_mm is not None
|
||
else f"{cc.min_in}–{cc.max_in} in"
|
||
)
|
||
await clamp_connection_crud.delete(db, cc)
|
||
await audit_crud.log(
|
||
db, actor=actor, action="delete", resource_type="ClampConnection",
|
||
resource_id=clamp_connection_id,
|
||
description=audit_crud.describe_delete("ClampConnection", clamp_connection_id, label),
|
||
)
|