From 657c01523bb7f2b4b93abeb095427c5edbc31260 Mon Sep 17 00:00:00 2001 From: haseebzaki-07 Date: Sat, 26 Oct 2024 23:04:00 +0530 Subject: [PATCH] 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;