![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/-41a9d47d/ |
<?php
/**
* Update Now Playing
* POST /api/radio/v1/now-playing
*
* Updates the currently playing track for a station
*/
$pdo = getDBConnection();
// Get station_id from authenticated station
$station_id = $station['id'];
// Get request data
$input = json_decode(file_get_contents('php://input'), true);
$track_id = $input['track_id'] ?? null;
$listener_count = $input['listener_count'] ?? 0;
if (!$track_id) {
http_response_code(400);
echo json_encode(['error' => 'track_id is required']);
exit;
}
// Verify track exists and is radio enabled
$stmt = $pdo->prepare("
SELECT id, duration FROM music_tracks
WHERE id = ? AND radio_enabled = 1 AND status = 'complete'
");
$stmt->execute([$track_id]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$track) {
http_response_code(404);
echo json_encode(['error' => 'Track not found or not available for radio']);
exit;
}
// Mark previous now playing as not live
$pdo->prepare("
UPDATE radio_now_playing
SET is_live = FALSE
WHERE station_id = ? AND is_live = TRUE
")->execute([$station_id]);
// Calculate estimated end time
$duration = $track['duration'] ?? 180; // Default 3 minutes if not set
$estimated_end = date('Y-m-d H:i:s', strtotime("+{$duration} seconds"));
// Create new now playing entry
$stmt = $pdo->prepare("
INSERT INTO radio_now_playing (
station_id, track_id, estimated_end_at, listener_count, is_live
) VALUES (?, ?, ?, ?, TRUE)
");
$stmt->execute([
$station_id,
$track_id,
$estimated_end,
$listener_count
]);
$now_playing_id = $pdo->lastInsertId();
// Get full now playing data
$stmt = $pdo->prepare("
SELECT
np.*,
t.id as track_id,
t.title,
t.artist_name,
t.genre,
t.duration
FROM radio_now_playing np
JOIN music_tracks t ON np.track_id = t.id
WHERE np.id = ?
");
$stmt->execute([$now_playing_id]);
$now_playing = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode([
'success' => true,
'now_playing' => $now_playing,
'message' => 'Now playing updated successfully'
]);