42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
if ($method === 'GET') {
|
|
$stmt = $pdo->query("SELECT * FROM saved_timers ORDER BY created_at DESC");
|
|
$timers = $stmt->fetchAll();
|
|
echo json_encode(['success' => true, 'data' => $timers]);
|
|
}
|
|
elseif ($method === 'POST') {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($data['name']) || empty($data['config'])) {
|
|
throw new Exception("Missing name or config");
|
|
}
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO saved_timers (name, config) VALUES (:name, :config)");
|
|
$stmt->execute([
|
|
':name' => $data['name'],
|
|
':config' => json_encode($data['config'])
|
|
]);
|
|
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);
|
|
}
|
|
elseif ($method === 'DELETE') {
|
|
$id = $_GET['id'] ?? null;
|
|
if (!$id) {
|
|
throw new Exception("Missing id");
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM saved_timers WHERE id = :id");
|
|
$stmt->execute([':id' => $id]);
|
|
echo json_encode(['success' => true]);
|
|
}
|
|
} catch (Exception $e) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|