From f603e6a0cf319ed58af95affc3846feb98a4447f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hel=C3=A9ne=20Abrahamsson?= Date: Thu, 5 Dec 2024 21:10:28 +0100 Subject: [PATCH 1/3] started project --- package.json | 8 +++++--- server.js | 57 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index f93ddb524..5a6ca4509 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,13 @@ "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", "nodemon": "^3.0.1" + }, + "devDependencies": { + "@babel/core": "^7.26.0", + "@babel/node": "^7.26.0", + "@babel/preset-env": "^7.26.0" } } diff --git a/server.js b/server.js index b5fec6fe2..7e4735f07 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,7 @@ import express from "express"; import cors from "cors"; -// 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"; +import avocadoSalesData from "./data/avocado-sales.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: @@ -21,7 +15,54 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + res.json({ + message: "Welcome to the API!", + documentation: "Below are the available endpoints:", + endpoints: [ + { + method: "GET", + path: "/avocadoSalesData", + description: "Retrieve avocado sales data, optionally filtered by date or region." + }, + { + method: "GET", + path: "/avocadoSalesData/:id", + description: "Retrieve a single avocado sales data entry by ID." + }, + ] + }); +}); + +// Filter avocado sales data by date or region +app.get("/avocadoSalesData", (req, res) => { + const { date, region } = req.query; + + let filteredData = avocadoSalesData; + + if (date) { + filteredData = filteredData.filter(avocados => avocados.date.toLowerCase() === date.toLowerCase()); + } + + if (region) { + filteredData = filteredData.filter(avocados => avocados.region.toLowerCase() === region.toLowerCase()); + + res.status(200).json(filteredData); + } + + +}); + +// Retrieve avocado sales data 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 From 45e3e5a3673309819d1a02cef52c1551f959414b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hel=C3=A9ne=20Abrahamsson?= Date: Thu, 5 Dec 2024 22:20:34 +0100 Subject: [PATCH 2/3] fixed server --- pull_request_template.md | 3 ++- server.js | 34 ++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pull_request_template.md b/pull_request_template.md index 39a2aa32f..b46ac8d6c 100644 --- a/pull_request_template.md +++ b/pull_request_template.md @@ -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 diff --git a/server.js b/server.js index 7e4735f07..bd0fd44eb 100644 --- a/server.js +++ b/server.js @@ -3,9 +3,7 @@ import cors from "cors"; import avocadoSalesData from "./data/avocado-sales.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(); @@ -13,7 +11,7 @@ const app = express(); app.use(cors()); app.use(express.json()); -// Start defining your routes here +// API documentation app.get("/", (req, res) => { res.json({ message: "Welcome to the API!", @@ -22,14 +20,14 @@ app.get("/", (req, res) => { { method: "GET", path: "/avocadoSalesData", - description: "Retrieve avocado sales data, optionally filtered by date or region." + description: "Retrieve avocado sales data, optionally filtered by date or region.", }, { method: "GET", path: "/avocadoSalesData/:id", - description: "Retrieve a single avocado sales data entry by ID." + description: "Retrieve a single avocado sales data entry by ID.", }, - ] + ], }); }); @@ -39,27 +37,35 @@ app.get("/avocadoSalesData", (req, res) => { let filteredData = avocadoSalesData; + // Filter by date if (date) { - filteredData = filteredData.filter(avocados => avocados.date.toLowerCase() === date.toLowerCase()); + filteredData = filteredData.filter( + (avocados) => avocados.date.toLowerCase() === date.toLowerCase() + ); } + // Filter by region if (region) { - filteredData = filteredData.filter(avocados => avocados.region.toLowerCase() === region.toLowerCase()); + 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"); } - - }); -// Retrieve avocado sales data by ID +// Filter by ID app.get("/avocadoSalesData/:id", (req, res) => { const id = req.params.id; - const avocados = avocadoSalesData.find(item => item.id === +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"); } From f145768de0281443a37d8f3a2a2c75006170eac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hel=C3=A9ne=20Abrahamsson?= Date: Sat, 7 Dec 2024 12:21:10 +0100 Subject: [PATCH 3/3] final commit --- package.json | 1 + server.js | 22 ++++++---------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 5a6ca4509..15e8da708 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.1", "nodemon": "^3.0.1" }, "devDependencies": { diff --git a/server.js b/server.js index bd0fd44eb..ddad218b8 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,9 @@ import express from "express"; import cors from "cors"; +import listEndpoints from "express-list-endpoints"; import avocadoSalesData from "./data/avocado-sales.json"; - const port = process.env.PORT || 8080; const app = express(); @@ -11,23 +11,13 @@ const app = express(); app.use(cors()); app.use(express.json()); -// API documentation +// API documentation with endpoint listing app.get("/", (req, res) => { + const endpoints = listEndpoints(app); res.json({ message: "Welcome to the API!", documentation: "Below are the available endpoints:", - endpoints: [ - { - method: "GET", - path: "/avocadoSalesData", - description: "Retrieve avocado sales data, optionally filtered by date or region.", - }, - { - method: "GET", - path: "/avocadoSalesData/:id", - description: "Retrieve a single avocado sales data entry by ID.", - }, - ], + endpoints, }); }); @@ -37,14 +27,14 @@ app.get("/avocadoSalesData", (req, res) => { let filteredData = avocadoSalesData; - // Filter by date + // Filter by date if (date) { filteredData = filteredData.filter( (avocados) => avocados.date.toLowerCase() === date.toLowerCase() ); } - // Filter by region + // Filter by region if (region) { filteredData = filteredData.filter( (avocados) => avocados.region.toLowerCase() === region.toLowerCase()