product.controller.js 1.05 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import Product from "../schemas/Product.js";
Jiwon Yoon's avatar
0111    
Jiwon Yoon committed
2
3
4
import multer from 'multer';


Jiwon Yoon's avatar
Jiwon Yoon committed
5
const upload = multer({ dest: 'uploads/' });
Jiwon Yoon's avatar
0111    
Jiwon Yoon committed
6
7
8
9

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

const regist = async (req, res) => {
    console.log('req.body=', req.body)
Jiwon Yoon's avatar
Jiwon Yoon committed
14
    const { pro_name, price, stock, main_category, sub_category, colors, sizes, description } = req.body
Jiwon Yoon's avatar
0111    
Jiwon Yoon committed
15
16
17
18
    console.log(req.files)
    const main_image = req.files['main_image'][0].filename
    console.log(main_image)
    const detail_image = req.files['detail_image'][0].filename
Jiwon Yoon's avatar
Jiwon Yoon committed
19
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
        const newProduct = await new Product({
            pro_name, price, stock, main_category, sub_category, colors, sizes, description, main_image, detail_image
Jiwon Yoon's avatar
Jiwon Yoon committed
22
23
24
25
26
27
28
29
        }).save()
        res.json(newProduct)
    } catch (error) {
        console.log(error)
        res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
30
31
32
33
34

const getProduct = (req, res) => {
    res.json(req.body)
}

Jiwon Yoon's avatar
Jiwon Yoon committed
35
export default { regist, fileUpload, getProduct }