feat: Add /shows/recent and /stats/top-songs API endpoints

This commit is contained in:
fullsizemalt 2025-12-20 22:19:12 -08:00
parent 0e71d283c9
commit ffce34f948
3 changed files with 39 additions and 1 deletions

View file

@ -1,5 +1,5 @@
from fastapi import FastAPI
from routers import auth, shows, venues, songs, social, tours, artists, preferences, reviews, badges, nicknames, moderation, attendance, groups, users, search, performances, notifications, feed, leaderboards
from routers import auth, shows, venues, songs, social, tours, artists, preferences, reviews, badges, nicknames, moderation, attendance, groups, users, search, performances, notifications, feed, leaderboards, stats
from fastapi.middleware.cors import CORSMiddleware
@ -33,6 +33,7 @@ app.include_router(performances.router)
app.include_router(notifications.router)
app.include_router(feed.router)
app.include_router(leaderboards.router)
app.include_router(stats.router)
@app.get("/")
def read_root():

View file

@ -37,6 +37,17 @@ def read_shows(
shows = session.exec(query.offset(offset).limit(limit)).all()
return shows
@router.get("/recent", response_model=List[ShowRead])
def read_recent_shows(
limit: int = Query(default=10, le=50),
session: Session = Depends(get_session)
):
"""Get the most recent shows ordered by date descending"""
from datetime import datetime
query = select(Show).where(Show.date <= datetime.now()).order_by(Show.date.desc()).limit(limit)
shows = session.exec(query).all()
return shows
@router.get("/{show_id}", response_model=ShowRead)
def read_show(show_id: int, session: Session = Depends(get_session)):
show = session.get(Show, show_id)

26
backend/routers/stats.py Normal file
View file

@ -0,0 +1,26 @@
from typing import List
from fastapi import APIRouter, Depends, Query
from sqlmodel import Session, select, func
from database import get_session
from models import Song, Performance
router = APIRouter(prefix="/stats", tags=["stats"])
@router.get("/top-songs")
def get_top_songs(
limit: int = Query(default=10, le=50),
session: Session = Depends(get_session)
):
"""Get top songs by number of performances"""
query = (
select(Song.id, Song.title, func.count(Performance.id).label("performance_count"))
.join(Performance, Song.id == Performance.song_id)
.group_by(Song.id, Song.title)
.order_by(func.count(Performance.id).desc())
.limit(limit)
)
results = session.exec(query).all()
return [
{"id": r[0], "title": r[1], "performance_count": r[2]}
for r in results
]