Skip to content
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

Project-express-api #524

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"cors": "^2.8.5",
"express": "^4.17.3",
"express-list-endpoints": "^7.1.1",
"nodemon": "^3.0.1"
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/node": "^7.26.0",
"@babel/preset-env": "^7.26.0"
}
}
3 changes: 2 additions & 1 deletion pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Add your Netlify link here.
PS. Don't forget to add it in your readme as well.

## Collaborators
Add any collaborators here.
https://github.com/smily342
https://github.com/Sherrydev11
63 changes: 50 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
import express from "express";
import cors from "cors";
import listEndpoints from "express-list-endpoints";

import avocadoSalesData from "./data/avocado-sales.json";

// 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 app = express();

// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());

// Start defining your routes here
// API documentation with endpoint listing
app.get("/", (req, res) => {
res.send("Hello Technigo!");
const endpoints = listEndpoints(app);
res.json({
message: "Welcome to the API!",
documentation: "Below are the available endpoints:",
endpoints,
});
});

// Filter avocado sales data by date or region
app.get("/avocadoSalesData", (req, res) => {
const { date, region } = req.query;
Comment on lines +25 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

Haha, love the choice of data 😆 🥑 🫶

Copy link
Contributor

Choose a reason for hiding this comment

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

Great that you made use of query params!


let filteredData = avocadoSalesData;

// Filter by date
if (date) {
filteredData = filteredData.filter(
(avocados) => avocados.date.toLowerCase() === date.toLowerCase()
);
}

// Filter by region
if (region) {
filteredData = filteredData.filter(
(avocados) => avocados.region.toLowerCase() === region.toLowerCase()
);
}

// Check if data matches the filters
if (filteredData.length > 0) {
res.status(200).json(filteredData);
} else {
res.status(404).send("Avocado not found");
}
});

// Filter by ID
app.get("/avocadoSalesData/:id", (req, res) => {
const id = req.params.id;

const avocados = avocadoSalesData.find((item) => item.id === +id);
if (avocados) {
res.status(200).json(avocados);
} else {
res.status(404).send("No avocado sales data found with the given ID");
}
});

// Start the server
Expand Down