Skip to content

Commit

Permalink
Add backend for orders
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 committed Oct 26, 2024
1 parent cc94f55 commit 657c015
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MONGO_URI=enter_your_mongo_uri
MONGO_URI=
EMAIL_USER=your_gmail
PORT=3000
EMAIL_PASS=your_16_digit_pass
Expand Down
76 changes: 76 additions & 0 deletions backend/controller/order.controller.js
Original file line number Diff line number Diff line change
@@ -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 });
}
};
22 changes: 16 additions & 6 deletions backend/models/customer.model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// models/Customer.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

Expand All @@ -18,20 +20,28 @@ const customerSchema = new Schema(
},
verificationCode: {
type: String,
default: ""
default: "",
},
role: {
type: String,
default: "customer",
},
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);
Expand Down
36 changes: 36 additions & 0 deletions backend/models/order.model.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 11 additions & 0 deletions backend/routes/orderRouter.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 657c015

Please sign in to comment.