const express = require('express');
const router = express.Router();

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

router.post('/upload/shop/:shopId', storage.single('image'), async (req, res) => {
    const images = req.files;
    for (let i = 0; i < images.length; i++) {
        await ShopImage.create({ 
            shop_id: req.params.shopId,
            image: images[i].buffer,
            type: images[i].mimetype
        });
    }
    res.send('success')
})
//get file
router.get('/file/Shop/:imageId', async (req, res) => {
    const existingImage = await ShopImage.findOne({
        where: {
            image_id: req.params.imageId
        }
    })
    const url = existingImage.image.toString('base64');
    res.status(200).send(url);
})

