92 lines
3.3 KiB
Python
92 lines
3.3 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 iso_thread as iso_thread_crud
|
||
from app.deps import require_permission
|
||
from app.models.user import User
|
||
from app.schemas.iso_thread import IsoThreadCreate, IsoThreadRead, IsoThreadUpdate
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
@router.get("", response_model=list[IsoThreadRead])
|
||
async def list_iso_threads(
|
||
skip: int = 0,
|
||
limit: int = 100,
|
||
db: AsyncSession = Depends(get_db),
|
||
_: User = Depends(require_permission("iso_threads:read")),
|
||
):
|
||
return await iso_thread_crud.list_all(db, skip=skip, limit=limit)
|
||
|
||
|
||
@router.post("", response_model=IsoThreadRead, status_code=status.HTTP_201_CREATED)
|
||
async def create_iso_thread(
|
||
payload: IsoThreadCreate,
|
||
db: AsyncSession = Depends(get_db),
|
||
actor: User = Depends(require_permission("iso_threads:write")),
|
||
):
|
||
thread = await iso_thread_crud.create(
|
||
db,
|
||
diameter_mm=payload.diameter_mm,
|
||
pitch_mm=payload.pitch_mm,
|
||
depth_mm=payload.depth_mm,
|
||
)
|
||
await audit_crud.log(
|
||
db, actor=actor, action="create", resource_type="IsoThread",
|
||
resource_id=thread.id,
|
||
description=audit_crud.describe_create("IsoThread", thread.id, f"M{thread.diameter_mm}×{thread.pitch_mm}"),
|
||
)
|
||
return thread
|
||
|
||
|
||
@router.get("/{iso_thread_id}", response_model=IsoThreadRead)
|
||
async def get_iso_thread(
|
||
iso_thread_id: int,
|
||
db: AsyncSession = Depends(get_db),
|
||
_: User = Depends(require_permission("iso_threads:read")),
|
||
):
|
||
thread = await iso_thread_crud.get(db, iso_thread_id)
|
||
if thread is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="ISO thread not found")
|
||
return thread
|
||
|
||
|
||
@router.patch("/{iso_thread_id}", response_model=IsoThreadRead)
|
||
async def update_iso_thread(
|
||
iso_thread_id: int,
|
||
payload: IsoThreadUpdate,
|
||
db: AsyncSession = Depends(get_db),
|
||
actor: User = Depends(require_permission("iso_threads:write")),
|
||
):
|
||
thread = await iso_thread_crud.get(db, iso_thread_id)
|
||
if thread is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="ISO thread not found")
|
||
changes = payload.model_dump(exclude_unset=True)
|
||
thread = await iso_thread_crud.update(db, thread, changes)
|
||
await audit_crud.log(
|
||
db, actor=actor, action="update", resource_type="IsoThread",
|
||
resource_id=thread.id,
|
||
description=audit_crud.describe_update("IsoThread", thread.id, changes),
|
||
)
|
||
return thread
|
||
|
||
|
||
@router.delete("/{iso_thread_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||
async def delete_iso_thread(
|
||
iso_thread_id: int,
|
||
db: AsyncSession = Depends(get_db),
|
||
actor: User = Depends(require_permission("iso_threads:delete")),
|
||
):
|
||
thread = await iso_thread_crud.get(db, iso_thread_id)
|
||
if thread is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="ISO thread not found")
|
||
name = f"M{thread.diameter_mm}×{thread.pitch_mm}"
|
||
await iso_thread_crud.delete(db, thread)
|
||
await audit_crud.log(
|
||
db, actor=actor, action="delete", resource_type="IsoThread",
|
||
resource_id=iso_thread_id,
|
||
description=audit_crud.describe_delete("IsoThread", iso_thread_id, name),
|
||
)
|