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 fastapi import FastAPI
|
||||||
|
from app.api.session import router as session_router
|
||||||
from app.api.tvdb import router as tvdb_router
|
from app.api.tvdb import router as tvdb_router
|
||||||
|
|
||||||
app = FastAPI(title="Rename MVP")
|
app = FastAPI(title="Rename MVP")
|
||||||
|
|
||||||
app.include_router(tvdb_router, prefix="/api/tvdb", tags=["tvdb"])
|
app.include_router(tvdb_router, prefix="/api/tvdb", tags=["tvdb"])
|
||||||
|
app.include_router(session_router, prefix="/api/session", tags=["session"])
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/health")
|
@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)
|
||||||
Executable
+150
@@ -0,0 +1,150 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
if [ -z "${BASE_URL:-}" ]; then
|
||||||
|
if curl --silent --fail http://127.0.0.1:8085/api/health >/dev/null 2>&1; then
|
||||||
|
BASE_URL="http://127.0.0.1:8085"
|
||||||
|
elif curl --silent --fail http://host.containers.internal:8085/api/health >/dev/null 2>&1; then
|
||||||
|
BASE_URL="http://host.containers.internal:8085"
|
||||||
|
else
|
||||||
|
echo "ERROR: could not determine BASE_URL. Tried 127.0.0.1 and host.containers.internal." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
SESSION_ID="selected-episodes-test-$(date +%s)-$$"
|
||||||
|
TMP_DIR="$(mktemp -d)"
|
||||||
|
trap 'rm -rf "$TMP_DIR"' EXIT
|
||||||
|
|
||||||
|
echo "== Feature test 1: add + list selected episodes =="
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X DELETE "${BASE_URL}/api/session/selected-episodes?session_id=${SESSION_ID}" \
|
||||||
|
-o "${TMP_DIR}/clear_before.json"
|
||||||
|
|
||||||
|
cat > "${TMP_DIR}/add_payload.json" <<'JSON'
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"id": 9784113,
|
||||||
|
"season_number": 1,
|
||||||
|
"episode_number": 1,
|
||||||
|
"title": "Pilot",
|
||||||
|
"aired": "2024-02-29",
|
||||||
|
"label": "S01E01 - Pilot - 2024-02-29"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 10347197,
|
||||||
|
"season_number": 1,
|
||||||
|
"episode_number": 2,
|
||||||
|
"title": "A Classic New York Character",
|
||||||
|
"aired": "2024-04-04",
|
||||||
|
"label": "S01E02 - A Classic New York Character - 2024-04-04"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
JSON
|
||||||
|
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X POST "${BASE_URL}/api/session/selected-episodes?session_id=${SESSION_ID}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
--data @"${TMP_DIR}/add_payload.json" \
|
||||||
|
-o "${TMP_DIR}/add_response.json"
|
||||||
|
|
||||||
|
cat "${TMP_DIR}/add_response.json"
|
||||||
|
|
||||||
|
python3 - "${TMP_DIR}/add_response.json" <<'PY'
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||||
|
assert isinstance(data, dict), "response must be an object"
|
||||||
|
assert isinstance(data.get("items"), list), "items must be a list"
|
||||||
|
assert len(data["items"]) == 2, "expected 2 selected episodes"
|
||||||
|
assert data["items"][0]["episode"]["title"] == "Pilot", "first title mismatch"
|
||||||
|
assert data["items"][1]["episode"]["title"] == "A Classic New York Character", "second title mismatch"
|
||||||
|
print("add/list validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "== Feature test 2: reorder selected episodes =="
|
||||||
|
cat > "${TMP_DIR}/reorder_payload.json" <<'JSON'
|
||||||
|
{
|
||||||
|
"from_index": 1,
|
||||||
|
"to_index": 0
|
||||||
|
}
|
||||||
|
JSON
|
||||||
|
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X POST "${BASE_URL}/api/session/selected-episodes/reorder?session_id=${SESSION_ID}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
--data @"${TMP_DIR}/reorder_payload.json" \
|
||||||
|
-o "${TMP_DIR}/reorder_response.json"
|
||||||
|
|
||||||
|
cat "${TMP_DIR}/reorder_response.json"
|
||||||
|
|
||||||
|
python3 - "${TMP_DIR}/reorder_response.json" <<'PY'
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||||
|
items = data.get("items")
|
||||||
|
assert isinstance(items, list), "items must be a list"
|
||||||
|
assert len(items) == 2, "expected 2 selected episodes"
|
||||||
|
assert items[0]["episode"]["title"] == "A Classic New York Character", "reorder did not move expected item"
|
||||||
|
assert items[1]["episode"]["title"] == "Pilot", "reorder did not preserve second item"
|
||||||
|
print("reorder validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "== Feature test 3: remove + clear selected episodes =="
|
||||||
|
python3 - "${TMP_DIR}/reorder_response.json" > "${TMP_DIR}/selection_id.txt" <<'PY'
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||||
|
print(data["items"][0]["selection_id"])
|
||||||
|
PY
|
||||||
|
|
||||||
|
SELECTION_ID="$(cat "${TMP_DIR}/selection_id.txt")"
|
||||||
|
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X DELETE "${BASE_URL}/api/session/selected-episodes/${SELECTION_ID}?session_id=${SESSION_ID}" \
|
||||||
|
-o "${TMP_DIR}/remove_response.json"
|
||||||
|
|
||||||
|
cat "${TMP_DIR}/remove_response.json"
|
||||||
|
|
||||||
|
python3 - "${TMP_DIR}/remove_response.json" <<'PY'
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||||
|
items = data.get("items")
|
||||||
|
assert isinstance(items, list), "items must be a list"
|
||||||
|
assert len(items) == 1, "expected 1 selected episode after remove"
|
||||||
|
print("remove validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X DELETE "${BASE_URL}/api/session/selected-episodes?session_id=${SESSION_ID}" \
|
||||||
|
-o "${TMP_DIR}/clear_response.json"
|
||||||
|
|
||||||
|
cat "${TMP_DIR}/clear_response.json"
|
||||||
|
|
||||||
|
python3 - "${TMP_DIR}/clear_response.json" <<'PY'
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||||
|
items = data.get("items")
|
||||||
|
assert isinstance(items, list), "items must be a list"
|
||||||
|
assert len(items) == 0, "expected empty list after clear"
|
||||||
|
print("clear validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "All selected episodes feature tests passed."
|
||||||
Reference in New Issue
Block a user