-
Notifications
You must be signed in to change notification settings - Fork 530
Week 17: API #531
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
base: master
Are you sure you want to change the base?
Week 17: API #531
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,67 @@ | ||
| import express from "express"; | ||
| import cors from "cors"; | ||
| import awardsData from "./data/golden-globes.json"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how your API can function when I can't see any data folder in your repo 😅
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HIPPIEKICK what do you mean exactly by not seeing a data folder? :')) |
||
| 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); | ||
| }); | ||
|
Comment on lines
+25
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These endpoints could be one if you make use of another query param, such as |
||
|
|
||
| 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}`); | ||
| }); | ||

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.
Is this file used? 👀