Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions backend/controllers/orderController.js
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
Copy link
Collaborator

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


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",
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 });
}
}
41 changes: 41 additions & 0 deletions backend/models/orderSchema.js
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: {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
4 changes: 3 additions & 1 deletion backend/routes/mainRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import authRouter from './authRouter.js';
import listingRouter from './listingRoutes.js';
import orderRoutes from './orderRoutes.js';

const router = express.Router();

Expand All @@ -11,6 +12,7 @@ router.get('/', (req, res) => {
});

router.use('/auth', authRouter);
router.use('/listing', listingRouter);
router.use('/listing', listingRouter);
router.use('/orders', orderRoutes);

export default router;
10 changes: 10 additions & 0 deletions backend/routes/orderRoutes.js
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these routes are not needed just have a / endpoint for buy

router.get("/:userId", getUserOrders); // Get all orders by user

export default router;