My Favorites
+ + +You haven't saved any favorites yet.
+ +diff --git a/api/favorites.php b/api/favorites.php new file mode 100644 index 0000000..a6658c3 --- /dev/null +++ b/api/favorites.php @@ -0,0 +1,34 @@ + false, 'error' => 'User not logged in.']); + exit; +} + +$data = json_decode(file_get_contents('php://input'), true); + +$video_id = $data['video_id'] ?? null; +$title = $data['title'] ?? null; +$thumbnail = $data['thumbnail'] ?? null; + +if (!$video_id || !$title || !$thumbnail) { + echo json_encode(['success' => false, 'error' => 'Invalid data.']); + exit; +} + +try { + $pdo = db(); + $stmt = $pdo->prepare("INSERT INTO favorites (user_id, video_id, title, thumbnail) VALUES (:user_id, :video_id, :title, :thumbnail)"); + $stmt->execute(['user_id' => $_SESSION['user_id'], 'video_id' => $video_id, 'title' => $title, 'thumbnail' => $thumbnail]); + echo json_encode(['success' => true]); +} catch (PDOException $e) { + if ($e->errorInfo[1] == 1062) { // Duplicate entry + echo json_encode(['success' => false, 'error' => 'Already in favorites.']); + } else { + echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]); + } +} diff --git a/assets/js/main.js b/assets/js/main.js index a2d1df9..0fe114d 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -3,14 +3,15 @@ document.addEventListener('DOMContentLoaded', function () { const resultsSection = document.getElementById('results'); const resultsTitle = document.getElementById('results-title'); const videoGrid = document.getElementById('video-grid'); + const isLoggedIn = document.body.dataset.loggedIn === 'true'; // Hardcoded YouTube video IDs for different moods const musicDatabase = { - happy: ['3_w0dsiud4E', 'ZbZSe6N_BXs', 'r5Mozr0B-4o'], - sad: ['h1YVae0pKnA', 'RB-RcX5DS5A', 'co512-p_gA8'], - energetic: ['_tV5LEBDs7w', 'l-sZyfFX4F0', 'q9d57g_m_dw'], - calm: ['5qap5aO4i9A', 'lFcLg2hQjdc', 'DWcJFNfaw9c'], - upbeat: ['9d8SzG4FPyM', 'fK_zwl-lnmc', '0-7IHOXkiV8'] + happy: [{id: 'y6Sxv-sUYtM', title: 'Pharrell Williams - Happy'}, {id: 'HgzGwKwLmgM', title: 'Queen - Don\'t Stop Me Now'}, {id: 'iPUmE-tne5s', title: 'Katrina & The Waves - Walking On Sunshine'}], + sad: [{id: 'hLQl3WQQoQ0', title: 'Adele - Someone Like You'}, {id: '8AHCfZTRGiI', title: 'Johnny Cash - Hurt'}, {id: 'XFkzRNyygfk', title: 'Radiohead - Creep'}], + energetic: [{id: 'v2AC41dglnM', title: 'AC/DC - Thunderstruck'}, {id: 'o1tj2zJ2Wvg', title: 'Guns N\' Roses - Welcome to the Jungle'}, {id: 'CD-E-LDc384', title: 'Metallica - Enter Sandman'}], + calm: [{id: 'vNwYtllyt3Q', title: 'Brian Eno - Music for Airports 1/1'}, {id: 'S-Xm7s9eGxU', title: 'Erik Satie - Gymnopédie No. 1'}, {id: 'U3u4pQ44e14', title: 'Claude Debussy - Clair de Lune'}], + upbeat: [{id: 'FGBhQbmPwH8', title: 'Daft Punk - One More Time'}, {id: 'sy1dYFGkPUE', title: 'Justice - D.A.N.C.E.'}, {id: 'Cj8JrQ9w5jY', title: 'LCD Soundsystem - Daft Punk Is Playing at My House'}] }; moodButtons.forEach(button => { @@ -33,7 +34,7 @@ document.addEventListener('DOMContentLoaded', function () { resultsTitle.textContent = `Here's your ${mood} playlist`; resultsSection.style.display = 'block'; - videos.forEach(videoId => { + videos.forEach(video => { const col = document.createElement('div'); col.className = 'col-lg-4 col-md-6'; @@ -41,13 +42,22 @@ document.addEventListener('DOMContentLoaded', function () { videoContainer.className = 'video-container'; const iframe = document.createElement('iframe'); - iframe.src = `https://www.youtube.com/embed/${videoId}`; + iframe.src = `https://www.youtube.com/embed/${video.id}`; iframe.title = 'YouTube video player'; iframe.frameBorder = '0'; iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture'; iframe.allowFullscreen = true; videoContainer.appendChild(iframe); + + if (isLoggedIn) { + const saveButton = document.createElement('button'); + saveButton.className = 'btn btn-sm btn-outline-light mt-2'; + saveButton.textContent = 'Save to favorites'; + saveButton.addEventListener('click', () => saveToFavorites(video.id, video.title)); + videoContainer.appendChild(saveButton); + } + col.appendChild(videoContainer); videoGrid.appendChild(col); }); @@ -59,4 +69,22 @@ document.addEventListener('DOMContentLoaded', function () { resultsSection.style.display = 'none'; } } + + function saveToFavorites(videoId, title) { + fetch('/api/favorites.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ video_id: videoId, title: title, thumbnail: `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg` }) + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + alert('Saved to favorites!'); + } else { + alert('Error: ' + data.error); + } + }); + } }); \ No newline at end of file diff --git a/db/migrate.php b/db/migrate.php new file mode 100644 index 0000000..4f72b99 --- /dev/null +++ b/db/migrate.php @@ -0,0 +1,24 @@ +exec($sql); + echo "Migration from $file ran successfully.\n"; + } catch (PDOException $e) { + echo "Error running migration from $file: " . $e->getMessage() . "\n"; + } + } + } +} + +run_migrations(); + diff --git a/db/migrations/001_create_users_table.sql b/db/migrations/001_create_users_table.sql new file mode 100644 index 0000000..8071f7a --- /dev/null +++ b/db/migrations/001_create_users_table.sql @@ -0,0 +1,11 @@ + +CREATE TABLE IF NOT EXISTS `users` ( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `username` VARCHAR(255) NOT NULL, + `email` VARCHAR(255) NOT NULL, + `password` VARCHAR(255) NOT NULL, + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `username` (`username`), + UNIQUE KEY `email` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/db/migrations/002_create_favorites_table.sql b/db/migrations/002_create_favorites_table.sql new file mode 100644 index 0000000..6ac8a36 --- /dev/null +++ b/db/migrations/002_create_favorites_table.sql @@ -0,0 +1,12 @@ + +CREATE TABLE IF NOT EXISTS `favorites` ( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `user_id` INT(11) NOT NULL, + `video_id` VARCHAR(255) NOT NULL, + `title` VARCHAR(255) NOT NULL, + `thumbnail` VARCHAR(255) NOT NULL, + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `user_video` (`user_id`, `video_id`), + FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/favorites.php b/favorites.php new file mode 100644 index 0000000..a29e8cc --- /dev/null +++ b/favorites.php @@ -0,0 +1,66 @@ +prepare("SELECT video_id, title, thumbnail FROM favorites WHERE user_id = :user_id ORDER BY created_at DESC"); + $stmt->execute(['user_id' => $_SESSION['user_id']]); + $favorites = $stmt->fetchAll(); +} catch (PDOException $e) { + // Handle error +} + +?> + + +
+ + +You haven't saved any favorites yet.
+ +Registration successful! You can now login.
+