From 790b078174b01559b65511e52712b8a672710ed1 Mon Sep 17 00:00:00 2001 From: beingPro007 <120173992+beingPro007@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:42:46 +0530 Subject: [PATCH] just a normal commit for the presentation requirement --- src/controllers/order.controllers.js | 55 ++++++++++++++++++++++ src/controllers/orderDetails.controller.js | 0 src/controllers/product.controller.js | 4 +- src/models/cart.model.js | 7 +++ src/models/product.models.js | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/controllers/order.controllers.js create mode 100644 src/controllers/orderDetails.controller.js create mode 100644 src/models/cart.model.js diff --git a/src/controllers/order.controllers.js b/src/controllers/order.controllers.js new file mode 100644 index 0000000..5c3c18c --- /dev/null +++ b/src/controllers/order.controllers.js @@ -0,0 +1,55 @@ +import { Order } from "../models/orders.models"; +import { ApiError } from "../utils/ApiError"; +import { asynchandler } from "../utils/asynchandler"; + +const getOrders = asynchandler(async(req, res) => { + const page = Number(req.query.pageNumber) || 1; + const pageSize = 3; + const userId = req.user?._id; + + if(!userId){ + throw new ApiError(400, "User not found you need to login first") + } + + const allOrders = Order.aggregate([ + { + $match: { + orderedBy: ObjectId(`${userId}`), + }, + }, + { + $project: { + prodName: 1, + images: 1, + orderStatus: 1, + price : 1, + qty: 1, + grandTotal: 1, + createdAt: 1, + }, + }, + { + $sort: { + createdAt: -1, + } + }, + { + $skip: pageSize * (page - 1), + }, + { + $limit: 3, + } + ]); + + if(!allOrders){ + throw new ApiError(400, "No order found") + } + + res.status(200) + .json(new ApiResponse( + 200, + "All Order fetched Successfully!", + allOrders + )); + +}) \ No newline at end of file diff --git a/src/controllers/orderDetails.controller.js b/src/controllers/orderDetails.controller.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controllers/product.controller.js b/src/controllers/product.controller.js index 4438d4f..acdca8b 100644 --- a/src/controllers/product.controller.js +++ b/src/controllers/product.controller.js @@ -183,6 +183,7 @@ const deleteProduct = asynchandler(async (req, res) => { ); }); + const buyNow = asynchandler(async (req, res) => { const userId = req.user?._id; @@ -207,6 +208,7 @@ const buyNow = asynchandler(async (req, res) => { const order = await Order.create({ orderedBy: userId, product: prodId, + prodName: product.prodName, price: product.price, qty: qty, orderStatus: 'Pending', @@ -231,6 +233,4 @@ const buyNow = asynchandler(async (req, res) => { } }); - - export { addProduct, getCategoryProducts, updateProductDetails, deleteProduct, buyNow }; diff --git a/src/models/cart.model.js b/src/models/cart.model.js new file mode 100644 index 0000000..d6c7d46 --- /dev/null +++ b/src/models/cart.model.js @@ -0,0 +1,7 @@ +import mongoose, {Schema} from "mongoose" + +const cartSchema = new Schema({ + +}, {timestamps: true}); + +const Cart = mongoose.model("Cart", cartSchema); \ No newline at end of file diff --git a/src/models/product.models.js b/src/models/product.models.js index 8ea5e2d..bc5d278 100644 --- a/src/models/product.models.js +++ b/src/models/product.models.js @@ -4,7 +4,7 @@ const productSchema = new Schema( { prodName: { type: String, - required: true, + required: false, index: true, unique: true, // Ensures product names are unique },