| Server IP : 173.209.174.21 / Your IP : 216.73.216.89 Web Server : Apache/2.4.58 (Ubuntu) System : Linux wcfs-server 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : nodor ( 1000) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/wcfs/pest/api/api/ |
Upload File : |
<?php
declare(strict_types=1);
require __DIR__ . '/db.php';
$pdo = db();
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$rows = $pdo->query('SELECT * FROM customers ORDER BY name ASC, firm ASC')->fetchAll();
$customers = array_map(function (array $row): array {
return [
'id' => $row['id'],
'name' => $row['name'],
'firm' => $row['firm'],
'sites' => json_decode($row['sites'] ?: '[]', true) ?: [],
'createdAt' => $row['created_at'],
'updatedAt' => $row['updated_at'],
];
}, $rows);
json_response($customers);
}
if ($method === 'POST') {
$data = input_json();
$id = text_value($data, 'id');
if ($id === '') {
json_response(['error' => 'Customer id is required.'], 422);
}
$now = gmdate('c');
$statement = $pdo->prepare('
INSERT INTO customers (id, name, firm, sites, created_at, updated_at)
VALUES (:id, :name, :firm, :sites, :created_at, :updated_at)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
firm = VALUES(firm),
sites = VALUES(sites),
updated_at = VALUES(updated_at)
');
$statement->execute([
':id' => $id,
':name' => text_value($data, 'name'),
':firm' => text_value($data, 'firm'),
':sites' => json_value($data, 'sites'),
':created_at' => text_value($data, 'createdAt') ?: $now,
':updated_at' => text_value($data, 'updatedAt') ?: $now,
]);
json_response($data);
}
json_response(['error' => 'Method not allowed.'], 405);