36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input || empty($input['title']) || empty($input['artist'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid input']);
|
|
exit;
|
|
}
|
|
|
|
$title = $input['title'];
|
|
$artist = $input['artist'];
|
|
$cover = $input['cover'] ?? null;
|
|
|
|
try {
|
|
$db = db();
|
|
|
|
// Check if the last recorded song is the same to avoid duplicates on refresh
|
|
$stmt = $db->prepare("SELECT title, artist FROM song_history ORDER BY played_at DESC LIMIT 1");
|
|
$stmt->execute();
|
|
$last = $stmt->fetch();
|
|
|
|
if ($last && $last['title'] === $title && $last['artist'] === $artist) {
|
|
echo json_encode(['success' => true, 'message' => 'Already recorded']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $db->prepare("INSERT INTO song_history (title, artist, cover) VALUES (?, ?, ?)");
|
|
$stmt->execute([$title, $artist, $cover]);
|
|
|
|
echo json_encode(['success' => true]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|