Add song listing endpoints
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
from fastapi import APIRouter
|
||||
# from sqlmodel import Session, select
|
||||
|
||||
# from models.user import User
|
||||
# from models.group import Group
|
||||
|
||||
# from ..app import get_session
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["Admin"])
|
||||
|
||||
|
||||
# @router.patch("/contest/status")
|
||||
# async def set_contest_status(is_active: bool, session: Session = Depends(get_session)):
|
||||
# """
|
||||
# Set the contest status (active/inactive)
|
||||
# Only accessible to admin users
|
||||
# """
|
||||
# # Implementation will be added with authentication logic
|
||||
# return {"is_active": is_active}
|
||||
|
||||
# @router.post("/users")
|
||||
# async def create_user(username: str, password: str, group_id: int, session: Session = Depends(get_session)):
|
||||
# """
|
||||
# Add a new user
|
||||
# Only accessible to admin users
|
||||
# """
|
||||
# # Implementation will be added with proper password hashing
|
||||
# return {"username": username, "group_id": group_id}
|
||||
|
||||
# @router.get("/users")
|
||||
# async def list_users(group_id: int = None, is_active: bool = None, session: Session = Depends(get_session)):
|
||||
# """
|
||||
# List users with optional filters
|
||||
# Only accessible to admin users
|
||||
# """
|
||||
# # Implementation for filtering users
|
||||
# return {"users": []}
|
||||
|
||||
# @router.get("/users/{user_id}")
|
||||
# async def get_user(user_id: int, session: Session = Depends(get_session)):
|
||||
# """
|
||||
# Get details for a specific user
|
||||
# Only accessible to admin users
|
||||
# """
|
||||
# # Implementation for getting user details
|
||||
# return {"user_id": user_id}
|
||||
|
||||
# @router.patch("/users/{user_id}")
|
||||
# async def update_user(
|
||||
# user_id: int,
|
||||
# group_id: int = None,
|
||||
# password: str = None,
|
||||
# is_active: bool = None,
|
||||
# session: Session = Depends(get_session)
|
||||
# ):
|
||||
# """
|
||||
# Update a user's details
|
||||
# Only accessible to admin users
|
||||
# """
|
||||
# # Implementation for updating user details
|
||||
# return {"user_id": user_id}
|
||||
@@ -0,0 +1,7 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["auth"])
|
||||
|
||||
@router.post("/token")
|
||||
async def login():
|
||||
return {"message": "Login successful"}
|
||||
@@ -0,0 +1,3 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/results", tags=["results"])
|
||||
@@ -0,0 +1,3 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/songs", tags=["songs"])
|
||||
@@ -0,0 +1,41 @@
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from lib.db import get_connection
|
||||
from models.song import Song
|
||||
from datetime import datetime
|
||||
|
||||
router = APIRouter(prefix="/songs", tags=["songs"])
|
||||
|
||||
|
||||
@router.get("/", response_model=list[Song])
|
||||
async def list_songs(year: int = Query(default=None)):
|
||||
# Use current year if year is not provided
|
||||
if year is None:
|
||||
year = datetime.now().year
|
||||
with get_connection() as conn:
|
||||
df = conn.execute(
|
||||
"SELECT Song.*, CountryCodes.name_fi AS country_fi, CountryCodes.name_en AS country_en, CountryCodes.code AS country_code FROM Song, CountryCodes WHERE Song.country = CountryCodes.code AND year = ? ORDER BY running_order",
|
||||
(year,),
|
||||
).fetchdf()
|
||||
# Convert DataFrame to a list of dictionaries
|
||||
songs = [Song(**row) for row in df.to_dict(orient="records")]
|
||||
if len(songs) == 0:
|
||||
raise HTTPException(status_code=404, detail="No songs found")
|
||||
return songs
|
||||
|
||||
|
||||
@router.get("/{song_id}", response_model=Song)
|
||||
async def get_song(song_id: int):
|
||||
with get_connection() as conn:
|
||||
df = conn.execute(
|
||||
"SELECT Song.*, CountryCodes.name_fi AS country_fi, CountryCodes.name_en AS country_en, CountryCodes.code AS country_code FROM Song, CountryCodes WHERE Song.country = CountryCodes.code AND Song.id = ?",
|
||||
(song_id,),
|
||||
).fetchdf()
|
||||
if df.empty:
|
||||
raise HTTPException(status_code=404, detail="Song not found")
|
||||
song = Song(**df.to_dict(orient="records")[0])
|
||||
return song
|
||||
|
||||
|
||||
@router.post("/")
|
||||
async def create_song():
|
||||
raise HTTPException(status_code=405, detail="Method not yet implemented")
|
||||
@@ -0,0 +1,27 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def list_users():
|
||||
return {"users": []}
|
||||
|
||||
|
||||
@router.get("/{user_id}")
|
||||
async def get_user(user_id: int):
|
||||
return {"user_id": user_id}
|
||||
|
||||
|
||||
# @router.post("/")
|
||||
# async def create_user(user: User):
|
||||
# return {"user": user}
|
||||
|
||||
# @router.patch("/{user_id}")
|
||||
# async def update_user(user_id: int, user: User):
|
||||
# return {"user_id": user_id, "user": user}
|
||||
|
||||
|
||||
@router.delete("/{user_id}")
|
||||
async def delete_user(user_id: int):
|
||||
return {"user_id": user_id}
|
||||
Reference in New Issue
Block a user