

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 ShopImage.findAll({
        where: {
            shop_id: req.params.shopId
        }
    })
    const urlimg = existingImage.image.toString('base64');
    const dataimg = {
        url: urlimg,
        type: existingImage.type
    }
    res.status(200).send(dataimg);
})

module.exports = router;
