From f77ce71bd61cbfecc5ab58c3f2a730f175c6bba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Gla=CC=88ser?= Date: Sun, 20 Sep 2026 12:20:57 +0200 Subject: [PATCH] =?UTF-8?q?Kostenpositionsspeicherung=20atomar=20ausf?= =?UTF-8?q?=C3=BChren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/api/market-occurrence.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/public/api/market-occurrence.php b/public/api/market-occurrence.php index 59bf6e3..61376c5 100644 --- a/public/api/market-occurrence.php +++ b/public/api/market-occurrence.php @@ -43,9 +43,10 @@ if ($requestMethod === 'POST') { echo json_encode(['ok' => true, 'occurrence_id' => (int) $occurrence['id']], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } - $scope = (string) ($payload['scope'] ?? ''); - $id = market_management_require_positive_id($payload['id'] ?? null, 'id'); - $fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : []; + $scope = (string) ($payload['scope'] ?? ''); + $id = market_management_require_positive_id($payload['id'] ?? null, 'id'); + $fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : []; + $costTransactionStarted = false; if (($payload['action'] ?? '') === 'delete') { if (($payload['confirmation'] ?? '') !== 'DELETE') { throw new InvalidArgumentException('Die Löschbestätigung muss exakt DELETE lauten.'); @@ -104,6 +105,10 @@ if ($requestMethod === 'POST') { if (!is_array($costItems)) { throw new InvalidArgumentException('Kostenpositionen sind ungültig.'); } + if (!$pdo->inTransaction()) { + $pdo->beginTransaction(); + $costTransactionStarted = true; + } unset($fields['cost_items']); $keptCostItemIds = array_values(array_filter(array_map( static fn (array $costItem): ?int => isset($costItem['id']) ? (int) $costItem['id'] : null, @@ -137,6 +142,10 @@ if ($requestMethod === 'POST') { } } market_management_update_occurrence($pdo, $id, $fields); + if ($costTransactionStarted) { + $pdo->commit(); + $costTransactionStarted = false; + } } } else { throw new InvalidArgumentException('Ungültiger Bearbeitungsbereich.'); @@ -144,6 +153,9 @@ if ($requestMethod === 'POST') { echo json_encode(['ok' => true]); exit; } catch (Throwable $error) { + if ($costTransactionStarted && $pdo->inTransaction()) { + $pdo->rollBack(); + } http_response_code(422); echo json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit;