38 lines
1.2 KiB
PHP
38 lines
1.2 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$db = db();
|
|
|
|
if ($method === 'POST') {
|
|
$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 {
|
|
$stmt = $db->query("SELECT * FROM song_requests ORDER BY created_at DESC LIMIT 10");
|
|
$requests = $stmt->fetchAll();
|
|
echo json_encode(['success' => true, 'requests' => $requests]);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => true, 'requests' => [], 'error' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|