From 657c01523bb7f2b4b93abeb095427c5edbc31260 Mon Sep 17 00:00:00 2001 From: haseebzaki-07 Date: Sat, 26 Oct 2024 23:04:00 +0530 Subject: [PATCH 1/2] Add backend for orders --- backend/.env.example | 2 +- backend/controller/order.controller.js | 76 ++++++++++++++++++++++++++ backend/models/customer.model.js | 22 ++++++-- backend/models/order.model.js | 36 ++++++++++++ backend/routes/index.js | 2 + backend/routes/orderRouter.js | 11 ++++ 6 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 backend/controller/order.controller.js create mode 100644 backend/models/order.model.js create mode 100644 backend/routes/orderRouter.js diff --git a/backend/.env.example b/backend/.env.example index 8ccf528c..8c71fad6 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -1,4 +1,4 @@ -MONGO_URI=enter_your_mongo_uri +MONGO_URI= EMAIL_USER=your_gmail PORT=3000 EMAIL_PASS=your_16_digit_pass diff --git a/backend/controller/order.controller.js b/backend/controller/order.controller.js new file mode 100644 index 00000000..24132926 --- /dev/null +++ b/backend/controller/order.controller.js @@ -0,0 +1,76 @@ +const Customer = require("../models/customer.model"); +const Order = require("../models/order.model"); + +// Create a new order +exports.createOrder = async (req, res) => { + try { + const { items } = req.body; + const customerId = req.params.id.trim(); + + if (!customerId) { + return res + .status(400) + .json({ success: false, message: "Customer ID is required." }); + } + + const totalAmount = items.reduce( + (sum, item) => sum + item.price * item.quantity, + 0 + ); + + const order = new Order({ + customer: customerId, + items, + totalAmount, + }); + + const savedOrder = await order.save(); + + await Customer.findByIdAndUpdate(customerId, { + $push: { orders: savedOrder._id }, + }); + + res.status(201).json({ success: true, order: savedOrder }); + } catch (error) { + res.status(500).json({ success: false, message: error.message }); + } +}; + +// Get all orders for a customer +exports.getOrders = async (req, res) => { + try { + const customerId = req.params.id.trim(); + + const orders = await Order.find({ customer: customerId }); + res.status(200).json({ success: true, orders }); + } catch (error) { + res.status(500).json({ success: false, message: error.message }); + } +}; + +// Delete an order +exports.deleteOrder = async (req, res) => { + try { + const { orderId } = req.body; + const customerId = req.params.id.trim(); + + const order = await Order.findOne({ _id: orderId, customer: customerId }); + + if (!order) { + return res + .status(404) + .json({ success: false, message: "Order not found" }); + } + + await Order.deleteOne({ _id: orderId }); + await Customer.findByIdAndUpdate(customerId, { + $pull: { orders: orderId }, + }); + + res + .status(200) + .json({ success: true, message: "Order deleted successfully" }); + } catch (error) { + res.status(500).json({ success: false, message: error.message }); + } +}; diff --git a/backend/models/customer.model.js b/backend/models/customer.model.js index 6047d222..1622fc0b 100644 --- a/backend/models/customer.model.js +++ b/backend/models/customer.model.js @@ -1,3 +1,5 @@ +// models/Customer.js + const mongoose = require("mongoose"); const Schema = mongoose.Schema; @@ -18,7 +20,7 @@ const customerSchema = new Schema( }, verificationCode: { type: String, - default: "" + default: "", }, role: { type: String, @@ -26,12 +28,20 @@ const customerSchema = new Schema( }, bio: String, profilePicture: String, - bookedEvents: [{ - type: Schema.Types.ObjectId, - ref: "Event", // Reference to the Event model - }], + bookedEvents: [ + { + type: Schema.Types.ObjectId, + ref: "Event", + }, + ], + orders: [ + { + type: Schema.Types.ObjectId, + ref: "Order", + }, + ], }, - { timestamps: true }, + { timestamps: true } ); const Customer = mongoose.model("Customer", customerSchema); diff --git a/backend/models/order.model.js b/backend/models/order.model.js new file mode 100644 index 00000000..76c4cfd5 --- /dev/null +++ b/backend/models/order.model.js @@ -0,0 +1,36 @@ +// models/Order.js + +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const orderSchema = new Schema( + { + customer: { + type: Schema.Types.ObjectId, + ref: "Customer", + required: true, + }, + items: [ + { + name: { type: String, required: true }, + description: String, + quantity: { type: Number, required: true, min: 1 }, + price: { type: Number, required: true }, + }, + ], + totalAmount: { + type: Number, + required: true, + }, + status: { + type: String, + enum: ["pending", "completed", "cancelled"], + default: "pending", + }, + }, + { timestamps: true } +); + +const Order = mongoose.model("Order", orderSchema); + +module.exports = Order; diff --git a/backend/routes/index.js b/backend/routes/index.js index 27a2b8bd..bb18bae4 100644 --- a/backend/routes/index.js +++ b/backend/routes/index.js @@ -54,4 +54,6 @@ router.use("/user", require("./customerRouter")); router.use("/reservation", require("./reservationRouter")); router.use("/newsletter", require("./newsletterRoute")); router.use("/forgot", require("./forgotRouter")); +router.use("/order", require("./orderRouter")); + module.exports = router; diff --git a/backend/routes/orderRouter.js b/backend/routes/orderRouter.js new file mode 100644 index 00000000..591b2b8c --- /dev/null +++ b/backend/routes/orderRouter.js @@ -0,0 +1,11 @@ +const express = require("express"); +const { createOrder, getOrders, deleteOrder } = require("../controller/order.controller.js"); + +const router = express.Router(); + + +router.post("/create/:id", createOrder); +router.get("/get/:id", getOrders); +router.delete("/delete/:id", deleteOrder); + +module.exports = router; From 9314d45ac299fffa02e6318e9a7b5b308d10e404 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 26 Oct 2024 17:37:50 +0000 Subject: [PATCH 2/2] docs(contributor): contrib-readme-action has updated readme --- README.md | 120 +++++++++++++++++------------------------------------- 1 file changed, 38 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index cf249873..665d77d7 100644 --- a/README.md +++ b/README.md @@ -259,20 +259,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Arindam - - - jainaryan04 -
- Aryan Ramesh Jain -
- - - - haseebzaki-07 -
- Haseeb Zaki -
- alo7lika @@ -280,8 +266,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made alolika bhowmik - - Ashwinib26 @@ -296,6 +280,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Mahera Nayan + + tejasbenibagde @@ -310,13 +296,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Tyarla Shirisha - - - VinayLodhi1712 -
- Vinay Anand Lodhi -
- Amnyadav @@ -324,8 +303,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Aman Yadav - - NilanchalaPanda @@ -334,17 +311,10 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - Suhas-Koheda -
- Suhas Koheda -
- - - - Sumanbhadra + + haseebzaki-07
- Suman Bhadra + Haseeb Zaki
@@ -354,22 +324,22 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sawan kushwah + + - - PavanTeja2005 + + Suhas-Koheda
- PavanTeja2005 + Suhas Koheda
- - sajalbatra + + Jay-1409
- Sajal Batra + Jay shah
- - vishnuprasad2004 @@ -378,10 +348,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - Jay-1409 + + sajalbatra
- Jay shah + Sajal Batra +
+ + + + PavanTeja2005 +
+ PavanTeja2005
@@ -391,6 +368,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Abhijit Motekar + + Navneetdadhich @@ -398,6 +377,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Navneet Dadhich + + + VinayLodhi1712 +
+ Vinay Anand Lodhi +
+ lade6501 @@ -412,8 +398,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made MD REHAN - - T-Rahul-prabhu-38 @@ -428,13 +412,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Aditya Bakshi - - - vaishnavipal1869 -
- vaishnavipal1869 -
- + + tanishirai @@ -456,8 +435,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sourabh Singh Rawat - - Shiva-Bajpai @@ -465,13 +442,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Shiva Bajpai - - - Pushpa472 -
- Pushpa Vishwakarma -
- devxMani @@ -486,6 +456,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Ayush Yadav + + smog-root @@ -500,8 +472,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Vaibhav._Y - - Vaibhav-Kumar-K-R @@ -530,13 +500,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sapna Kul - - - Nikhil0-3 -
- Nikhil More -
- + + MutiatBash @@ -544,8 +509,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Bashua Mutiat - - Mohitranag18 @@ -560,13 +523,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Jai Dhingra - - - harjasae2001 -
- Harjas Singh -
- mishradev1