from fastapi import FastAPI, Depends from sqlalchemy.orm import Session from .database import engine, Base, get_db from .models import PlayHistory as PlayHistoryModel, Track as TrackModel from . import schemas from .ingest import ingest_recently_played import asyncio from typing import List from dotenv import load_dotenv load_dotenv() # Create tables Base.metadata.create_all(bind=engine) app = FastAPI(title="Music Analyser Backend") @app.get("/") def read_root(): return {"status": "ok", "message": "Music Analyser API is running"} @app.get("/history", response_model=List[schemas.PlayHistory]) def get_history(limit: int = 50, db: Session = Depends(get_db)): history = db.query(PlayHistoryModel).order_by(PlayHistoryModel.played_at.desc()).limit(limit).all() return history @app.post("/trigger-ingest") async def trigger_ingest(db: Session = Depends(get_db)): """Manually trigger the ingestion process (useful for testing)""" await ingest_recently_played(db) return {"status": "Ingestion triggered"} @app.get("/tracks", response_model=List[schemas.Track]) def get_tracks(limit: int = 50, db: Session = Depends(get_db)): tracks = db.query(TrackModel).limit(limit).all() return tracks