Skip to content

Commit aa03f4f

Browse files
committed
Create Search Route and change view
/songs/search /albums/search
1 parent 441b817 commit aa03f4f

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

controllers/albumController.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const cleanUpSongs = (songs, album) => {
1414
const getAlbum = async (req, res) => {
1515
try {
1616
const { name } = req.params;
17-
console.log(name);
1817
const album = await Album.findOne({ title: name })
1918
.select("-__v")
2019
.select("-_id")
@@ -58,6 +57,32 @@ const getAllAlbums = async (req, res) => {
5857
}
5958
};
6059

60+
const searchAlbum = async (req, res) => {
61+
const { name } = req.query;
62+
63+
try {
64+
// Use a regular expression to perform a case-insensitive search
65+
const albums = await Album.find({ title: { $regex: name, $options: "i" } })
66+
.select("-__v -_id")
67+
.lean();
68+
69+
for (const album of albums) {
70+
const songPromises = album.tracks.map((songId) =>
71+
Song.findById(songId.toString()).select("-__v").select("-_id").lean()
72+
);
73+
const songs = await Promise.all(songPromises);
74+
75+
album.tracks = cleanUpSongs(songs, album);
76+
album.songs = album.tracks.length;
77+
}
78+
79+
res.json(albums);
80+
} catch (error) {
81+
console.error("Error searching songs:", error.message);
82+
res.status(500).json({ error: "Internal Server Error" });
83+
}
84+
};
85+
6186
const deleteAlbum = async (req, res) => {
6287
try {
6388
const { id } = req.params;
@@ -92,7 +117,6 @@ const newAlbum = async (req, res) => {
92117
const newAlbum = new Album({
93118
title,
94119
artist,
95-
releaseYear,
96120
releaseDate,
97121
albumCover,
98122
tracks: [],
@@ -111,4 +135,5 @@ module.exports = {
111135
deleteAlbum,
112136
newAlbum,
113137
getAlbum,
138+
searchAlbum,
114139
};

controllers/songController.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ const getAllSongs = async (req, res) => {
5151
}
5252
};
5353

54+
const searchSongs = async (req, res) => {
55+
const { name } = req.query;
56+
57+
try {
58+
// Use a regular expression to perform a case-insensitive search
59+
const songs = await Song.find({ name: { $regex: name, $options: "i" } })
60+
.select("-__v -_id")
61+
.lean();
62+
63+
// Fetch additional album details for each song
64+
const songsWithAlbum = await Promise.all(
65+
songs.map(async (song) => {
66+
const albumDetails = await getAlbumDetails(song.albumId);
67+
68+
if (albumDetails) {
69+
song.albumName = albumDetails.albumName;
70+
delete song.albumId;
71+
}
72+
73+
return song;
74+
})
75+
);
76+
77+
res.json(songsWithAlbum);
78+
} catch (error) {
79+
console.error("Error searching songs:", error.message);
5480
res.status(500).json({ error: "Internal Server Error" });
5581
}
5682
};
@@ -97,4 +123,5 @@ module.exports = {
97123
getSong,
98124
getAllSongs,
99125
newSong,
126+
searchSongs,
100127
};

routes/album.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const express = require("express");
2-
const Album = require("../models/Album");
3-
const Song = require("../models/Song");
42
const {
53
getAllAlbums,
64
deleteAlbum,
75
newAlbum,
6+
searchAlbum,
87
} = require("../controllers/albumController");
98
const router = express.Router();
109

1110
router.route("/").get(getAllAlbums);
11+
router.route("/search").get(searchAlbum);
1212
router.route("/:id").delete(deleteAlbum);
1313
router.route("/newalbum").post(newAlbum);
1414

routes/song.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const express = require("express");
2-
const Song = require("../models/Song");
3-
const Album = require("../models/Album");
4-
const { getAllSongs, newSong } = require("../controllers/songController");
2+
const {
3+
getAllSongs,
4+
newSong,
5+
searchSongs,
6+
} = require("../controllers/songController");
57
const router = express.Router();
68

79
router.route("/").get(getAllSongs);
10+
router.route("/search").get(searchSongs);
811
router.route("/new").post(newSong);
912

1013
module.exports = router;

views/index.ejs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
/>
8383
</head>
8484

85-
<body class="bg-[#C6A969]">
85+
<body style="background-color: #f1e4c3">
8686
<div class="max-w-3xl mx-auto my-12 px-5">
8787
<div class="mb-3">
8888
<div class="flex items-center justify-between">
@@ -110,7 +110,7 @@
110110
class="w-10 h-10"
111111
/></a>
112112
</div>
113-
<hr />
113+
<hr class="h-1 rounded" style="background-color: #000" />
114114
</div>
115115
<p class="text-base mb-5">
116116
Open Source API for Taylor Swift Albums and Songs<br />
@@ -124,6 +124,7 @@
124124
<div class="">
125125
<h2 class="text-2xl font-semibold mb-3">API End points</h2>
126126
<hr />
127+
<hr style="background-color: #3a3a3a; height: 2px" />
127128
<ol class="list-decimal px-7 my-5 text-xl">
128129
<li class="mb-5">
129130
<h1 class="text-xl mb-1 font-semibold">Albums</h1>
@@ -165,6 +166,24 @@
165166
Get detailed information about a specific album.
166167
</p>
167168
</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/?name=lover"
178+
class="bg-gray-200 px-2 hover:underline py-1 rounded"
179+
target="_blank"
180+
>
181+
<code>/albums/?name=lover</code>
182+
</a>
183+
</div>
184+
185+
<p class="text-base">Search Albums by thier keywords and name</p>
186+
</div>
168187
</li>
169188
<li class="mb-5">
170189
<h1 class="text-xl mb-1 font-semibold">Songs</h1>
@@ -205,9 +224,26 @@
205224
Get detailed information about a specific song.
206225
</p>
207226
</div>
227+
<div
228+
class="flex flex-col md:flex-row mb-2 items-start text-base gap-2"
229+
>
230+
<div>
231+
<span class="bg-green-500 px-2 py-1 text-white rounded"
232+
>GET</span
233+
>
234+
<a
235+
href="https://taylor-swift-api.onrender.com/songs/?name=world"
236+
class="bg-gray-200 px-2 hover:underline py-1 rounded"
237+
target="_blank"
238+
>
239+
<code>/songs/?name=World</code>
240+
</a>
241+
</div>
242+
<p class="text-base">Search song by thier keywords and name</p>
243+
</div>
208244
</li>
209245
</ol>
210-
<hr />
246+
<hr style="background-color: #3a3a3a; height: 2px" />
211247
<button
212248
class="flex items-center justify-center gap-2 sm:hidden bg-gray-300 mt-4 text-center py-2 text-black w-full"
213249
>

0 commit comments

Comments
 (0)