diff --git a/package.json b/package.json index f93ddb52..69044116 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,12 @@ "author": "", "license": "ISC", "dependencies": { - "@babel/core": "^7.17.9", - "@babel/node": "^7.16.8", - "@babel/preset-env": "^7.16.11", + "@babel/core": "^7.26.0", + "@babel/node": "^7.26.0", + "@babel/preset-env": "^7.26.0", "cors": "^2.8.5", - "express": "^4.17.3", - "nodemon": "^3.0.1" + "express": "^4.21.2", + "express-list-endpoints": "^7.1.1", + "nodemon": "^3.1.7" } } diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index 39a2aa32..00000000 --- a/pull_request_template.md +++ /dev/null @@ -1,6 +0,0 @@ -## Netlify link -Add your Netlify link here. -PS. Don't forget to add it in your readme as well. - -## Collaborators -Add any collaborators here. diff --git a/routes/awards.js b/routes/awards.js new file mode 100644 index 00000000..0d9cb0b3 --- /dev/null +++ b/routes/awards.js @@ -0,0 +1,40 @@ +import express from 'express'; +import awards from '../data/golden-globes.json'; + +const router = express.Router(); + +router.get('/', (req, res) => { + const { year, category, winner } = req.query; + let filteredAwards = awards; + + if (year) { + filteredAwards = filteredAwards.filter((award) => award.year_award == year); + } + if (category) { + filteredAwards = filteredAwards.filter((award) => + award.category.toLowerCase().includes(category.toLowerCase()) + ); + } + if (winner) { + filteredAwards = filteredAwards.filter( + (award) => award.win.toString() === winner + ); + } + + res.json(filteredAwards); +}); + +router.get('/:id', (req, res) => { + const id = parseInt(req.params.id, 10); + if (id >= 0 && id < awards.length) { + res.json(awards[id]); + } else { + res.status(404).json({ error: 'Award not found' }); + } +}); + +router.post('/dummy-endpoint', (req, res) => { + res.status(501).json({ message: 'This endpoint is under construction.' }); +}); + +export default router; diff --git a/server.js b/server.js index b5fec6fe..039e5db0 100644 --- a/server.js +++ b/server.js @@ -1,30 +1,67 @@ import express from "express"; import cors from "cors"; +import awardsData from "./data/golden-globes.json"; +import listEndpoints from "express-list-endpoints"; -// If you're using one of our datasets, uncomment the appropriate import below -// to get started! -// import avocadoSalesData from "./data/avocado-sales.json"; -// import booksData from "./data/books.json"; -// import goldenGlobesData from "./data/golden-globes.json"; -// import netflixData from "./data/netflix-titles.json"; -// import topMusicData from "./data/top-music.json"; - -// Defines the port the app will run on. Defaults to 8080, but can be overridden -// when starting the server. Example command to overwrite PORT env variable value: -// PORT=9000 npm start -const port = process.env.PORT || 8080; +const port = process.env.PORT || 3000; const app = express(); -// Add middlewares to enable cors and json body parsing app.use(cors()); app.use(express.json()); -// Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + const endpoints = listEndpoints(app).map((endpoint) => ({ + path: endpoint.path, + methods: endpoint.methods, + middlewares: endpoint.middlewares || ["anonymous"], + })); + + res.json({ + message: "Welcome to the Golden Globes API!", + endpoints: endpoints, + }); +}); + +app.get("/awards/winners", (req, res) => { + const winners = awardsData.filter((award) => award.win === true); + res.status(200).json(winners); +}); + +app.get("/awards", (req, res) => { + const { year, category } = req.query; + + let filteredAwards = awardsData; + + if (year) { + filteredAwards = filteredAwards.filter( + (award) => award.year_award === parseInt(year, 10) + ); + } + + if (category) { + filteredAwards = filteredAwards.filter((award) => + award.category.toLowerCase().includes(category.toLowerCase()) + ); + } + + if (filteredAwards.length === 0) { + return res.status(404).json({ error: "No awards found matching the criteria" }); + } + + res.status(200).json(filteredAwards); +}); + +app.get("/awards/:id", (req, res) => { + const id = parseInt(req.params.id, 10); + const award = awardsData[id]; + + if (award) { + res.status(200).json(award); + } else { + res.status(404).json({ error: "Award not found" }); + } }); -// Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });