delete games
This commit is contained in:
+119
-39
@@ -82,49 +82,40 @@ function getDb(): PDO {
|
||||
// Shared game helpers (usable for both GET and POST actions)
|
||||
function saveGameStatus(int $igdbId, string $name, ?string $originalName, int $status, ?string $note = null, ?string $coverUrl = null): array {
|
||||
$pdo = getDb();
|
||||
static $hasCover = null;
|
||||
if ($hasCover === null) {
|
||||
try {
|
||||
$pdo->query("SHOW COLUMNS FROM game LIKE 'cover_url'");
|
||||
$hasCover = true;
|
||||
} catch (Throwable $e) {
|
||||
$hasCover = false;
|
||||
}
|
||||
$hasCover = gameHasColumn('cover_url');
|
||||
$hasReleaseYear = gameHasColumn('release_year');
|
||||
$releaseYear = $GLOBALS['__release_year'] ?? null;
|
||||
|
||||
$fields = ['igdb_id', 'name', 'original_name', 'status', 'note'];
|
||||
if ($hasCover) $fields[] = 'cover_url';
|
||||
if ($hasReleaseYear) $fields[] = 'release_year';
|
||||
|
||||
$placeholders = array_map(fn($f) => ':'.$f, $fields);
|
||||
$updates = [];
|
||||
foreach ($fields as $f) {
|
||||
if ($f === 'igdb_id') continue;
|
||||
$updates[] = "$f = VALUES($f)";
|
||||
}
|
||||
|
||||
$sql = $hasCover
|
||||
? '
|
||||
INSERT INTO game (igdb_id, name, original_name, status, note, cover_url)
|
||||
VALUES (:igdb_id, :name, :original_name, :status, :note, :cover_url)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
name = VALUES(name),
|
||||
original_name = VALUES(original_name),
|
||||
status = VALUES(status),
|
||||
note = VALUES(note),
|
||||
cover_url = VALUES(cover_url),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
'
|
||||
: '
|
||||
INSERT INTO game (igdb_id, name, original_name, status, note)
|
||||
VALUES (:igdb_id, :name, :original_name, :status, :note)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
name = VALUES(name),
|
||||
original_name = VALUES(original_name),
|
||||
status = VALUES(status),
|
||||
note = VALUES(note),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
';
|
||||
$sql = sprintf(
|
||||
'INSERT INTO game (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s',
|
||||
implode(', ', $fields),
|
||||
implode(', ', $placeholders),
|
||||
implode(', ', $updates) . ', updated_at = CURRENT_TIMESTAMP'
|
||||
);
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$params = [
|
||||
':igdb_id' => $igdbId,
|
||||
':name' => $name,
|
||||
':original_name' => $originalName,
|
||||
':status' => $status,
|
||||
':note' => $note,
|
||||
];
|
||||
if ($hasCover) {
|
||||
$params[':cover_url'] = $coverUrl;
|
||||
$params = [];
|
||||
foreach ($fields as $f) {
|
||||
switch ($f) {
|
||||
case 'igdb_id': $params[':igdb_id'] = $igdbId; break;
|
||||
case 'name': $params[':name'] = $name; break;
|
||||
case 'original_name': $params[':original_name'] = $originalName; break;
|
||||
case 'status': $params[':status'] = $status; break;
|
||||
case 'note': $params[':note'] = $note; break;
|
||||
case 'cover_url': $params[':cover_url'] = $coverUrl; break;
|
||||
case 'release_year': $params[':release_year'] = $releaseYear; break;
|
||||
}
|
||||
}
|
||||
$stmt->execute($params);
|
||||
$stmt = $pdo->prepare('SELECT * FROM game WHERE igdb_id = :id LIMIT 1');
|
||||
@@ -139,6 +130,26 @@ function getGameByIgdbId(int $igdbId): ?array {
|
||||
return $stmt->fetch() ?: null;
|
||||
}
|
||||
|
||||
function gameColumns(): array {
|
||||
static $cols = null;
|
||||
if ($cols !== null) return $cols;
|
||||
$cols = [];
|
||||
try {
|
||||
$pdo = getDb();
|
||||
$stmt = $pdo->query('DESCRIBE game');
|
||||
foreach ($stmt as $row) {
|
||||
if (isset($row['Field'])) $cols[] = $row['Field'];
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$cols = [];
|
||||
}
|
||||
return $cols;
|
||||
}
|
||||
|
||||
function gameHasColumn(string $col): bool {
|
||||
return in_array($col, gameColumns(), true);
|
||||
}
|
||||
|
||||
function getLocalization(int $igdbId, string $lang = 'de'): ?array {
|
||||
$pdo = getDb();
|
||||
$stmt = $pdo->prepare('SELECT * FROM igdb_localizations WHERE igdb_id = :id AND lang = :lang LIMIT 1');
|
||||
@@ -563,6 +574,8 @@ if (isset($_GET['action'])) {
|
||||
$status = (int)($data['status'] ?? 0);
|
||||
$note = isset($data['note']) ? trim($data['note']) : '';
|
||||
$coverUrl = isset($data['cover_url']) ? trim($data['cover_url']) : null;
|
||||
$releaseYear = isset($data['release_year']) ? (int)$data['release_year'] : null;
|
||||
$GLOBALS['__release_year'] = $releaseYear;
|
||||
if ($igdbId <= 0) error_response('Invalid \"igdb_id\"', 400);
|
||||
if ($name === '') error_response('Missing \"name\"', 400);
|
||||
if ($status < 0 || $status > 2) error_response('Invalid \"status\" (0, 1, 2 allowed)', 400);
|
||||
@@ -570,6 +583,27 @@ if (isset($_GET['action'])) {
|
||||
respond(['success' => true, 'game' => $game]);
|
||||
break;
|
||||
|
||||
case 'delete_game':
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') error_response('Use POST', 405);
|
||||
$igdbId = (int)($in['igdb_id'] ?? 0);
|
||||
if ($igdbId <= 0) fail('Invalid \"igdb_id\"');
|
||||
$pdo->beginTransaction();
|
||||
try {
|
||||
// remove localizations first
|
||||
$stmt = $pdo->prepare('DELETE FROM igdb_localizations WHERE igdb_id = ?');
|
||||
$stmt->execute([$igdbId]);
|
||||
// remove game entry
|
||||
$stmt = $pdo->prepare('DELETE FROM game WHERE igdb_id = ?');
|
||||
$stmt->execute([$igdbId]);
|
||||
$pdo->commit();
|
||||
resp(['ok' => true]);
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
if (MM_DEBUG) fail('delete failed: '.$e->getMessage(), 500);
|
||||
fail('delete failed', 500);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'get_game':
|
||||
$idParam = $_GET['igdb_id'] ?? '';
|
||||
if ($idParam === '') error_response('Missing \"igdb_id\"', 400);
|
||||
@@ -579,6 +613,24 @@ if (isset($_GET['action'])) {
|
||||
respond(['found' => (bool)$game, 'game' => $game]);
|
||||
break;
|
||||
|
||||
case 'delete_game':
|
||||
$idParam = $_GET['igdb_id'] ?? '';
|
||||
$igdbId = (int)$idParam;
|
||||
if ($igdbId <= 0) error_response('Invalid \"igdb_id\"', 400);
|
||||
$pdo->beginTransaction();
|
||||
try {
|
||||
$stmt = $pdo->prepare('DELETE FROM igdb_localizations WHERE igdb_id = ?');
|
||||
$stmt->execute([$igdbId]);
|
||||
$stmt = $pdo->prepare('DELETE FROM game WHERE igdb_id = ?');
|
||||
$stmt->execute([$igdbId]);
|
||||
$pdo->commit();
|
||||
respond(['ok' => true]);
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
error_response('delete failed', 500);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'wiki_test':
|
||||
$title = $_GET['title'] ?? '';
|
||||
if ($title === '') error_response('Missing \"title\"', 400);
|
||||
@@ -1007,7 +1059,11 @@ try {
|
||||
$s = strtolower($status);
|
||||
if ($s==='init') $statusVal = 0; elseif ($s==='progress') $statusVal = 1; elseif ($s==='done') $statusVal = 2;
|
||||
}
|
||||
$hasRelease = gameHasColumn('release_year');
|
||||
$hasCover = gameHasColumn('cover_url');
|
||||
$sql = "SELECT g.*,
|
||||
".($hasRelease ? 'g.release_year' : 'NULL AS release_year').",
|
||||
".($hasCover ? 'g.cover_url' : 'NULL AS cover_url').",
|
||||
CASE g.status WHEN 1 THEN 'Progress' WHEN 2 THEN 'Done' ELSE 'Init' END AS status,
|
||||
loc.title AS loc_title,
|
||||
loc.summary AS loc_summary,
|
||||
@@ -1264,6 +1320,7 @@ try {
|
||||
'origin' => $origin,
|
||||
'php' => PHP_VERSION,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'set_game_status': {
|
||||
@@ -1277,6 +1334,8 @@ try {
|
||||
$locSummary = isset($in['summary']) ? trim($in['summary']) : '';
|
||||
$locStory = isset($in['storyline']) ? trim($in['storyline']) : '';
|
||||
$coverUrl = isset($in['cover_url']) ? trim($in['cover_url']) : null;
|
||||
$releaseYear = isset($in['release_year']) ? (int)$in['release_year'] : null;
|
||||
$GLOBALS['__release_year'] = $releaseYear;
|
||||
if ($igdbId <= 0) fail('Invalid "igdb_id"');
|
||||
if ($name === '') fail('Missing "name"');
|
||||
if ($status < 0 || $status > 2) fail('Invalid "status" (0,1,2)');
|
||||
@@ -1285,6 +1344,26 @@ try {
|
||||
saveLocalization($igdbId, $lang, $locTitle ?: null, $locSummary ?: null, $locStory ?: null, null);
|
||||
}
|
||||
resp(['ok' => true, 'game' => $game]);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'delete_game': {
|
||||
$igdbId = (int)($in['igdb_id'] ?? 0);
|
||||
if ($igdbId <= 0) fail('Invalid "igdb_id"');
|
||||
$pdo->beginTransaction();
|
||||
try {
|
||||
$stmt = $pdo->prepare('DELETE FROM igdb_localizations WHERE igdb_id = ?');
|
||||
$stmt->execute([$igdbId]);
|
||||
$stmt = $pdo->prepare('DELETE FROM game WHERE igdb_id = ?');
|
||||
$stmt->execute([$igdbId]);
|
||||
$pdo->commit();
|
||||
resp(['ok' => true]);
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
if (MM_DEBUG) fail('delete failed: '.$e->getMessage(), 500);
|
||||
fail('delete failed', 500);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'get_game': {
|
||||
@@ -1292,6 +1371,7 @@ try {
|
||||
if ($igdbId <= 0) fail('Invalid "igdb_id"');
|
||||
$game = getGameByIgdbId($igdbId);
|
||||
resp(['ok' => true, 'found' => (bool)$game, 'game' => $game]);
|
||||
break;
|
||||
}
|
||||
|
||||
default: fail('unknown action');
|
||||
|
||||
Reference in New Issue
Block a user