38436-vm/api/tickets.php
Flatlogic Bot 8d5d2597c6 V1.0001
2026-02-14 23:14:09 +00:00

35 lines
989 B
PHP

<?php
require_once __DIR__ . '/../db/config.php';
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
try {
$pdo = db();
if ($method === 'GET') {
$stmt = $pdo->query("SELECT * FROM tickets ORDER BY created_at DESC");
echo json_encode($stmt->fetchAll());
}
elseif ($method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data['title'])) {
throw new Exception("Title is required");
}
$stmt = $pdo->prepare("INSERT INTO tickets (title, description, priority, status) VALUES (?, ?, ?, 'Open')");
$stmt->execute([
$data['title'],
$data['description'] ?? '',
$data['priority'] ?? 'Medium'
]);
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);
}
} catch (Exception $e) {
http_response_code(400);
echo json_encode(['error' => $e->getMessage()]);
}