Skip to content

Commit 5d02ef4

Browse files
committed
Create routes that gives a random song and album
1 parent d5fa369 commit 5d02ef4

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

controllers/albumController.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ const deleteAlbum = async (req, res) => {
8484
errorHandler(res, error);
8585
}
8686
};
87+
const getRandomAlbum = async (req, res) => {
88+
try {
89+
const albums = await Album.find({}).lean().select("-__v").select("-_id");
90+
let album = albums[Math.floor(Math.random() * albums.length)];
8791

92+
album = await enhanceAlbumWithSongsData(album);
93+
94+
return res.json(album);
95+
} catch (error) {
96+
errorHandler(res, error);
97+
}
98+
};
8899
const newAlbum = async (req, res) => {
89100
try {
90101
const { title, artist, releaseDate, albumCover, user } = req.body;
@@ -108,6 +119,7 @@ const newAlbum = async (req, res) => {
108119
};
109120

110121
module.exports = {
122+
getRandomAlbum,
111123
getAllAlbums,
112124
deleteAlbum,
113125
newAlbum,

controllers/songController.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,42 @@ const getSong = async (req, res) => {
1414
}
1515

1616
// Get album details without the tracks property
17-
const albumDetails = await getAlbumDetails(song.albumId);
17+
const { albumName, albumCover, artist, releaseDate } =
18+
await getAlbumDetails(song.albumId);
1819

19-
if (!albumDetails) {
20+
if (!albumName) {
2021
return res.status(404).json({ error: "Album not found" });
2122
}
23+
delete song.albumId;
2224

2325
res.json({
2426
...song,
25-
album: albumDetails,
27+
album: albumName,
28+
});
29+
} catch (error) {
30+
console.error("Error fetching songs");
31+
errorHandler(res, error);
32+
}
33+
};
34+
35+
const getRandomSong = async (req, res) => {
36+
try {
37+
const songs = await Song.find().select("-__v -_id").lean();
38+
const song = songs[Math.floor(Math.random() * songs.length)];
39+
if (!song) {
40+
return res.status(404).json({ error: "Song not available" });
41+
}
42+
// Get album details without the tracks property
43+
const { albumName, albumCover, artist, releaseDate } =
44+
await getAlbumDetails(song.albumId);
45+
46+
if (!albumName) {
47+
return res.status(404).json({ error: "Album not found" });
48+
}
49+
delete song.albumId;
50+
res.json({
51+
...song,
52+
album: albumName,
2653
});
2754
} catch (error) {
2855
console.error("Error fetching songs");
@@ -124,4 +151,5 @@ module.exports = {
124151
getAllSongs,
125152
newSong,
126153
searchSongs,
154+
getRandomSong,
127155
};

routes/album.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const {
44
deleteAlbum,
55
newAlbum,
66
searchAlbum,
7+
getRandomAlbum,
78
} = require("../controllers/albumController");
89
const router = express.Router();
910

1011
router.route("/").get(getAllAlbums);
1112
router.route("/search").get(searchAlbum);
13+
router.route("/random").get(getRandomAlbum);
1214
router.route("/:id").delete(deleteAlbum);
1315
router.route("/newalbum").post(newAlbum);
1416

routes/song.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ const {
33
getAllSongs,
44
newSong,
55
searchSongs,
6+
getRandomSong,
67
} = require("../controllers/songController");
78
const router = express.Router();
89

910
router.route("/").get(getAllSongs);
11+
router.route("/random").get(getRandomSong);
1012
router.route("/search").get(searchSongs);
1113
router.route("/new").post(newSong);
1214

views/index.ejs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@
166166
Get detailed information about a specific album.
167167
</p>
168168
</div>
169+
<div
170+
class="flex flex-col md:flex-row mb-2 items-start text-base gap-2"
171+
>
172+
<div>
173+
<span class="bg-green-500 px-2 py-1 text-white rounded"
174+
>GET</span
175+
>
176+
<a
177+
href="https://taylor-swift-api.onrender.com/albums/random"
178+
class="bg-gray-200 px-2 hover:underline py-1 rounded"
179+
target="_blank"
180+
>
181+
<code>/albums/random</code>
182+
</a>
183+
</div>
184+
185+
<p class="text-base">Gives a random album</p>
186+
</div>
169187
<div
170188
class="flex flex-col md:flex-row mb-2 items-start text-base gap-2"
171189
>
@@ -224,6 +242,23 @@
224242
Get detailed information about a specific song.
225243
</p>
226244
</div>
245+
<div
246+
class="flex flex-col md:flex-row mb-2 items-start text-base gap-2"
247+
>
248+
<div>
249+
<span class="bg-green-500 px-2 py-1 text-white rounded"
250+
>GET</span
251+
>
252+
<a
253+
href="https://taylor-swift-api.onrender.com/songs/random"
254+
class="bg-gray-200 px-2 hover:underline py-1 rounded"
255+
target="_blank"
256+
>
257+
<code>/songs/random</code>
258+
</a>
259+
</div>
260+
<p class="text-base">Gives a random song</p>
261+
</div>
227262
<div
228263
class="flex flex-col md:flex-row mb-2 items-start text-base gap-2"
229264
>
@@ -236,7 +271,7 @@
236271
class="bg-gray-200 px-2 hover:underline py-1 rounded"
237272
target="_blank"
238273
>
239-
<code>/songs/?name=World</code>
274+
<code>/songs/search/?name=World</code>
240275
</a>
241276
</div>
242277
<p class="text-base">Search song by thier keywords and name</p>

0 commit comments

Comments
 (0)