text: const express = require("express");
  const router = express.Router();
  const mongoose = require("mongoose");
  const multer = require("multer");
  
  const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, "./uploads/");
    },
    filename: function (req, file, cb) {
      cb(null, Date.now() + file.originalname);
    },
  });
  
  const fileFilter = (req, file, cb) => {
    // reject a file
    if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") {
      cb(null, true);
    } else {
      cb(null, false);
    }
  };
  
  const upload = multer({
    storage: storage,
    limits: {
      fileSize: 1024 * 1024 * 5,
    },
    fileFilter: fileFilter,
  });
  
  const Product = require("../models/product");
  // const { update } = require("../models/product");
  
  router.get("/", (req, res, next) => {
    Product.find()
      .select("name price _id productImage")
      .exec()
      .then((docs) => {
        const response = {
          count: docs.length,
          product: docs.map((doc) => {
            return {
              name: doc.name,
              price: doc.price,
              _id: doc._id,
              productImage: doc.productImage,
              response: {
                type: "GET",
                url: "http://localhost:3030/products/" + doc._id,
              },
            };
          }),
        };
        // if (doc.length >= 0) {
        res.status(200).json(response);
        // } else {
        //   res.status(404).json({
        //     message: "no entries found",
        //   });
        // }
      })
      .catch((err) => {
        console.log(err);
        res.status(500).json({
          error: err,
        });
      });
  });
  
  router.post("/", upload.single("productImage"), (req, res, next) => {
    const product = new Product({
      _id: new mongoose.Types.ObjectId(),
      name: req.body.name,
      price: req.body.price,
      productImage: req.file.path,
    });
    product
      .save()
      .then((result) => {
        console.log(result);
        res.status(201).json({
          message: "Created product Successfully",
          createdProduct: {
            name: result.name,
            price: result.price,
            _id: result._id,
            request: {
              type: "GET",
              url: "http://localhost:3030/products/" + result._id,
            },
          },
        });
      })
      .catch((err) => {
        console.log(err);
        res.status(500).json({
          error: err,
        });
      });
  });
  
  router.get("/:productId", (req, res, next) => {
    const id = req.params.productId;
    Product.findById(id)
      .select("name price _id productImage")
      .exec()
      .then((doc) => {
        console.log("From database", doc);
        if (doc) {
          res.status(200).json({
            product: doc,
            request: {
              type: "GET",
              description: "Get all products",
              url: "http://localhost:3030/products",
            },
          });
        } else {
          res.status(404).json({ message: "no id for validation" });
        }
        // res.status(200).json(doc);
      })
      .catch((err) => {
        console.log(err);
        res.status(500).json({ error: err });
      });
  });
  
  router.patch("/:productId", (req, res, next) => {
    const id = req.params.productId;
    const updateOps = {};
    for (const ops of req.body) {
      updateOps[ops.propName] = ops.value;
    }
    Product.update({ _id: id }, { $set: updateOps })
      .exec()
      .then((result) => {
        console.log(result);
        res.status(200).json({
          message: "product updated",
          request: {
            type: "GET",
            url: "http://localhost:3030/products/" + id,
          },
        });
      })
      .catch((err) => {
        console.log(err);
        res.status(500).json({
          error: err,
        });
      });
  });
  
  router.delete("/:productId", (req, res, next) => {
    const id = req.params.productId;
    Product.remove({ _id: id })
      .exec()
      .then((result) => {
        res.status(200).json({
          message: "product deleted",
          request: {
            type: "POST",
            url: "http://localhost:3030/products/",
            body: { name: "String", price: "Number" },
          },
        });
      })
      .catch((err) => {
        console.log(err);
        res.status(500).json({
          error: err,
        });
      });
  });
  
  module.exports = router;
  