diff --git a/backend/main.py b/backend/main.py index 5b363c9..7560c23 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,6 +1,6 @@ from fastapi import FastAPI import os -from routers import auth, shows, venues, songs, social, tours, artists, preferences, reviews, badges, nicknames, moderation, attendance, groups, users, search, performances, notifications, feed, leaderboards, stats, admin, chase, gamification, videos, musicians, sequences, verticals, canon +from routers import auth, shows, venues, songs, social, tours, artists, preferences, reviews, badges, nicknames, moderation, attendance, groups, users, search, performances, notifications, feed, leaderboards, stats, admin, chase, gamification, videos, musicians, sequences, verticals, canon, on_this_day from fastapi.middleware.cors import CORSMiddleware @@ -46,6 +46,7 @@ app.include_router(musicians.router) app.include_router(sequences.router) app.include_router(verticals.router) app.include_router(canon.router) +app.include_router(on_this_day.router) # Optional features - can be disabled via env vars diff --git a/backend/routers/on_this_day.py b/backend/routers/on_this_day.py new file mode 100644 index 0000000..3fb3d2e --- /dev/null +++ b/backend/routers/on_this_day.py @@ -0,0 +1,125 @@ +""" +On This Day API endpoint - shows what happened on this date in history. +""" +from datetime import date +from typing import List, Optional +from fastapi import APIRouter, Depends, Query +from sqlmodel import Session, select +from pydantic import BaseModel +from database import get_session +from models import Show, Venue, Vertical, Performance, Song + +router = APIRouter(prefix="/on-this-day", tags=["on-this-day"]) + + +class ShowOnThisDay(BaseModel): + id: int + date: str + slug: str | None + venue_name: str | None + venue_city: str | None + vertical_name: str + vertical_slug: str + years_ago: int + + +class OnThisDayResponse(BaseModel): + month: int + day: int + shows: List[ShowOnThisDay] + total_shows: int + + +@router.get("/", response_model=OnThisDayResponse) +def get_on_this_day( + month: Optional[int] = None, + day: Optional[int] = None, + vertical: Optional[str] = None, + session: Session = Depends(get_session) +): + """ + Get all shows that happened on this day in history. + Defaults to today's date if month/day not specified. + """ + today = date.today() + target_month = month or today.month + target_day = day or today.day + + # Build query + query = select(Show).where( + Show.date.isnot(None) + ) + + # Filter by vertical if specified + if vertical: + vertical_obj = session.exec( + select(Vertical).where(Vertical.slug == vertical) + ).first() + if vertical_obj: + query = query.where(Show.vertical_id == vertical_obj.id) + + # Execute and filter by month/day in Python (SQL date functions vary) + all_shows = session.exec(query.order_by(Show.date.desc())).all() + + matching_shows = [] + for show in all_shows: + if show.date and show.date.month == target_month and show.date.day == target_day: + venue = session.get(Venue, show.venue_id) if show.venue_id else None + vert = session.get(Vertical, show.vertical_id) + + years_ago = today.year - show.date.year + + matching_shows.append(ShowOnThisDay( + id=show.id, + date=show.date.strftime("%Y-%m-%d"), + slug=show.slug, + venue_name=venue.name if venue else None, + venue_city=venue.city if venue else None, + vertical_name=vert.name if vert else "Unknown", + vertical_slug=vert.slug if vert else "unknown", + years_ago=years_ago + )) + + # Sort by years ago (most recent anniversary first) + matching_shows.sort(key=lambda x: x.years_ago) + + return OnThisDayResponse( + month=target_month, + day=target_day, + shows=matching_shows, + total_shows=len(matching_shows) + ) + + +@router.get("/highlights", response_model=List[ShowOnThisDay]) +def get_on_this_day_highlights( + limit: int = 5, + session: Session = Depends(get_session) +): + """Get highlighted shows for today across all bands (limited list for homepage)""" + today = date.today() + + all_shows = session.exec( + select(Show).where(Show.date.isnot(None)) + ).all() + + matching = [] + for show in all_shows: + if show.date and show.date.month == today.month and show.date.day == today.day: + venue = session.get(Venue, show.venue_id) if show.venue_id else None + vert = session.get(Vertical, show.vertical_id) + + matching.append(ShowOnThisDay( + id=show.id, + date=show.date.strftime("%Y-%m-%d"), + slug=show.slug, + venue_name=venue.name if venue else None, + venue_city=venue.city if venue else None, + vertical_name=vert.name if vert else "Unknown", + vertical_slug=vert.slug if vert else "unknown", + years_ago=today.year - show.date.year + )) + + # Sort by anniversary and return limited + matching.sort(key=lambda x: x.years_ago) + return matching[:limit]