Merge branch 'master' of http://128.199.30.231/possibillion/smellika-api
This commit is contained in:
commit
a65e35aef6
@ -1,7 +1,7 @@
|
|||||||
import { Product } from "./ProductModel.js";
|
import { Product } from "./ProductModel.js";
|
||||||
import cloudinary from "../../Utils/cloudinary.js";
|
import cloudinary from "../../Utils/cloudinary.js";
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
import { CategoryModel } from "../Category/CategoryModel.js";
|
||||||
export const createProduct = async (req, res) => {
|
export const createProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.files) {
|
if (!req.files) {
|
||||||
@ -240,13 +240,20 @@ export const deleteProduct = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getProductsByCategory = async (req, res) => {
|
export const getProductsByCategory = async (req, res) => {
|
||||||
const { categoryName } = req.params; // Assuming category name is in the route
|
const { categoryName } = req.params; // Assuming category name is in the route
|
||||||
|
// console.log(categoryName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const products = await Product.find({
|
// Find the category object by name first
|
||||||
category: categoryName,
|
const category = await CategoryModel.findOne({ categoryName });
|
||||||
}).sort({ createdAt: -1 });
|
|
||||||
|
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) {
|
if (products && products.length > 0) {
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
|
Loading…
Reference in New Issue
Block a user