const router = require("express-promise-router")();

const storagebeautician = require("../../multer");
const BeauticianImage = require("../../models/beauticianImg");
const User = require("../../models/user");

router.post(
  "/upload/beautician/:beauticianId",
  storagebeautician.single("images"),
  async (req, res) => {
    //  console.log(req.body.shopId);
    const images = req.file;
    const beautician_id = req.params.beauticianId;
    // console.log(req);

    await BeauticianImage.create({
      beauticians_id: req.params.beauticianId,
      image: images.buffer,
      type: images.mimetype,
    });
    res.send("success");
  }
);
//get file
router.get("/file/beautician/:beauticianId", async (req, res) => {
  const existingImage = await BeauticianImage.findAll({
    where: {
      beautician_id: req.params.beauticianId,
    },
  });
  if (existingImage) {
    const urlimg = existingImage.image.toString("base64");
    const dataimg = {
      url: urlimg,
      type: existingImage.type,
    };
    res.status(200).send(dataimg);
  } else {
    res.status(200).send();
  }
});
module.exports = router;
