feat: add undo for bulk personal actions
CI / test (push) Successful in 35s

This commit is contained in:
strsv committed 2026-11-06 18:24:13 +03:00
1 parent 94cc60b393
commit 1bd4ca8c53
10 files changed
+143 -22

No files matched your search

+47 -3
View File
@@ -517,6 +517,8 @@ class Handler(BaseHTTPRequestHandler):
self._json({"queued": len(ids)})
elif path == "/api/personal/bulk":
self._update_personal_bulk(self._body())
elif path == "/api/personal/restore":
self._restore_personal(self._body())
elif path.startswith("/api/personal/"):
appid = int(path.rsplit("/", 1)[1])
self._update_personal(appid, self._body())
@@ -1074,11 +1076,17 @@ class Handler(BaseHTTPRequestHandler):
con = connect()
try:
placeholders = ",".join("?" * len(appids))
existing = [r["appid"] for r in con.execute(
f"SELECT appid FROM games WHERE appid IN ({placeholders})", appids)]
rows = con.execute(
f"""SELECT g.appid, COALESCE(pe.status, 'backlog') AS status,
COALESCE(pe.favorite, 0) AS favorite
FROM games g LEFT JOIN personal pe ON pe.appid = g.appid
WHERE g.appid IN ({placeholders})""", appids).fetchall()
existing = [r["appid"] for r in rows]
if not existing:
self._error(404, "unknown appids")
return
previous = [{"appid": r["appid"], "status": r["status"],
"favorite": r["favorite"]} for r in rows]
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
con.executemany(
"INSERT OR IGNORE INTO personal (appid, status, updated_at) VALUES (?, 'backlog', ?)",
@@ -1090,7 +1098,43 @@ class Handler(BaseHTTPRequestHandler):
f"WHERE appid IN ({','.join('?' * len(existing))})",
[*vals, *existing])
con.commit()
self._json({"updated": len(existing)})
self._json({"updated": len(existing), "previous": previous})
finally:
con.close()
def _restore_personal(self, body):
items = []
for item in body.get("items") or []:
try:
appid = int(item.get("appid"))
except (AttributeError, TypeError, ValueError):
continue
if item.get("status") not in STATUSES:
continue
items.append((appid, item["status"], 1 if item.get("favorite") else 0))
if not items:
self._error(400, "no items")
return
con = connect()
try:
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
updated = 0
for appid, status, favorite in items:
if not con.execute("SELECT 1 FROM games WHERE appid = ?", (appid,)).fetchone():
continue
con.execute(
"INSERT OR IGNORE INTO personal (appid, status, updated_at) "
"VALUES (?, 'backlog', ?)", (appid, now))
con.execute(
"UPDATE personal SET status = ?, favorite = ?, updated_at = ? "
"WHERE appid = ?", (status, favorite, now, appid))
updated += 1
con.commit()
if not updated:
self._error(404, "unknown appids")
return
self._json({"updated": updated})
finally:
con.close()