payfrit-api/api/menu/updateStations.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

57 lines
1.4 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Update Station Assignments
*
* POST body:
* {
* "BusinessID": 37,
* "Assignments": [
* { "ItemID": 1, "StationID": 5 },
* { "ItemID": 2, "StationID": 5 }
* ]
* }
*/
$data = readJsonBody();
$businessID = (int) ($data['BusinessID'] ?? 0);
$assignments = $data['Assignments'] ?? [];
if ($businessID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_businessid', 'MESSAGE' => 'BusinessID is required.']);
}
try {
$updateCount = 0;
// Clear all station assignments for items in this business
queryTimed("UPDATE Items SET StationID = NULL WHERE BusinessID = ?", [$businessID]);
// Apply new assignments
foreach ($assignments as $assignment) {
if (isset($assignment['ItemID'], $assignment['StationID'])) {
queryTimed("UPDATE Items SET StationID = ? WHERE ID = ?", [
(int) $assignment['StationID'],
(int) $assignment['ItemID'],
]);
$updateCount++;
}
}
jsonResponse([
'OK' => true,
'ERROR' => '',
'MESSAGE' => 'Station assignments updated',
'UPDATED_COUNT' => $updateCount,
]);
} catch (Exception $e) {
jsonResponse([
'OK' => false,
'ERROR' => 'server_error',
'MESSAGE' => 'Error updating stations',
'DETAIL' => $e->getMessage(),
]);
}