-
Notifications
You must be signed in to change notification settings - Fork 12
Order Implementation of buy #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import Order from "../models/orderSchema.js"; | ||
|
|
||
| export async function placeBuyOrder(req, res) { | ||
| try { | ||
| console.log("Received body:", req.body); // 🔍 Debug incoming data | ||
|
|
||
| const { user, product, quantity, price } = req.body; | ||
|
|
||
| if (!user || !product || !quantity || !price) { | ||
| return res.status(400).json({ message: "Missing required fields" }); | ||
| } | ||
|
|
||
| const order = new Order({ | ||
| user, | ||
| type: "buy", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type not needed |
||
| product, | ||
| quantity, | ||
| price, | ||
| status: "pending", | ||
| }); | ||
|
|
||
| await order.save(); | ||
|
|
||
| res.status(201).json({ message: "Order saved successfully", order }); | ||
|
|
||
| } catch (err) { | ||
| console.error("Error placing order:", err); // Log the real error | ||
| res.status(500).json({ message: "Failed to place buy order", error: err.message }); | ||
| } | ||
| } | ||
|
|
||
| export async function placeSellOrder(req, res) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sell endpoint not needed |
||
| try { | ||
| console.log("Received body:", req.body); // for debugging | ||
|
|
||
| const { user, product, quantity, price } = req.body; | ||
|
|
||
| if (!user || !product || !quantity || !price) { | ||
| return res.status(400).json({ message: "Missing required fields" }); | ||
| } | ||
|
|
||
| const order = new Order({ | ||
| user, | ||
| type: "sell", // ✅ Only difference | ||
| product, | ||
| quantity, | ||
| price, | ||
| status: "pending", // Optional, for tracking order status | ||
| }); | ||
|
|
||
| await order.save(); | ||
|
|
||
| res.status(201).json({ message: "Sell order placed successfully", order }); | ||
|
|
||
| } catch (err) { | ||
| console.error("Error placing sell order:", err); | ||
| res.status(500).json({ message: "Failed to place sell order", error: err.message }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export async function getUserOrders(req, res) { | ||
| try { | ||
| const orders = await Order.find({ user: req.params.userId }) | ||
| .populate("product") | ||
| .sort({ createdAt: -1 }); | ||
| res.json(orders); | ||
| } catch (err) { | ||
| res.status(500).json({ message: "Failed to fetch orders", error: err }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { Schema, model } from "mongoose"; | ||
|
|
||
| const orderSchema = new Schema({ | ||
| user: { | ||
| type: Schema.Types.ObjectId, | ||
| ref: "social-logins", | ||
| required: true, | ||
| }, | ||
| type: { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type not needed |
||
| type: String, | ||
| enum: ["buy", "sell"], | ||
| required: true, | ||
| }, | ||
| product: { | ||
| type: Schema.Types.ObjectId, | ||
| ref: "Product", | ||
| required: true, | ||
| }, | ||
| quantity: { | ||
| type: Number, | ||
| required: true, | ||
| min: 1, | ||
| }, | ||
| price: { | ||
| type: Number, | ||
| required: true, | ||
| }, | ||
| status: { | ||
| type: String, | ||
| enum: ["pending", "completed", "cancelled"], | ||
| default: "pending", | ||
| }, | ||
| createdAt: { | ||
| type: Date, | ||
| default: Date.now, | ||
| }, | ||
| }); | ||
|
|
||
| const Order = model("Order", orderSchema); | ||
|
|
||
| export default Order; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Router } from "express"; | ||
| const router = Router(); | ||
| import { placeBuyOrder, placeSellOrder, getUserOrders } from "../controllers/orderController.js"; | ||
|
|
||
| // Routes | ||
| router.post("/buy", placeBuyOrder); | ||
| router.post("/sell", placeSellOrder); | ||
|
Comment on lines
+6
to
+7
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these routes are not needed just have a |
||
| router.get("/:userId", getUserOrders); // Get all orders by user | ||
|
|
||
| export default router; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment out debug logs while pushing