fase 2 afgerond
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.services.session_service import SessionService
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class SelectedEpisodesAddRequest(BaseModel):
|
||||
items: list[dict] = Field(default_factory=list)
|
||||
|
||||
|
||||
class SelectedEpisodesReorderRequest(BaseModel):
|
||||
from_index: int = Field(ge=0)
|
||||
to_index: int = Field(ge=0)
|
||||
|
||||
|
||||
def _normalize_session_id(session_id: str) -> str:
|
||||
normalized = session_id.strip()
|
||||
if not normalized:
|
||||
raise HTTPException(status_code=400, detail="session_id must not be empty")
|
||||
return normalized
|
||||
|
||||
|
||||
@router.get("/selected-episodes")
|
||||
def get_selected_episodes(session_id: str = Query("default", min_length=1)):
|
||||
service = SessionService()
|
||||
normalized_session_id = _normalize_session_id(session_id)
|
||||
items = service.list_selected_episodes(normalized_session_id)
|
||||
return {"session_id": normalized_session_id, "items": items}
|
||||
|
||||
|
||||
@router.post("/selected-episodes")
|
||||
def add_selected_episodes(
|
||||
payload: SelectedEpisodesAddRequest,
|
||||
session_id: str = Query("default", min_length=1),
|
||||
):
|
||||
service = SessionService()
|
||||
normalized_session_id = _normalize_session_id(session_id)
|
||||
items = service.add_selected_episodes(normalized_session_id, payload.items)
|
||||
return {"session_id": normalized_session_id, "items": items}
|
||||
|
||||
|
||||
@router.delete("/selected-episodes")
|
||||
def clear_selected_episodes(session_id: str = Query("default", min_length=1)):
|
||||
service = SessionService()
|
||||
normalized_session_id = _normalize_session_id(session_id)
|
||||
service.clear_selected_episodes(normalized_session_id)
|
||||
return {"session_id": normalized_session_id, "items": []}
|
||||
|
||||
|
||||
@router.delete("/selected-episodes/{selection_id}")
|
||||
def remove_selected_episode(selection_id: int, session_id: str = Query("default", min_length=1)):
|
||||
service = SessionService()
|
||||
normalized_session_id = _normalize_session_id(session_id)
|
||||
items = service.remove_selected_episode(normalized_session_id, selection_id)
|
||||
return {"session_id": normalized_session_id, "items": items}
|
||||
|
||||
|
||||
@router.post("/selected-episodes/reorder")
|
||||
def reorder_selected_episodes(
|
||||
payload: SelectedEpisodesReorderRequest,
|
||||
session_id: str = Query("default", min_length=1),
|
||||
):
|
||||
service = SessionService()
|
||||
normalized_session_id = _normalize_session_id(session_id)
|
||||
try:
|
||||
items = service.reorder_selected_episodes(
|
||||
normalized_session_id,
|
||||
payload.from_index,
|
||||
payload.to_index,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
return {"session_id": normalized_session_id, "items": items}
|
||||
@@ -1,9 +1,11 @@
|
||||
from fastapi import FastAPI
|
||||
from app.api.session import router as session_router
|
||||
from app.api.tvdb import router as tvdb_router
|
||||
|
||||
app = FastAPI(title="Rename MVP")
|
||||
|
||||
app.include_router(tvdb_router, prefix="/api/tvdb", tags=["tvdb"])
|
||||
app.include_router(session_router, prefix="/api/session", tags=["session"])
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import json
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
from app.config import APP_DATA_DIR
|
||||
|
||||
|
||||
class SessionService:
|
||||
def __init__(self) -> None:
|
||||
self._db_path = Path(APP_DATA_DIR) / "session_state.sqlite3"
|
||||
self._db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self._init_db()
|
||||
|
||||
def _connect(self) -> sqlite3.Connection:
|
||||
conn = sqlite3.connect(self._db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
def _init_db(self) -> None:
|
||||
with self._connect() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS selected_episodes (
|
||||
selection_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
position INTEGER NOT NULL,
|
||||
payload_json TEXT NOT NULL
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_selected_episodes_session_position
|
||||
ON selected_episodes(session_id, position)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_selected_episodes_session
|
||||
ON selected_episodes(session_id)
|
||||
"""
|
||||
)
|
||||
|
||||
def list_selected_episodes(self, session_id: str) -> list[dict]:
|
||||
with self._connect() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT selection_id, position, payload_json
|
||||
FROM selected_episodes
|
||||
WHERE session_id = ?
|
||||
ORDER BY position ASC
|
||||
""",
|
||||
(session_id,),
|
||||
).fetchall()
|
||||
|
||||
items = []
|
||||
for row in rows:
|
||||
payload = json.loads(row["payload_json"])
|
||||
items.append(
|
||||
{
|
||||
"selection_id": row["selection_id"],
|
||||
"position": row["position"],
|
||||
"episode": payload,
|
||||
}
|
||||
)
|
||||
return items
|
||||
|
||||
def add_selected_episodes(self, session_id: str, items: list[dict]) -> list[dict]:
|
||||
if not items:
|
||||
return self.list_selected_episodes(session_id)
|
||||
|
||||
with self._connect() as conn:
|
||||
current_max = conn.execute(
|
||||
"""
|
||||
SELECT COALESCE(MAX(position), -1) AS max_position
|
||||
FROM selected_episodes
|
||||
WHERE session_id = ?
|
||||
""",
|
||||
(session_id,),
|
||||
).fetchone()
|
||||
next_position = int(current_max["max_position"]) + 1
|
||||
|
||||
for item in items:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO selected_episodes (session_id, position, payload_json)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(session_id, next_position, json.dumps(item, ensure_ascii=True)),
|
||||
)
|
||||
next_position += 1
|
||||
|
||||
return self.list_selected_episodes(session_id)
|
||||
|
||||
def clear_selected_episodes(self, session_id: str) -> None:
|
||||
with self._connect() as conn:
|
||||
conn.execute(
|
||||
"DELETE FROM selected_episodes WHERE session_id = ?",
|
||||
(session_id,),
|
||||
)
|
||||
|
||||
def remove_selected_episode(self, session_id: str, selection_id: int) -> list[dict]:
|
||||
with self._connect() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
DELETE FROM selected_episodes
|
||||
WHERE session_id = ? AND selection_id = ?
|
||||
""",
|
||||
(session_id, selection_id),
|
||||
)
|
||||
return self._compact_positions(session_id)
|
||||
|
||||
def reorder_selected_episodes(
|
||||
self,
|
||||
session_id: str,
|
||||
from_index: int,
|
||||
to_index: int,
|
||||
) -> list[dict]:
|
||||
current_items = self.list_selected_episodes(session_id)
|
||||
|
||||
if from_index < 0 or from_index >= len(current_items):
|
||||
raise ValueError("from_index out of range")
|
||||
if to_index < 0 or to_index >= len(current_items):
|
||||
raise ValueError("to_index out of range")
|
||||
if from_index == to_index:
|
||||
return current_items
|
||||
|
||||
moved = current_items.pop(from_index)
|
||||
current_items.insert(to_index, moved)
|
||||
|
||||
with self._connect() as conn:
|
||||
# Two-phase update avoids transient UNIQUE conflicts on (session_id, position).
|
||||
for position, item in enumerate(current_items):
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE selected_episodes
|
||||
SET position = ?
|
||||
WHERE session_id = ? AND selection_id = ?
|
||||
""",
|
||||
(-(position + 1), session_id, item["selection_id"]),
|
||||
)
|
||||
|
||||
for position, item in enumerate(current_items):
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE selected_episodes
|
||||
SET position = ?
|
||||
WHERE session_id = ? AND selection_id = ?
|
||||
""",
|
||||
(position, session_id, item["selection_id"]),
|
||||
)
|
||||
|
||||
return self.list_selected_episodes(session_id)
|
||||
|
||||
def _compact_positions(self, session_id: str) -> list[dict]:
|
||||
items = self.list_selected_episodes(session_id)
|
||||
with self._connect() as conn:
|
||||
for position, item in enumerate(items):
|
||||
if item["position"] == position:
|
||||
continue
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE selected_episodes
|
||||
SET position = ?
|
||||
WHERE session_id = ? AND selection_id = ?
|
||||
""",
|
||||
(position, session_id, item["selection_id"]),
|
||||
)
|
||||
return self.list_selected_episodes(session_id)
|
||||
Reference in New Issue
Block a user