product.controller.js 4.3 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
import multer from 'multer';

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

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

const regist = async (req, res) => {
이재연's avatar
이재연 committed
12
    console.log("req.body=", req.body)
Jiwon Yoon's avatar
Jiwon Yoon committed
13
    try {
kusang96's avatar
kusang96 committed
14
        const { pro_name, price, stock, main_category, sub_category, description, colors, sizes } = req.body
kusang96's avatar
dd    
kusang96 committed
15
16
17
18
19
20
21
22
        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({
kusang96's avatar
kusang96 committed
23
            pro_name, price, stock, main_category, sub_category, description, main_imgUrl, detail_imgUrls, colors, sizes
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
27
        }).save()
        res.json(newProduct)
    } catch (error) {
        console.log(error)
kusang96's avatar
dd    
kusang96 committed
28
        res.status(500).send('제품 정보 등록에 실패하였습니다. 다시 진행해 주십시오.')
Jiwon Yoon's avatar
Jiwon Yoon committed
29
30
31
    }
}

kusang96's avatar
kusang96 committed
32
const getToHome = async (req, res) => {
kusang96's avatar
0115    
kusang96 committed
33
    try {
kusang96's avatar
kusang96 committed
34
        const bestProduct = await Product.find({}).sort({ purchase: -1 }).limit(6)
kusang96's avatar
0115    
kusang96 committed
35
        const newProduct = await Product.find({}).sort({ createdAt: -1 }).limit(6)
36
37
        // console.log("best=", bestProduct)
        // console.log("new=", newProduct)
kusang96's avatar
kusang96 committed
38
        res.json({ bestProduct, newProduct })
kusang96's avatar
0115    
kusang96 committed
39
40
41
42
43
    } catch {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}

kusang96's avatar
kusang96 committed
44
const getAll = async (req, res) => {
이재연's avatar
이재연 committed
45
    try {
kusang96's avatar
kusang96 committed
46
47
        const productslist = await Product.find({}).sort({ createdAt: -1 })
        res.json(productslist)
이재연's avatar
0113    
이재연 committed
48
    } catch (error) {
이재연's avatar
이재연 committed
49
50
51
52
53
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}

const getlist = (req, res) => {
kusang96's avatar
kusang96 committed
54
    console.log('get list')
이재연's avatar
0113    
이재연 committed
55
56
57
58
59
60
61
    try {
        res.json(req.productslist)
    } catch (error) {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}

박상호's avatar
박상호 committed
62
const subname = async (req, res) => {
이재연's avatar
0113    
이재연 committed
63
    try {
박상호's avatar
박상호 committed
64
65
        console.log("last subname::: LET ME SEE")
        res.json(req.findsubname)
이재연's avatar
0113    
이재연 committed
66
67
68
69
    } catch (error) {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}
이재연's avatar
dd    
이재연 committed
70

이재연's avatar
0113    
이재연 committed
71
const categoryId = async (req, res, next, category) => {
kusang96's avatar
kusang96 committed
72
    const { search } = req.body
kusang96's avatar
sda    
kusang96 committed
73
    console.log("server search=", search)
이재연's avatar
이재연 committed
74
    try {
kusang96's avatar
kusang96 committed
75
        const productslist = await Product.find({ main_category: category })
kusang96's avatar
sda    
kusang96 committed
76
77
78
        // if (!productslist) {
        //     res.status(404).send('상품을 찾을 수 없습니다.')
        // }
이재연's avatar
0113    
이재연 committed
79
        req.productslist = productslist
박상호's avatar
박상호 committed
80
        console.log("nononono", req.productslist)
이재연's avatar
0113    
이재연 committed
81
        next()
이재연's avatar
이재연 committed
82
    } catch (error) {
이재연's avatar
dd    
이재연 committed
83
84
85
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}
kusang96's avatar
kusang96 committed
86

박상호's avatar
박상호 committed
87
const subcategoryId = async (req, res, next, subname) => {
이재연's avatar
dd    
이재연 committed
88
    try {
박상호's avatar
박상호 committed
89
        console.log("Please===>>>", subname)
kusang96's avatar
kusang96 committed
90
        const findSubname = await Product.find({ sub_category: subname })
박상호's avatar
박상호 committed
91
        console.log("findSubname111=", findSubname)
kusang96's avatar
kusang96 committed
92

박상호's avatar
박상호 committed
93
94
95
96
97
98
99
100
101
        if (!findSubname) {
            const findSubname = {
                _id: 'nothing',
                pro_name: '상품준비중',
                price: 0,
                main_imgUrl:''
            }
            console.log("findSubname2222=", findSubname)
            res.send(findSubname)
이재연's avatar
dd    
이재연 committed
102
        }
박상호's avatar
박상호 committed
103
        res.send(findSubname)
이재연's avatar
dd    
이재연 committed
104
    } catch (error) {
박상호's avatar
박상호 committed
105
        res.send('상품을 불러오지 못했습니다.')
이재연's avatar
dd    
이재연 committed
106
107
108
    }
}

109
110
111
112
113
114
const plusPurchase = async (req, res) => {
    const { products } = req.body
    try {
        for (let i = 0; i < products.length; i++) {
            const count = products[i].count
            const product = await Product.findOne(
115
                { _id: products[i].productId._id }
116
117
            )
            const purchase = product.purchase
118
            const stock = product.stock
119
120
            await Product.updateOne(
                { _id: products[i].productId._id },
121
122
123
124
125
126
                { $set: 
                    { 
                        purchase: count + purchase, 
                        stock: stock - count 
                    } 
                }
127
128
            )
        }
129
        res.send("구매수 늘리기, 재고수 줄이기 성공")
130
131
132
133
134
    } catch (error) {
        res.status(500).send('구매숫자를 늘리지 못함')
    }
}

135
export default { imageUpload, regist, getToHome, getAll, categoryId, getlist, subcategoryId, subname, plusPurchase }