From ea1acb75ec4f8be7fd2109136dd9393fd74fe7cc Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 7 May 2024 14:33:43 +0900 Subject: [PATCH 01/11] update mongoose --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6830a48aa..ac5a542b9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", - "mongoose": "^8.0.0", + "mongoose": "^8.3.4", "nodemon": "^3.0.1" } } From aa51260a91b921fa2646b0705345839b649945b3 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 7 May 2024 15:40:55 +0900 Subject: [PATCH 02/11] update express --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ac5a542b9..01fc4b958 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", - "express": "^4.17.3", + "express": "^4.19.2", "mongoose": "^8.3.4", "nodemon": "^3.0.1" } From 44a430d37618e56bb8908ddbd85d8efa913641e5 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 7 May 2024 15:42:14 +0900 Subject: [PATCH 03/11] addd mongoose --- server.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index 647e7b144..dfd5a2c08 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,7 @@ import express from "express"; import cors from "cors"; import mongoose from "mongoose"; - -// If you're using one of our datasets, uncomment the appropriate import below -// to get started! -// import avocadoSalesData from "./data/avocado-sales.json"; +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"; @@ -14,9 +11,27 @@ const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; -// 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 avocadoSchema = new mongoose.Schema({ + id: Number, + date: String, + averagePrice: Number, + totalVolume: Number, + totalBagSold: Number, + smallBagsSold: Number, + largeBagsSold: Number, + xLargeBagsSold: Number, + region: String, +}); +const Avocado = mongoose.model("Avocado", avocadoSchema); + +const seedDatabase = async () => { + await Avocado.deleteMany({}); + avocadoSalesData.forEach(async (entry) => { + await new Avocado(entry).save(); + }); + console.log("database") +}; + const port = process.env.PORT || 8080; const app = express(); @@ -26,7 +41,7 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + res.send("Welcome to the Avocado Sales API!"); }); // Start the server From 5cc7807f1425a1197ae6310e6c2dca11ab0c0427 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 7 May 2024 15:50:36 +0900 Subject: [PATCH 04/11] add API route in server.js --- server.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index dfd5a2c08..22e04a3e0 100644 --- a/server.js +++ b/server.js @@ -29,7 +29,7 @@ const seedDatabase = async () => { avocadoSalesData.forEach(async (entry) => { await new Avocado(entry).save(); }); - console.log("database") + console.log("database"); }; const port = process.env.PORT || 8080; @@ -39,12 +39,27 @@ const app = express(); app.use(cors()); app.use(express.json()); -// Start defining your routes here +// API route app.get("/", (req, res) => { res.send("Welcome to the Avocado Sales API!"); }); +app.get("/", async (req, res) => { + const avocados = await Avocado.find(); + res.json(avocados); +}); + +app.get("/avocados/:id", async (req, res) => { + const avocado = await Avocado.findOne({ id: parceInt(req.params.id) }); + if (avocado) { + res.json(avocado); + } else { + res.status(404).json({ message: "Not found" }); + } +}); + // Start the server -app.listen(port, () => { +app.listen(port, async () => { console.log(`Server running on http://localhost:${port}`); + await seedDatabase; }); From 66ef892434b2165aca8d6d057149116bb2874c87 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 7 May 2024 15:51:41 +0900 Subject: [PATCH 05/11] add "type": "module" in package json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 01fc4b958..b1d5a1d4a 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "project-mongo-api", "version": "1.0.0", "description": "Starter project to get up and running with express quickly", + "type": "module", "scripts": { "start": "babel-node server.js", "dev": "nodemon server.js --exec babel-node" From ee4989da810537aff6608a4d0720753d01d25847 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 7 May 2024 15:57:23 +0900 Subject: [PATCH 06/11] add assert { type: "json" } --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 22e04a3e0..9dc703848 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,7 @@ import express from "express"; import cors from "cors"; import mongoose from "mongoose"; -import avocadoSalesData from "./data/avocado-sales.json"; +import avocadoSalesData from "./data/avocado-sales.json" assert { type: "json" }; // import booksData from "./data/books.json"; // import goldenGlobesData from "./data/golden-globes.json"; // import netflixData from "./data/netflix-titles.json"; From eeefcfc164c11abd4b544f68ace6e693132d7c16 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 14 May 2024 11:04:22 +0200 Subject: [PATCH 07/11] add express-list-endpoint --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index b1d5a1d4a..3f0f226c0 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.19.2", + "express-list-endpoints": "^7.1.0", "mongoose": "^8.3.4", "nodemon": "^3.0.1" } From 860b28868a90d8e7bf84dfa1ee93d15f58a6087a Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 14 May 2024 11:12:39 +0200 Subject: [PATCH 08/11] fix fix several parts --- server.js | 97 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 33 deletions(-) diff --git a/server.js b/server.js index 9dc703848..84f272527 100644 --- a/server.js +++ b/server.js @@ -1,65 +1,96 @@ import express from "express"; import cors from "cors"; import mongoose from "mongoose"; +import expressListEndpoints from "express-list-endpoints"; import avocadoSalesData from "./data/avocado-sales.json" assert { type: "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"; +// Database connection const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; -mongoose.connect(mongoUrl); +mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true }); mongoose.Promise = Promise; -const avocadoSchema = new mongoose.Schema({ +const app = express(); +const port = process.env.PORT || 8080; + +// Avocado sales model +const AvocadoSales = mongoose.model("AvocadoSales", { id: Number, - date: String, + date: Date, averagePrice: Number, totalVolume: Number, - totalBagSold: Number, + totalBagsSold: Number, smallBagsSold: Number, largeBagsSold: Number, xLargeBagsSold: Number, region: String, }); -const Avocado = mongoose.model("Avocado", avocadoSchema); -const seedDatabase = async () => { - await Avocado.deleteMany({}); - avocadoSalesData.forEach(async (entry) => { - await new Avocado(entry).save(); - }); - console.log("database"); -}; - -const port = process.env.PORT || 8080; -const app = express(); +// Seed database if required +if (process.env.RESET_DB) { + const seedDatabase = async () => { + await AvocadoSales.deleteMany(); + const salesDocuments = avocadoSalesData.map( + (salesData) => new AvocadoSales(salesData) + ); + await AvocadoSales.insertMany(salesDocuments); + console.log("Database seeded"); + }; + seedDatabase(); +} -// Add middlewares to enable cors and json body parsing +// Middleware app.use(cors()); app.use(express.json()); -// API route +// API documentation endpoint app.get("/", (req, res) => { - res.send("Welcome to the Avocado Sales API!"); + res.json( + expressListEndpoints(app).map((endpoint) => ({ + method: endpoint.methods.join(", "), + path: endpoint.path, + description: "Describe what each endpoint does", // Consider adding more detailed descriptions + })) + ); }); -app.get("/", async (req, res) => { - const avocados = await Avocado.find(); - res.json(avocados); +// List all sales +app.get("/avocado-sales", async (req, res) => { + try { + const sales = await AvocadoSales.find().select("-_id -__v").sort({ id: 1 }); + res.json(sales); + } catch (error) { + res.status(500).json({ error: "Failed to retrieve sales data" }); + } +}); + +// Get sales by region +app.get("/avocado-sales/region/:regionName", async (req, res) => { + const { regionName } = req.params; + try { + const sales = await AvocadoSales.find({ region: regionName }) + .select("-_id -__v") + .sort({ id: 1 }); + res.json(sales); + } catch (error) { + res.status(500).json({ error: "Failed to retrieve sales by region" }); + } }); -app.get("/avocados/:id", async (req, res) => { - const avocado = await Avocado.findOne({ id: parceInt(req.params.id) }); - if (avocado) { - res.json(avocado); - } else { - res.status(404).json({ message: "Not found" }); +// Get sales by specific date +app.get("/avocado-sales/date/:date", async (req, res) => { + const { date } = req.params; + try { + const formattedDate = new Date(date).toISOString(); + const sales = await AvocadoSales.find({ date: formattedDate }) + .select("-_id -__v") + .sort({ id: 1 }); + res.json(sales); + } catch (error) { + res.status(500).json({ error: "Failed to retrieve sales by date" }); } }); // Start the server -app.listen(port, async () => { +app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); - await seedDatabase; }); From 84faae90563e928b8df04872acdc4019aaab3da4 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 14 May 2024 11:27:46 +0200 Subject: [PATCH 09/11] update readme file --- README.md | 9 +++------ package.json | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 35019cd8a..d8956e58b 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,10 @@ # Project Mongo API - -Replace this readme with your own information about your project. - -Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. +This project was designed to develop a RESTful API for avocado sales data, utilizing Express.js and MongoDB to manage the database and implement API endpoints effectively. ## The problem -Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? +The main challenge was to create efficient queries to retrieve and manipulate data according to the API's requirements. If I had more time, I would implement additional features like authentication and more complex data aggregation to enhance the API's functionality. ## View it live -Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. + diff --git a/package.json b/package.json index 3f0f226c0..f2dde5dc7 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "express": "^4.19.2", "express-list-endpoints": "^7.1.0", "mongoose": "^8.3.4", - "nodemon": "^3.0.1" + "nodemon": "^3.0.1", + "dotenv": "^16.0.0" } } From f740227620703223c367a6a9827940472b57feed Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 14 May 2024 11:35:20 +0200 Subject: [PATCH 10/11] add link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d8956e58b..1c1aa7e59 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,4 @@ The main challenge was to create efficient queries to retrieve and manipulate da ## View it live - +https://project-mongo-api-ude1.onrender.com \ No newline at end of file From 0bd6c6dd4b0849682c559461e410d191edddc227 Mon Sep 17 00:00:00 2001 From: Mai Kanetaka Date: Tue, 14 May 2024 21:26:05 +0200 Subject: [PATCH 11/11] fix the bugs --- package.json | 2 +- server.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index f2dde5dc7..c0e9aec7e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "start": "babel-node server.js", - "dev": "nodemon server.js --exec babel-node" + "dev": "nodemon -r dotenv/config server.js --exec babel-node" }, "author": "", "license": "ISC", diff --git a/server.js b/server.js index 84f272527..038748352 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,7 @@ import express from "express"; import cors from "cors"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; -import avocadoSalesData from "./data/avocado-sales.json" assert { type: "json" }; +import avocadoSalesData from "./data/avocado-sales.json" with { type: "json" }; // Database connection const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; @@ -48,7 +48,7 @@ app.get("/", (req, res) => { expressListEndpoints(app).map((endpoint) => ({ method: endpoint.methods.join(", "), path: endpoint.path, - description: "Describe what each endpoint does", // Consider adding more detailed descriptions + description: "Describe what each endpoint does", })) ); });