product.controller.js 1.02 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import Product from "../schemas/Product.js";
kusang96's avatar
dd    
kusang96 committed
2
3
4
5
6
7
8
9
import multer from 'multer';

const upload = multer({ dest: 'uploads/' })

const imageUpload = upload.fields([
    { name: 'main_image' },
    { name: 'detail_image' }
  ])
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12

const regist = async (req, res) => {
    try {
kusang96's avatar
dd    
kusang96 committed
13
14
15
16
17
18
19
20
21
22
        const { pro_name, price, stock, main_category, sub_category, description } = req.body
        const main_img = req.files['main_image'][0]
        const detail_img = req.files['detail_image']
        const main_imgUrl = main_img.filename
        const detail_imgUrls = []
        detail_img.forEach(file => {
            detail_imgUrls.push(file.filename)
        })
        const newProduct = await new Product({
            pro_name, price, stock, main_category, sub_category, description, main_imgUrl, detail_imgUrls
Jiwon Yoon's avatar
Jiwon Yoon committed
23
24
25
26
        }).save()
        res.json(newProduct)
    } catch (error) {
        console.log(error)
kusang96's avatar
dd    
kusang96 committed
27
        res.status(500).send('제품 정보 등록에 실패하였습니다. 다시 진행해 주십시오.')
Jiwon Yoon's avatar
Jiwon Yoon committed
28
29
30
    }
}

kusang96's avatar
dd    
kusang96 committed
31
export default { imageUpload, regist }