const router = require("express-promise-router")();
const storageshop = require("../../multer");

const WorkImage = require("../../models/workimage");
const Shop = require("../../models/shop");

router.post(
  "/upload/work/:shopId",
  storageshop.single("images"),
  async (req, res) => {
    //  console.log(req.body.shopId);
    const images = req.file;
    const shop_id = req.body.shopId;
    // const user_id = req.body.userId

    // console.log(req);

    await WorkImage.create({
      shop_id: req.params.shopId,
      // user_id: req.params.userId,
      image: images.buffer,
      type: images.mimetype,
    });
    res.send("success");
  }
);
//get file
router.get("/file/shop/:shopId", async (req, res) => {
  const existingImage = await WorkImage.findAll({
    where: {
      shop_id: req.params.shopId,
    },
  });
  console.log(existingImage);
  
  
    let dataimg=[]
    for (let i = 0; i < existingImage.length;i++) {
      const urlimg = existingImage[i].image.toString("base64");
      dataimg.push = {
        url: urlimg,
        type: existingImage[i].type,
      };
      res.status(200).send(dataimg);
    }
});

module.exports = router;
