category wise product find controller updated
This commit is contained in:
parent
fd6a8e9c9b
commit
d294d2706b
@ -1,7 +1,7 @@
|
||||
import { Product } from "./ProductModel.js";
|
||||
import cloudinary from "../../Utils/cloudinary.js";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import { CategoryModel } from "../Category/CategoryModel.js";
|
||||
export const createProduct = async (req, res) => {
|
||||
try {
|
||||
if (!req.files) {
|
||||
@ -240,13 +240,20 @@ export const deleteProduct = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getProductsByCategory = async (req, res) => {
|
||||
const { categoryName } = req.params; // Assuming category name is in the route
|
||||
// console.log(categoryName);
|
||||
|
||||
try {
|
||||
const products = await Product.find({
|
||||
category: categoryName,
|
||||
}).sort({ createdAt: -1 });
|
||||
// Find the category object by name first
|
||||
const category = await CategoryModel.findOne({ categoryName });
|
||||
|
||||
if (!category) {
|
||||
throw new Error("Category not found");
|
||||
}
|
||||
const products = await Product.find({ category: category._id }).populate('category');
|
||||
// console.log(products);
|
||||
|
||||
if (products && products.length > 0) {
|
||||
return res.status(200).json({
|
||||
|
Loading…
Reference in New Issue
Block a user