-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Express API - Kpop albums - Cholpon #532
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job creating your first API Cholpon! Just remember going forward to make use of query params instead of creating one endpoint for each filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to include the compiled files, you can add it to your gitignore file
// Path to your JSON dataset | ||
const dataPath = path.resolve("./data/kpop-album-releases.json"); | ||
|
||
// Load the dataset | ||
let data = []; | ||
try { | ||
const rawData = fs.readFileSync(dataPath, "utf-8"); | ||
data = JSON.parse(rawData); | ||
console.log("Dataset loaded successfully:", data.length, "records found."); | ||
} catch (error) { | ||
console.error("Error loading dataset:", error.message); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? 👀
app.get("/api/albums/artist/:artist", (req, res) => { | ||
const artist = req.params.artist.toLowerCase(); | ||
const albums = data.filter((album) => | ||
album.artist.toLowerCase().includes(artist) | ||
); | ||
|
||
if (albums.length > 0) { | ||
res.json(albums); | ||
} else { | ||
res.status(404).json({ error: "No albums found for the given artist" }); | ||
} | ||
}); | ||
|
||
// Get albums by category | ||
app.get("/api/albums/category/:category", (req, res) => { | ||
const category = req.params.category.toLowerCase(); | ||
const albums = data.filter( | ||
(album) => album.category.toLowerCase() === category | ||
); | ||
|
||
if (albums.length > 0) { | ||
res.json(albums); | ||
} else { | ||
res.status(404).json({ error: "No albums found in the given category" }); | ||
} | ||
}); | ||
|
||
// Get albums released in a specific year | ||
app.get("/api/albums/year/:year", (req, res) => { | ||
const year = parseInt(req.params.year); | ||
const albums = data.filter((album) => album.year === year); | ||
|
||
if (albums.length > 0) { | ||
res.json(albums); | ||
} else { | ||
res.status(404).json({ error: "No albums found for the given year" }); | ||
} | ||
}); | ||
|
||
// Get albums with a minimum rating | ||
app.get("/api/albums/rating/:rating", (req, res) => { | ||
const rating = parseFloat(req.params.rating); | ||
const albums = data.filter((album) => album.rating >= rating); | ||
|
||
if (albums.length > 0) { | ||
res.json(albums); | ||
} else { | ||
res.status(404).json({ error: "No albums found with the given rating" }); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could all be query params under the /albums endpoint, no need to create different endpoints for each filter. This way you also have the possibility to chain filters, e.g. if you want to filter on year and category
https://project-express-api-fy1c.onrender.com