Skip to content

Commit

Permalink
Create Place Order APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
elyse502 committed Nov 8, 2024
1 parent 7d48d11 commit db1da92
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
50 changes: 50 additions & 0 deletions backend/controllers/orderController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import orderModel from "../models/orderModel.js";
import userModel from "../models/userModel";

// Placing orders using COD Method
const placeOrder = async (req, res) => {
try {
const { userId, items, amount, address } = req.body;

const orderData = {
userId,
items,
address,
amount,
paymentMethod: "COD",
payment: false,
date: Date.now(),
};

const newOrder = new orderModel(orderData);
await newOrder.save();

await userModel.findByIdAndUpdate(userId, { cartData: {} });

res.json({ success: true, message: "Order Placed" });
} catch (error) {
console.log(error);
res.json({ success: false, message: error.message });
}
};

// Placing orders using Stripe Method
const placeOrderStripe = async (req, res) => {};

// All Orders data for Admin Panel
const allOrders = async (req, res) => {};

// User Order Data for Frontend
const userOrders = async (req, res) => {};

// update order status from Admin Panel
const updateStatus = async (req, res) => {};

export {
placeOrder,
placeOrderStripe,
placeOrderRazorpay,
allOrders,
userOrders,
updateStatus,
};
16 changes: 16 additions & 0 deletions backend/models/orderModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from "mongoose";

const orderSchema = new mongoose.Schema({
userId: { type: String, required: true },
items: { type: Object, required: true },
amount: { type: Number, required: true },
address: { type: Object, required: true },
status: { type: String, required: true, default: "Order Placed" },
paymentMethod: { type: String, required: true },
payment: { type: Boolean, required: true, default: false },
date: { type: Number, required: true },
});

const orderModel =
mongoose.models.order || mongoose.model("order", orderSchema);
export default orderModel;
25 changes: 25 additions & 0 deletions backend/routes/orderRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from "express";
import {
placeOrder,
placeOrderStripe,
allOrders,
userOrders,
updateStatus,
} from "../controllers/orderController.js";
import adminAuth from "../middleware/adminAuth.js";
import authUser from "../middleware/auth.js";

const orderRouter = express.Router();

// Admin Features
orderRouter.post("/list", adminAuth, allOrders);
orderRouter.post("/status", adminAuth, updateStatus);

// Payment Features
orderRouter.post("/place", authUser, placeOrder);
orderRouter.post("/stripe", authUser, placeOrderStripe);

// user Feature
orderRouter.post("/userorders", authUser, userOrders);

export default orderRouter;
2 changes: 2 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import connectCloudinary from "./config/cloudinary.js";
import userRouter from "./routes/userRoute.js";
import productRouter from "./routes/productRoute.js";
import cartRouter from "./routes/cartRoute.js";
import orderRouter from "./routes/orderRoute.js";

// App Config
const app = express();
Expand All @@ -21,6 +22,7 @@ app.use(cors());
app.use("/api/user", userRouter);
app.use("/api/product", productRouter);
app.use("/api/cart", cartRouter);
app.use("/api/order", orderRouter);

app.get("/", (req, res) => {
res.send("API Working");
Expand Down

0 comments on commit db1da92

Please sign in to comment.