Add contest table and routes
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from lib.db import get_connection
|
||||
from lib.logger import logger
|
||||
from models.contest import Contest
|
||||
from models.msg import Message
|
||||
|
||||
router = APIRouter(prefix="/contest", tags=["contest"])
|
||||
|
||||
|
||||
@router.get("/", response_model=list[Contest])
|
||||
async def list_contests():
|
||||
with get_connection() as conn:
|
||||
logger.debug("Listing all contests")
|
||||
df = conn.execute("SELECT * FROM Contest").fetchdf()
|
||||
logger.debug(f"Contests: {df}")
|
||||
contests = [Contest(**row) for row in df.to_dict(orient="records")]
|
||||
return contests
|
||||
|
||||
|
||||
@router.get("/{year}", response_model=Contest)
|
||||
async def get_contest(year: int):
|
||||
with get_connection() as conn:
|
||||
df = conn.execute("SELECT * FROM Contest WHERE year = ?", (year,)).fetchdf()
|
||||
if df.empty:
|
||||
raise HTTPException(status_code=404, detail="Contest not found")
|
||||
contest = Contest(**df.to_dict(orient="records")[0])
|
||||
return contest
|
||||
|
||||
|
||||
@router.put("/", response_model=Message)
|
||||
async def update_contest(year: int, is_active: bool):
|
||||
with get_connection() as conn:
|
||||
logger.debug(f"Updating contest {year} to active: {is_active}")
|
||||
conn.execute(
|
||||
"UPDATE Contest SET is_active = ? WHERE year = ?",
|
||||
(is_active, year),
|
||||
)
|
||||
return Message(message="Contest updated successfully")
|
||||
Reference in New Issue
Block a user