

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

const storage = require('../../multer');
const ShopImage = require('../../models/shopimage');
const User = require('../../models/user');

router.post('/upload/shop/:shopId', storage.single('images'), async (req, res) => {
    //  console.log(req.body.shopId);
    const images = req.file;
    const shop_id = req.body.shopId

    // console.log(req);

        await ShopImage.create({
            shop_id: req.params.shopId,
            image: images.buffer,
            type: images.mimetype
         });
        res.send('success')
})
//get file
router.get('/file/shop/:shopId', async (req, res) => {
    const existingImage = await ShopImage.findOne({
        where: {
            shop_id: req.params.shopId
        }
    })
    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();
    }
})
router.put('/file/shop/:shopId', storage.single('images') ,async (req, res) => {
    const editImage = req.file;
    const profileimage = {
        image: editImage.buffer
    }

    const shopId = req.params.shopId;

    await ShopImage.update(
        profileimage,
        { where: { shop_id: shopId } }
      )
      res.send('success')
})
module.exports = router;
