init
This commit is contained in:
0
backend/app/api/__init__.py
Normal file
0
backend/app/api/__init__.py
Normal file
35
backend/app/api/admin.py
Normal file
35
backend/app/api/admin.py
Normal file
@@ -0,0 +1,35 @@
|
||||
"""Admin API endpoints"""
|
||||
from fastapi import APIRouter, HTTPException, Header
|
||||
from typing import Optional
|
||||
from app.core.database import pool
|
||||
from app.core.config import settings
|
||||
from app.services.sync import sync_all_arrs
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
async def verify_admin_token(authorization: Optional[str] = Header(None)):
|
||||
"""Verify admin token if configured"""
|
||||
if settings.admin_token:
|
||||
if not authorization or authorization != f"Bearer {settings.admin_token}":
|
||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
# If no admin token configured, allow (assuming localhost-only access)
|
||||
|
||||
|
||||
@router.post("/sync")
|
||||
async def trigger_sync(authorization: Optional[str] = Header(None)):
|
||||
"""
|
||||
Trigger sync from all *arr instances.
|
||||
Requires admin token if MOVIEMAP_ADMIN_TOKEN is set.
|
||||
"""
|
||||
await verify_admin_token(authorization)
|
||||
|
||||
try:
|
||||
result = await sync_all_arrs()
|
||||
return {
|
||||
"status": "success",
|
||||
"synced": result
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Sync failed: {str(e)}")
|
||||
|
||||
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
|
||||
|
||||
87
backend/app/api/pins.py
Normal file
87
backend/app/api/pins.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""Manual pins API endpoints"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
from app.core.database import pool
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class PinCreate(BaseModel):
|
||||
country_code: str
|
||||
label: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_pins():
|
||||
"""List all manual pins"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = """
|
||||
SELECT id, country_code, label, pinned_at
|
||||
FROM moviemap.manual_pin
|
||||
ORDER BY pinned_at DESC
|
||||
"""
|
||||
await cur.execute(query)
|
||||
rows = await cur.fetchall()
|
||||
|
||||
pins = []
|
||||
for row in rows:
|
||||
pins.append({
|
||||
"id": str(row[0]),
|
||||
"country_code": row[1],
|
||||
"label": row[2],
|
||||
"pinned_at": row[3].isoformat() if row[3] else None,
|
||||
})
|
||||
|
||||
return pins
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_pin(pin: PinCreate):
|
||||
"""Create a new manual pin"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = """
|
||||
INSERT INTO moviemap.manual_pin (country_code, label)
|
||||
VALUES (%s, %s)
|
||||
RETURNING id
|
||||
"""
|
||||
await cur.execute(query, (pin.country_code, pin.label))
|
||||
result = await cur.fetchone()
|
||||
await conn.commit()
|
||||
|
||||
return {"id": str(result[0]), "status": "created"}
|
||||
|
||||
|
||||
@router.delete("/{pin_id}")
|
||||
async def delete_pin(pin_id: UUID):
|
||||
"""Delete a manual pin"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = "DELETE FROM moviemap.manual_pin WHERE id = %s RETURNING id"
|
||||
await cur.execute(query, (str(pin_id),))
|
||||
result = await cur.fetchone()
|
||||
await conn.commit()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Pin not found")
|
||||
|
||||
return {"id": str(result[0]), "status": "deleted"}
|
||||
|
||||
208
backend/app/api/watched.py
Normal file
208
backend/app/api/watched.py
Normal file
@@ -0,0 +1,208 @@
|
||||
"""Watched items API endpoints"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
from app.core.database import pool
|
||||
import json
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class WatchedItemCreate(BaseModel):
|
||||
media_type: str # "movie" or "show"
|
||||
title: str
|
||||
year: Optional[int] = None
|
||||
country_code: str
|
||||
watched_at: Optional[datetime] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
class WatchedItemUpdate(BaseModel):
|
||||
title: Optional[str] = None
|
||||
year: Optional[int] = None
|
||||
country_code: Optional[str] = None
|
||||
watched_at: Optional[datetime] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/summary")
|
||||
async def get_watched_summary():
|
||||
"""Get watched items summary by country"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = """
|
||||
SELECT
|
||||
country_code,
|
||||
media_type,
|
||||
COUNT(*) as count
|
||||
FROM moviemap.watched_item
|
||||
WHERE watched_at IS NOT NULL
|
||||
GROUP BY country_code, media_type
|
||||
ORDER BY country_code, media_type
|
||||
"""
|
||||
await cur.execute(query)
|
||||
rows = await cur.fetchall()
|
||||
|
||||
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
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_watched_items():
|
||||
"""List all watched items"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = """
|
||||
SELECT
|
||||
id, media_type, title, year, country_code,
|
||||
watched_at, notes, created_at, updated_at
|
||||
FROM moviemap.watched_item
|
||||
ORDER BY created_at DESC
|
||||
"""
|
||||
await cur.execute(query)
|
||||
rows = await cur.fetchall()
|
||||
|
||||
items = []
|
||||
for row in rows:
|
||||
items.append({
|
||||
"id": str(row[0]),
|
||||
"media_type": row[1],
|
||||
"title": row[2],
|
||||
"year": row[3],
|
||||
"country_code": row[4],
|
||||
"watched_at": row[5].isoformat() if row[5] else None,
|
||||
"notes": row[6],
|
||||
"created_at": row[7].isoformat() if row[7] else None,
|
||||
"updated_at": row[8].isoformat() if row[8] else None,
|
||||
})
|
||||
|
||||
return items
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_watched_item(item: WatchedItemCreate):
|
||||
"""Create a new watched item"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
if item.media_type not in ["movie", "show"]:
|
||||
raise HTTPException(status_code=400, detail="media_type must be 'movie' or 'show'")
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = """
|
||||
INSERT INTO moviemap.watched_item
|
||||
(media_type, title, year, country_code, watched_at, notes)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
"""
|
||||
await cur.execute(
|
||||
query,
|
||||
(
|
||||
item.media_type,
|
||||
item.title,
|
||||
item.year,
|
||||
item.country_code,
|
||||
item.watched_at,
|
||||
item.notes,
|
||||
)
|
||||
)
|
||||
result = await cur.fetchone()
|
||||
await conn.commit()
|
||||
|
||||
return {"id": str(result[0]), "status": "created"}
|
||||
|
||||
|
||||
@router.patch("/{item_id}")
|
||||
async def update_watched_item(item_id: UUID, item: WatchedItemUpdate):
|
||||
"""Update a watched item"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Build dynamic update query
|
||||
updates = []
|
||||
params = []
|
||||
|
||||
if item.title is not None:
|
||||
updates.append("title = %s")
|
||||
params.append(item.title)
|
||||
if item.year is not None:
|
||||
updates.append("year = %s")
|
||||
params.append(item.year)
|
||||
if item.country_code is not None:
|
||||
updates.append("country_code = %s")
|
||||
params.append(item.country_code)
|
||||
if item.watched_at is not None:
|
||||
updates.append("watched_at = %s")
|
||||
params.append(item.watched_at)
|
||||
if item.notes is not None:
|
||||
updates.append("notes = %s")
|
||||
params.append(item.notes)
|
||||
|
||||
if not updates:
|
||||
raise HTTPException(status_code=400, detail="No fields to update")
|
||||
|
||||
updates.append("updated_at = NOW()")
|
||||
params.append(str(item_id))
|
||||
|
||||
query = f"""
|
||||
UPDATE moviemap.watched_item
|
||||
SET {', '.join(updates)}
|
||||
WHERE id = %s
|
||||
RETURNING id
|
||||
"""
|
||||
|
||||
await cur.execute(query, params)
|
||||
result = await cur.fetchone()
|
||||
await conn.commit()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Watched item not found")
|
||||
|
||||
return {"id": str(result[0]), "status": "updated"}
|
||||
|
||||
|
||||
@router.delete("/{item_id}")
|
||||
async def delete_watched_item(item_id: UUID):
|
||||
"""Delete a watched item"""
|
||||
# Pool should be initialized on startup
|
||||
if not pool:
|
||||
from app.core.database import init_db
|
||||
await init_db()
|
||||
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
query = "DELETE FROM moviemap.watched_item WHERE id = %s RETURNING id"
|
||||
await cur.execute(query, (str(item_id),))
|
||||
result = await cur.fetchone()
|
||||
await conn.commit()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Watched item not found")
|
||||
|
||||
return {"id": str(result[0]), "status": "deleted"}
|
||||
|
||||
Reference in New Issue
Block a user