forked from RamakrushnaBiswal/PlayCafe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request RamakrushnaBiswal#405 from haseebzaki-07/new_branch_4
Add backend for orders
- Loading branch information
Showing
7 changed files
with
181 additions
and
86 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
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
Oops, something went wrong.