69 lines
2.1 KiB
Python
69 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_file import ContestFileEntity
|
|
from app.infrastructure.dependencies import require_admin
|
|
from app.infrastructure.contest_files_service import ContestFilesService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get(
|
|
"/contests/{contest_id}/",
|
|
response_model=list[ContestFileEntity],
|
|
summary="Get all contest files",
|
|
description="Returns metadata of all files uploaded for the specified contest."
|
|
)
|
|
async def get_files_by_contest_id(
|
|
contest_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
service = ContestFilesService(db)
|
|
return await service.get_files_by_contest_id(contest_id)
|
|
|
|
|
|
@router.get(
|
|
"/{file_id}/file",
|
|
response_class=FileResponse,
|
|
summary="Download contest file by ID",
|
|
description="Returns the file for the specified file ID."
|
|
)
|
|
async def download_contest_file(
|
|
file_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
service = ContestFilesService(db)
|
|
return await service.get_file_by_id(file_id)
|
|
|
|
|
|
@router.post(
|
|
"/contests/{contest_id}/upload",
|
|
response_model=ContestFileEntity,
|
|
summary="Upload a new file for the contest",
|
|
description="Uploads a new file and associates it with the specified contest."
|
|
)
|
|
async def upload_contest_file(
|
|
contest_id: int,
|
|
file: UploadFile = File(...),
|
|
db: AsyncSession = Depends(get_db),
|
|
user=Depends(require_admin),
|
|
):
|
|
service = ContestFilesService(db)
|
|
return await service.upload_file(contest_id, file, user)
|
|
|
|
|
|
@router.delete(
|
|
"/{file_id}/",
|
|
response_model=ContestFileEntity,
|
|
summary="Delete a contest file by ID",
|
|
description="Deletes the file and its database entry."
|
|
)
|
|
async def delete_contest_file(
|
|
file_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
user=Depends(require_admin),
|
|
):
|
|
service = ContestFilesService(db)
|
|
return await service.delete_file(file_id, user) |