-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc94f55
commit 657c015
Showing
6 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |