112 lines
3.4 KiB
PHP
112 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/db.php';
|
|
|
|
try {
|
|
match ($_SERVER['REQUEST_METHOD']) {
|
|
'GET' => handle_get(),
|
|
'POST' => handle_post(),
|
|
'PUT' => handle_put(),
|
|
'DELETE' => handle_delete(),
|
|
default => error_out('Method not allowed', 405),
|
|
};
|
|
} catch (PDOException $e) {
|
|
error_out('Database error: ' . $e->getMessage(), 500);
|
|
}
|
|
|
|
// ─── GET /api/templates.php ───────────────────────────────────────────────────
|
|
|
|
function handle_get(): never
|
|
{
|
|
$rows = db()->query(
|
|
'SELECT t.id, t.name, t.body, t.created_at, t.updated_at,
|
|
COUNT(sr.id) AS scheduled_count
|
|
FROM sms_templates t
|
|
LEFT JOIN scheduled_rules sr ON sr.template_id = t.id
|
|
GROUP BY t.id
|
|
ORDER BY t.name'
|
|
)->fetchAll();
|
|
|
|
json_out(['templates' => $rows]);
|
|
}
|
|
|
|
// ─── POST /api/templates.php ──────────────────────────────────────────────────
|
|
// Body: { "name": "...", "body": "..." }
|
|
|
|
function handle_post(): never
|
|
{
|
|
$body = json_body();
|
|
$name = trim((string) ($body['name'] ?? ''));
|
|
$text = trim((string) ($body['body'] ?? ''));
|
|
|
|
if ($name === '') {
|
|
error_out('name is required');
|
|
}
|
|
if ($text === '') {
|
|
error_out('body is required');
|
|
}
|
|
if (mb_strlen($text) > 160) {
|
|
error_out('body must be 160 characters or fewer');
|
|
}
|
|
|
|
$stmt = db()->prepare('INSERT INTO sms_templates (name, body) VALUES (:name, :body)');
|
|
$stmt->execute([':name' => $name, ':body' => $text]);
|
|
$id = (int) db()->lastInsertId();
|
|
|
|
json_out(['id' => $id, 'name' => $name, 'body' => $text], 201);
|
|
}
|
|
|
|
// ─── PUT /api/templates.php?id=N ─────────────────────────────────────────────
|
|
// Body: { "name": "...", "body": "..." }
|
|
|
|
function handle_put(): never
|
|
{
|
|
$id = (int) ($_GET['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
error_out('Missing or invalid id');
|
|
}
|
|
|
|
$body = json_body();
|
|
$name = trim((string) ($body['name'] ?? ''));
|
|
$text = trim((string) ($body['body'] ?? ''));
|
|
|
|
if ($name === '') {
|
|
error_out('name is required');
|
|
}
|
|
if ($text === '') {
|
|
error_out('body is required');
|
|
}
|
|
if (mb_strlen($text) > 160) {
|
|
error_out('body must be 160 characters or fewer');
|
|
}
|
|
|
|
$stmt = db()->prepare('UPDATE sms_templates SET name = :name, body = :body WHERE id = :id');
|
|
$stmt->execute([':name' => $name, ':body' => $text, ':id' => $id]);
|
|
|
|
if ($stmt->rowCount() === 0) {
|
|
error_out('Template not found', 404);
|
|
}
|
|
|
|
json_out(['id' => $id, 'name' => $name, 'body' => $text]);
|
|
}
|
|
|
|
// ─── DELETE /api/templates.php?id=N ──────────────────────────────────────────
|
|
|
|
function handle_delete(): never
|
|
{
|
|
$id = (int) ($_GET['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
error_out('Missing or invalid id');
|
|
}
|
|
|
|
$stmt = db()->prepare('DELETE FROM sms_templates WHERE id = :id');
|
|
$stmt->execute([':id' => $id]);
|
|
|
|
if ($stmt->rowCount() === 0) {
|
|
error_out('Template not found', 404);
|
|
}
|
|
|
|
json_out(['deleted' => true]);
|
|
}
|