Skip to content

Conversation

@cholpoun
Copy link

@cholpoun cholpoun commented Dec 8, 2024

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a 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

Copy link
Contributor

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

Comment on lines +7 to +18
// 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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? 👀

Comment on lines +64 to 113
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" });
}
});
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants