![]() 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/f3f44d7/ |
<?php
/**
* Like/Unlike Track
* POST /api/radio/v1/tracks/{id}/like
*
* Toggle like status for a track
*/
$pdo = getDBConnection();
// Get station_id from authenticated station (optional - can be public)
$station_id = $station['id'] ?? null;
// Get track_id from URL or request
$track_id = $_GET['track_id'] ?? json_decode(file_get_contents('php://input'), true)['track_id'] ?? null;
if (!$track_id) {
http_response_code(400);
echo json_encode(['error' => 'track_id is required']);
exit;
}
// Get user info if available
$user_id = $_SESSION['user_id'] ?? null;
$ip_address = $_SERVER['REMOTE_ADDR'] ?? null;
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? null;
// Check if already liked
$stmt = $pdo->prepare("
SELECT id FROM radio_track_likes
WHERE station_id = ? AND track_id = ?
AND (user_id = ? OR (user_id IS NULL AND ip_address = ?))
LIMIT 1
");
$stmt->execute([$station_id, $track_id, $user_id, $ip_address]);
$existing_like = $stmt->fetch();
if ($existing_like) {
// Unlike - remove the like
$pdo->prepare("DELETE FROM radio_track_likes WHERE id = ?")->execute([$existing_like['id']]);
// Update cache
$pdo->prepare("
UPDATE music_tracks
SET radio_like_count = GREATEST(0, radio_like_count - 1)
WHERE id = ?
")->execute([$track_id]);
$liked = false;
} else {
// Like - add the like
$stmt = $pdo->prepare("
INSERT INTO radio_track_likes (station_id, track_id, user_id, ip_address, user_agent)
VALUES (?, ?, ?, ?, ?)
");
$stmt->execute([$station_id, $track_id, $user_id, $ip_address, $user_agent]);
// Update cache
$pdo->prepare("
UPDATE music_tracks
SET radio_like_count = radio_like_count + 1
WHERE id = ?
")->execute([$track_id]);
$liked = true;
}
// Get updated like count
$stmt = $pdo->prepare("SELECT radio_like_count FROM music_tracks WHERE id = ?");
$stmt->execute([$track_id]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode([
'success' => true,
'liked' => $liked,
'like_count' => (int)($track['radio_like_count'] ?? 0)
]);