You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

featured-products.js 879B

123456789101112131415161718192021222324252627282930313233343536
  1. const Product = require('../../../models/product');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. async function handler(req, res) {
  4. const { method } = req;
  5. await dbConnect();
  6. switch (method) {
  7. case 'GET': {
  8. try {
  9. const featuredProducts = await Product.find({ isFeatured: true });
  10. if (!featuredProducts) {
  11. res.status(200).json({
  12. message: 'There are no featured products right now.',
  13. featuredProducts: [],
  14. });
  15. }
  16. res.status(200).json({
  17. message: 'Featured products were fetched successfully.',
  18. featuredProducts,
  19. });
  20. } catch (error) {
  21. res.status(400).json({ message: error.message });
  22. }
  23. break;
  24. }
  25. default:
  26. res.status(405).json({ message: 'Method not allowed' });
  27. break;
  28. }
  29. }
  30. export default handler;