36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, HTTPException, UploadFile, status
|
|
|
|
from app.core.dependencies import CurrentUser, DbSession, get_minio_client
|
|
from app.models.object_photo import ObjectPhoto
|
|
from app.schemas.photo import PhotoRead
|
|
from app.services import objects as objects_service
|
|
from app.services.photos import build_photo_read, delete_photo, upload_photo
|
|
|
|
router = APIRouter(prefix="/api/v1", tags=["photos"])
|
|
|
|
|
|
@router.post(
|
|
"/objects/{object_id}/photos", response_model=PhotoRead, status_code=status.HTTP_201_CREATED
|
|
)
|
|
async def upload_object_photo(
|
|
db: DbSession, _: CurrentUser, object_id: uuid.UUID, file: UploadFile
|
|
) -> PhotoRead:
|
|
obj = await objects_service.get_object(db, object_id)
|
|
if obj is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Object not found")
|
|
try:
|
|
photo = await upload_photo(db, get_minio_client(), object_id, file)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))
|
|
return build_photo_read(photo)
|
|
|
|
|
|
@router.delete("/photos/{photo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_object_photo(db: DbSession, _: CurrentUser, photo_id: uuid.UUID) -> None:
|
|
photo = await db.get(ObjectPhoto, photo_id)
|
|
if photo is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Photo not found")
|
|
await delete_photo(db, get_minio_client(), photo)
|