business-card-site/API/app/contollers/contest_carousel_photos_router.py

68 lines
2.1 KiB
Python

from fastapi import Depends, File, UploadFile, APIRouter
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.responses import FileResponse
from app.database.session import get_db
from app.domain.entities.contest_carousel_photo import ContestCarouselPhotoEntity
from app.infrastructure.contest_carousel_photos_service import ContestCarouselPhotosService
router = APIRouter()
@router.get(
"/contests/{contest_id}/",
response_model=list[ContestCarouselPhotoEntity],
summary="Get all carousel photos metadata for a contest",
description="Returns metadata of all carousel photos for the given contest_id",
)
async def get_photos_by_contest_id(
contest_id: int,
db: AsyncSession = Depends(get_db),
):
service = ContestCarouselPhotosService(db)
return await service.get_photos_by_contest_id(contest_id)
@router.get(
"/{photo_id}/file",
response_class=FileResponse,
summary="Download carousel photo file by photo ID",
description="Returns the image file for the given carousel photo ID",
)
async def download_photo_file(
photo_id: int,
db: AsyncSession = Depends(get_db),
):
service = ContestCarouselPhotosService(db)
return await service.get_photo_file_by_id(photo_id)
@router.post(
"/contests/{contest_id}/upload",
response_model=ContestCarouselPhotoEntity,
summary="Upload a new carousel photo for a contest",
description="Uploads a new photo file and associates it with the given contest ID",
)
async def upload_photo(
contest_id: int,
file: UploadFile = File(...),
db: AsyncSession = Depends(get_db),
):
service = ContestCarouselPhotosService(db)
return await service.upload_photo(contest_id, file)
@router.delete(
"/{photo_id}/",
response_model=ContestCarouselPhotoEntity,
summary="Delete a carousel photo by ID",
description="Deletes a carousel photo and its file from storage",
)
async def delete_photo(
photo_id: int,
db: AsyncSession = Depends(get_db),
):
service = ContestCarouselPhotosService(db)
return await service.delete_photo(photo_id)