from typing import Optional from fastapi import APIRouter, Depends from sqlalchemy.ext.asyncio import AsyncSession from app.database.session import get_db from app.domain.entities.contest import ContestEntity from app.infrastructure.contests_service import ContestsService from app.infrastructure.dependencies import require_admin, get_current_user router = APIRouter() @router.get( '/', response_model=list[ContestEntity], summary='Get all contests', description='Returns all contests', ) async def get_all_contests( db: AsyncSession = Depends(get_db), ): contests_service = ContestsService(db) return await contests_service.get_all_contests() @router.post( '/', response_model=Optional[ContestEntity], summary='Create a new contest', description='Creates a new contest', ) async def create_contest( contest: ContestEntity, db: AsyncSession = Depends(get_db), user=Depends(require_admin), ): contests_service = ContestsService(db) return await contests_service.create_contest(contest) @router.put( '/{contest_id}/', response_model=Optional[ContestEntity], summary='Update a contest', description='Updates a contest', ) async def update_contest( contest_id: int, contest: ContestEntity, db: AsyncSession = Depends(get_db), user=Depends(get_current_user), ): contests_service = ContestsService(db) return await contests_service.update_contest(contest_id, contest, user) @router.delete( '/{contest_id}/', response_model=Optional[ContestEntity], summary='Delete a contest', description='Delete a contest', ) async def delete_contest( contest_id: int, db: AsyncSession = Depends(get_db), user=Depends(require_admin), ): contests_service = ContestsService(db) return await contests_service.delete(contest_id, user) @router.get( '/project/{project_id}/', response_model=Optional[ContestEntity], summary='Get project by contest ID', description='Retrieve project data by contest ID', ) async def get_project_by_contest_id( project_id: int, db: AsyncSession = Depends(get_db), user=Depends(get_current_user), ): contests_service = ContestsService(db) contest = await contests_service.get_by_project(project_id) return contest