init
This commit is contained in:
61
backend/app/api/collection.py
Normal file
61
backend/app/api/collection.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""Collection API endpoints"""
|
||||
from fastapi import APIRouter, Query
|
||||
from typing import List, Optional
|
||||
from app.core.database import pool
|
||||
import json
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/summary")
|
||||
async def get_collection_summary(
|
||||
types: Optional[str] = Query(None, description="Comma-separated list: movie,show,music")
|
||||
):
|
||||
"""
|
||||
Get collection summary by country and media type.
|
||||
Returns counts per country per media type.
|
||||
"""
|
||||
# Pool should be initialized on startup, but check just in case
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
# Parse types filter
|
||||
type_filter = []
|
||||
if types:
|
||||
type_filter = [t.strip() for t in types.split(",") if t.strip() in ["movie", "show", "music"]]
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Build query
|
||||
query = """
|
||||
SELECT
|
||||
mc.country_code,
|
||||
mi.media_type,
|
||||
COUNT(*) as count
|
||||
FROM moviemap.media_country mc
|
||||
JOIN moviemap.media_item mi ON mc.media_item_id = mi.id
|
||||
"""
|
||||
params = []
|
||||
if type_filter:
|
||||
query += " WHERE mi.media_type = ANY(%s)"
|
||||
params.append(type_filter)
|
||||
|
||||
query += """
|
||||
GROUP BY mc.country_code, mi.media_type
|
||||
ORDER BY mc.country_code, mi.media_type
|
||||
"""
|
||||
|
||||
await cur.execute(query, params if params else None)
|
||||
rows = await cur.fetchall()
|
||||
|
||||
# Transform to nested dict structure
|
||||
result = {}
|
||||
for row in rows:
|
||||
country_code, media_type, count = row
|
||||
if country_code not in result:
|
||||
result[country_code] = {}
|
||||
result[country_code][media_type] = count
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user