38428-vm/api/song_requests.php
2026-02-17 17:11:08 +00:00

75 lines
2.5 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$method = $_SERVER['REQUEST_METHOD'];
$db = db();
if ($method === 'POST') {
if (isset($_POST['action'])) {
$action = $_POST['action'];
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
echo json_encode(['success' => false, 'error' => 'ID inválido']);
exit;
}
try {
if ($action === 'mark_played') {
$stmt = $db->prepare("UPDATE song_requests SET status = 'played' WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['success' => true]);
} elseif ($action === 'delete') {
$stmt = $db->prepare("DELETE FROM song_requests WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Acción no reconocida']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
exit;
}
$artist = trim($_POST['artist'] ?? '');
$song = trim($_POST['song'] ?? '');
$requester = trim($_POST['requester'] ?? 'Anónimo');
if (empty($artist) || empty($song)) {
echo json_encode(['success' => false, 'error' => 'Falta artista o canción']);
exit;
}
try {
$stmt = $db->prepare("INSERT INTO song_requests (artist, song, requester) VALUES (?, ?, ?)");
$stmt->execute([$artist, $song, $requester]);
echo json_encode(['success' => true]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
exit;
}
if ($method === 'GET') {
try {
$status = $_GET['status'] ?? 'pending';
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 10;
if ($status === 'all') {
$stmt = $db->query("SELECT * FROM song_requests ORDER BY created_at DESC LIMIT $limit");
} else {
$stmt = $db->prepare("SELECT * FROM song_requests WHERE status = ? ORDER BY created_at DESC LIMIT $limit");
$stmt->execute([$status]);
}
$requests = $stmt->fetchAll();
echo json_encode(['success' => true, 'requests' => $requests]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'requests' => [], 'error' => $e->getMessage()]);
}
exit;
}