61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""HTTP mapping for explicit repair workflows."""
|
|
|
|
from typing import Annotated
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
from app.identity import require_permission
|
|
from app.repairs.dependencies import get_repair_service
|
|
from app.repairs.exceptions import (
|
|
RepairActorRequired,
|
|
RepairConflict,
|
|
RepairMinerNotFound,
|
|
)
|
|
from app.repairs.models import BeginRepairRequest, BeginRepairResponse
|
|
from app.repairs.service import RepairService
|
|
|
|
|
|
router = APIRouter(prefix="/api/repairs", tags=["repairs"])
|
|
require_repair_create = require_permission("repairs.create")
|
|
|
|
RepairServiceDependency = Annotated[
|
|
RepairService,
|
|
Depends(get_repair_service),
|
|
]
|
|
RepairCreateUser = Annotated[dict, Depends(require_repair_create)]
|
|
|
|
|
|
@router.post(
|
|
"",
|
|
response_model=BeginRepairResponse,
|
|
status_code=status.HTTP_201_CREATED,
|
|
)
|
|
def begin_repair(
|
|
request: BeginRepairRequest,
|
|
service: RepairServiceDependency,
|
|
current_user: RepairCreateUser,
|
|
) -> BeginRepairResponse:
|
|
try:
|
|
repair = service.begin_repair(
|
|
request,
|
|
actor_user_id=UUID(str(current_user["id"])),
|
|
)
|
|
except RepairMinerNotFound as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Miner not found",
|
|
) from exc
|
|
except RepairConflict as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=str(exc),
|
|
) from exc
|
|
except RepairActorRequired as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
detail=str(exc),
|
|
) from exc
|
|
|
|
return BeginRepairResponse(data=repair)
|