fase 3 afgerond

This commit is contained in:
kodi
2026-03-07 14:15:14 +01:00
parent d34b6236bd
commit aa2d0c9bac
3 changed files with 354 additions and 0 deletions
+63
View File
@@ -15,6 +15,15 @@ class SelectedEpisodesReorderRequest(BaseModel):
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:
normalized = session_id.strip()
if not normalized:
@@ -74,3 +83,57 @@ def reorder_selected_episodes(
raise HTTPException(status_code=400, detail=str(exc))
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}
+149
View File
@@ -40,6 +40,28 @@ class SessionService:
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]:
with self._connect() as conn:
@@ -167,3 +189,130 @@ class SessionService:
(position, session_id, item["selection_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)