fase 3 afgerond
This commit is contained in:
@@ -15,6 +15,15 @@ class SelectedEpisodesReorderRequest(BaseModel):
|
|||||||
to_index: int = Field(ge=0)
|
to_index: int = Field(ge=0)
|
||||||
|
|
||||||
|
|
||||||
|
class SelectedFilesAddRequest(BaseModel):
|
||||||
|
items: list[dict] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
class SelectedFilesReorderRequest(BaseModel):
|
||||||
|
from_index: int = Field(ge=0)
|
||||||
|
to_index: int = Field(ge=0)
|
||||||
|
|
||||||
|
|
||||||
def _normalize_session_id(session_id: str) -> str:
|
def _normalize_session_id(session_id: str) -> str:
|
||||||
normalized = session_id.strip()
|
normalized = session_id.strip()
|
||||||
if not normalized:
|
if not normalized:
|
||||||
@@ -74,3 +83,57 @@ def reorder_selected_episodes(
|
|||||||
raise HTTPException(status_code=400, detail=str(exc))
|
raise HTTPException(status_code=400, detail=str(exc))
|
||||||
|
|
||||||
return {"session_id": normalized_session_id, "items": items}
|
return {"session_id": normalized_session_id, "items": items}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/selected-files")
|
||||||
|
def get_selected_files(session_id: str = Query("default", min_length=1)):
|
||||||
|
service = SessionService()
|
||||||
|
normalized_session_id = _normalize_session_id(session_id)
|
||||||
|
items = service.list_selected_files(normalized_session_id)
|
||||||
|
return {"session_id": normalized_session_id, "items": items}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/selected-files")
|
||||||
|
def add_selected_files(
|
||||||
|
payload: SelectedFilesAddRequest,
|
||||||
|
session_id: str = Query("default", min_length=1),
|
||||||
|
):
|
||||||
|
service = SessionService()
|
||||||
|
normalized_session_id = _normalize_session_id(session_id)
|
||||||
|
items = service.add_selected_files(normalized_session_id, payload.items)
|
||||||
|
return {"session_id": normalized_session_id, "items": items}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/selected-files")
|
||||||
|
def clear_selected_files(session_id: str = Query("default", min_length=1)):
|
||||||
|
service = SessionService()
|
||||||
|
normalized_session_id = _normalize_session_id(session_id)
|
||||||
|
service.clear_selected_files(normalized_session_id)
|
||||||
|
return {"session_id": normalized_session_id, "items": []}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/selected-files/{selection_id}")
|
||||||
|
def remove_selected_file(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_file(normalized_session_id, selection_id)
|
||||||
|
return {"session_id": normalized_session_id, "items": items}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/selected-files/reorder")
|
||||||
|
def reorder_selected_files(
|
||||||
|
payload: SelectedFilesReorderRequest,
|
||||||
|
session_id: str = Query("default", min_length=1),
|
||||||
|
):
|
||||||
|
service = SessionService()
|
||||||
|
normalized_session_id = _normalize_session_id(session_id)
|
||||||
|
try:
|
||||||
|
items = service.reorder_selected_files(
|
||||||
|
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}
|
||||||
|
|||||||
@@ -40,6 +40,28 @@ class SessionService:
|
|||||||
ON selected_episodes(session_id)
|
ON selected_episodes(session_id)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS selected_files (
|
||||||
|
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_files_session_position
|
||||||
|
ON selected_files(session_id, position)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_selected_files_session
|
||||||
|
ON selected_files(session_id)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
def list_selected_episodes(self, session_id: str) -> list[dict]:
|
def list_selected_episodes(self, session_id: str) -> list[dict]:
|
||||||
with self._connect() as conn:
|
with self._connect() as conn:
|
||||||
@@ -167,3 +189,130 @@ class SessionService:
|
|||||||
(position, session_id, item["selection_id"]),
|
(position, session_id, item["selection_id"]),
|
||||||
)
|
)
|
||||||
return self.list_selected_episodes(session_id)
|
return self.list_selected_episodes(session_id)
|
||||||
|
|
||||||
|
def list_selected_files(self, session_id: str) -> list[dict]:
|
||||||
|
with self._connect() as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT selection_id, position, payload_json
|
||||||
|
FROM selected_files
|
||||||
|
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"],
|
||||||
|
"file": payload,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return items
|
||||||
|
|
||||||
|
def add_selected_files(self, session_id: str, items: list[dict]) -> list[dict]:
|
||||||
|
if not items:
|
||||||
|
return self.list_selected_files(session_id)
|
||||||
|
|
||||||
|
with self._connect() as conn:
|
||||||
|
current_max = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT COALESCE(MAX(position), -1) AS max_position
|
||||||
|
FROM selected_files
|
||||||
|
WHERE session_id = ?
|
||||||
|
""",
|
||||||
|
(session_id,),
|
||||||
|
).fetchone()
|
||||||
|
next_position = int(current_max["max_position"]) + 1
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO selected_files (session_id, position, payload_json)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
""",
|
||||||
|
(session_id, next_position, json.dumps(item, ensure_ascii=True)),
|
||||||
|
)
|
||||||
|
next_position += 1
|
||||||
|
|
||||||
|
return self.list_selected_files(session_id)
|
||||||
|
|
||||||
|
def clear_selected_files(self, session_id: str) -> None:
|
||||||
|
with self._connect() as conn:
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM selected_files WHERE session_id = ?",
|
||||||
|
(session_id,),
|
||||||
|
)
|
||||||
|
|
||||||
|
def remove_selected_file(self, session_id: str, selection_id: int) -> list[dict]:
|
||||||
|
with self._connect() as conn:
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
DELETE FROM selected_files
|
||||||
|
WHERE session_id = ? AND selection_id = ?
|
||||||
|
""",
|
||||||
|
(session_id, selection_id),
|
||||||
|
)
|
||||||
|
return self._compact_file_positions(session_id)
|
||||||
|
|
||||||
|
def reorder_selected_files(
|
||||||
|
self,
|
||||||
|
session_id: str,
|
||||||
|
from_index: int,
|
||||||
|
to_index: int,
|
||||||
|
) -> list[dict]:
|
||||||
|
current_items = self.list_selected_files(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_files
|
||||||
|
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_files
|
||||||
|
SET position = ?
|
||||||
|
WHERE session_id = ? AND selection_id = ?
|
||||||
|
""",
|
||||||
|
(position, session_id, item["selection_id"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.list_selected_files(session_id)
|
||||||
|
|
||||||
|
def _compact_file_positions(self, session_id: str) -> list[dict]:
|
||||||
|
items = self.list_selected_files(session_id)
|
||||||
|
with self._connect() as conn:
|
||||||
|
for position, item in enumerate(items):
|
||||||
|
if item["position"] == position:
|
||||||
|
continue
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
UPDATE selected_files
|
||||||
|
SET position = ?
|
||||||
|
WHERE session_id = ? AND selection_id = ?
|
||||||
|
""",
|
||||||
|
(position, session_id, item["selection_id"]),
|
||||||
|
)
|
||||||
|
return self.list_selected_files(session_id)
|
||||||
|
|||||||
Executable
+142
@@ -0,0 +1,142 @@
|
|||||||
|
#!/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-files-test-$(date +%s)-$$"
|
||||||
|
TMP_DIR="$(mktemp -d)"
|
||||||
|
trap 'rm -rf "$TMP_DIR"' EXIT
|
||||||
|
|
||||||
|
echo "== Feature test 1: add + list selected files =="
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X DELETE "${BASE_URL}/api/session/selected-files?session_id=${SESSION_ID}" \
|
||||||
|
-o "${TMP_DIR}/clear_before.json"
|
||||||
|
|
||||||
|
cat > "${TMP_DIR}/add_payload.json" <<'JSON'
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"path": "/Volumes/8TB/Shared_Folders/TV_Shows/Elsbeth/episode1.mkv",
|
||||||
|
"name": "episode1.mkv"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/Volumes/8TB/Shared_Folders/TV_Shows/Elsbeth/episode2.mkv",
|
||||||
|
"name": "episode2.mkv"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
JSON
|
||||||
|
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X POST "${BASE_URL}/api/session/selected-files?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 files"
|
||||||
|
assert data["items"][0]["file"]["name"] == "episode1.mkv", "first file mismatch"
|
||||||
|
assert data["items"][1]["file"]["name"] == "episode2.mkv", "second file mismatch"
|
||||||
|
print("add/list validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "== Feature test 2: reorder selected files =="
|
||||||
|
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-files/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 files"
|
||||||
|
assert items[0]["file"]["name"] == "episode2.mkv", "reorder did not move expected file"
|
||||||
|
assert items[1]["file"]["name"] == "episode1.mkv", "reorder did not preserve second file"
|
||||||
|
print("reorder validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "== Feature test 3: remove + clear selected files =="
|
||||||
|
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-files/${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 file after remove"
|
||||||
|
print("remove validation passed")
|
||||||
|
PY
|
||||||
|
|
||||||
|
curl --fail --silent --show-error \
|
||||||
|
-X DELETE "${BASE_URL}/api/session/selected-files?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 files feature tests passed."
|
||||||
Reference in New Issue
Block a user