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

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

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

const regist = async (req, res) => {
    try {
kusang96's avatar
kusang96 committed
13
        const { pro_name, price, stock, main_category, sub_category, description, colors, sizes } = req.body
kusang96's avatar
dd    
kusang96 committed
14
15
16
17
18
19
20
21
        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
22
            pro_name, price, stock, main_category, sub_category, description, main_imgUrl, detail_imgUrls, colors, sizes
Jiwon Yoon's avatar
Jiwon Yoon committed
23
24
25
        }).save()
        res.json(newProduct)
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
26
        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
kusang96 committed
31
const getToHome = async (req, res) => {
kusang96's avatar
0115    
kusang96 committed
32
    try {
kusang96's avatar
kusang96 committed
33
        const bestProduct = await Product.find({}).sort({ purchase: -1 }).limit(6)
kusang96's avatar
0115    
kusang96 committed
34
        const newProduct = await Product.find({}).sort({ createdAt: -1 }).limit(6)
kusang96's avatar
kusang96 committed
35
        res.json({ bestProduct, newProduct })
kusang96's avatar
0115    
kusang96 committed
36
    } catch {
Kim, Subin's avatar
정리    
Kim, Subin committed
37
        console.log(error)
kusang96's avatar
0115    
kusang96 committed
38
39
40
41
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}

kusang96's avatar
kusang96 committed
42
const getAll = async (req, res) => {
43
    const per = 9;
이재연's avatar
0113    
이재연 committed
44
    try {
kusang96's avatar
kusang96 committed
45
46
        if (req.query.product) {
            const productslist = await Product.find({ pro_name: { $regex: new RegExp(req.query.product) } }).sort({ createdAt: -1 })
47
48
            const length = productslist.length
            const productPiece = await Product.find({ pro_name: { $regex: new RegExp(req.query.product) } }).sort({ createdAt: -1 }).skip((req.query.page - 1) * per).limit(per)
kusang96's avatar
kusang96 committed
49
50
51
            if (productslist.length == 0) {
                res.status(404).send('상품을 찾을 수 없습니다.')
            } else {
Kim, Subin's avatar
Kim, Subin committed
52
                res.json({ productPiece, length })
kusang96's avatar
kusang96 committed
53
54
55
            }
        } else {
            const productslist = await Product.find({}).sort({ createdAt: -1 })
56
57
            const length = productslist.length
            const productPiece = await Product.find({}).sort({ createdAt: -1 }).skip((req.query.page - 1) * per).limit(per)
Kim, Subin's avatar
Kim, Subin committed
58
            res.json({ productPiece, length })
kusang96's avatar
kusang96 committed
59
        }
이재연's avatar
0113    
이재연 committed
60
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
61
        console.log(error)
이재연's avatar
0113    
이재연 committed
62
63
64
65
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}

kusang96's avatar
kusang96 committed
66
67
const getlist = (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        if (req.str && req.length) {
            const str = req.str
            const productsPiece = req.productsPiece
            const length = req.length
            res.json({ productsPiece, length, str })
        } else if (req.str) {
            const str = req.str
            const productsPiece = req.productsPiece
            res.json({ productsPiece, str })
        } else {
            const productsPiece = req.productsPiece
            const length = req.length
            res.json({ productsPiece, length })
        }
kusang96's avatar
kusang96 committed
82
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
83
        console.log(error)
kusang96's avatar
kusang96 committed
84
85
86
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}
이재연's avatar
dd    
이재연 committed
87

이재연's avatar
0113    
이재연 committed
88
const categoryId = async (req, res, next, category) => {
89
    const per = 9;
이재연's avatar
0113    
이재연 committed
90
    try {
Kim, Subin's avatar
Kim, Subin committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        if (req.query.product && req.query.method) {
            let method = ''
            let methodStr = ''
            switch (req.query.method) {
                case "purchase":
                    method = "purchase"
                    methodStr = '인기상품'
                    break;
                case "newest":
                    method = "createdAt"
                    methodStr = '신상품'
                    break;
                case "lowest":
                    method = "price"
                    methodStr = '낮은가격'
                    break;
                case "highest":
                    method = "price"
                    methodStr = '높은가격'
                    break;
            }
            req.str = methodStr
            if (req.query.method == 'lowest') {
                let productslist = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).sort({ [method]: 1 })
                let length = productslist.length
                req.length = length
                let productsPiece = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).sort({ [method]: 1 }).skip((req.query.page - 1) * per).limit(per)
                req.productsPiece = productsPiece
kusang96's avatar
kusang96 committed
119
            } else {
Kim, Subin's avatar
Kim, Subin committed
120
121
                let productslist = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).sort({ [method]: -1 })
                let length = productslist.length
122
                req.length = length
Kim, Subin's avatar
Kim, Subin committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                let productsPiece = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).sort({ [method]: -1 }).skip((req.query.page - 1) * per).limit(per)
                req.productsPiece = productsPiece
            }
        } else if (req.query.method) {
            let method = ''
            let methodStr = ''
            switch (req.query.method) {
                case "purchase":
                    method = "purchase"
                    methodStr = '인기상품'
                    break;
                case "newest":
                    method = "createdAt"
                    methodStr = '신상품'
                    break;
                case "lowest":
                    method = "price"
                    methodStr = '낮은가격'
                    break;
                case "highest":
                    method = "price"
                    methodStr = '높은가격'
                    break;
            }
            req.str = methodStr
            if (req.query.method == 'lowest') {
                let productsPiece = await Product.find({ main_category: category }).sort({ [method]: 1 }).skip((req.query.page - 1) * per).limit(per)
                req.productsPiece = productsPiece
            } else {
                let productsPiece = await Product.find({ main_category: category }).sort({ [method]: -1 }).skip((req.query.page - 1) * per).limit(per)
153
                req.productsPiece = productsPiece
kusang96's avatar
kusang96 committed
154
            }
Kim, Subin's avatar
Kim, Subin committed
155
156
157
158
159
160
        } else if (req.query.product) {
            const productslist = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).sort({ createdAt: -1 })
            const length = productslist.length
            req.length = length
            const productsPiece = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).sort({ createdAt: -1 }).skip((req.query.page - 1) * per).limit(per)
            req.productsPiece = productsPiece
kusang96's avatar
kusang96 committed
161
        } else {
Kim, Subin's avatar
Kim, Subin committed
162
            const productslist = await Product.find({ main_category: category }).sort({ createdAt: -1 })
163
164
            const length = productslist.length
            req.length = length
Kim, Subin's avatar
Kim, Subin committed
165
            const productsPiece = await Product.find({ main_category: category }).sort({ createdAt: -1 }).skip((req.query.page - 1) * per).limit(per)
166
            req.productsPiece = productsPiece
kusang96's avatar
kusang96 committed
167
        }
이재연's avatar
0113    
이재연 committed
168
169
        next()
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
170
        console.log(error)
이재연's avatar
0113    
이재연 committed
171
172
173
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}
이재연's avatar
dd    
이재연 committed
174

박상호's avatar
박상호 committed
175
const subname = async (req, res) => {
176
    const per = 9;
kusang96's avatar
kusang96 committed
177
    try {
Kim, Subin's avatar
Kim, Subin committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        if (req.query.method) {
            let method = ''
            let methodStr = ''
            switch (req.query.method) {
                case "purchase":
                    method = "purchase"
                    methodStr = '인기상품'
                    break;
                case "newest":
                    method = "createdAt"
                    methodStr = '신상품'
                    break;
                case "lowest":
                    method = "price"
                    methodStr = '낮은가격'
                    break;
                case "highest":
                    method = "price"
                    methodStr = '높은가격'
                    break;
            }
            let str = methodStr
            if (req.query.method == 'lowest') {
                let productsPiece = await Product.find({ sub_category: req.query.subname }).sort({ [method]: 1 }).skip((req.query.page - 1) * per).limit(per)
                res.send({ productsPiece, str })
            } else {
                let productsPiece = await Product.find({ sub_category: req.query.subname }).sort({ [method]: -1 }).skip((req.query.page - 1) * per).limit(per)
                res.send({ productsPiece, str })
            }
        } else {
            const productslist = await Product.find({ sub_category: req.query.subname }).sort({ createdAt: -1 })
            const length = productslist.length
            const productsPiece = await Product.find({ sub_category: req.query.subname }).sort({ createdAt: -1 }).skip((req.query.page - 1) * per).limit(per)
            res.send({ productsPiece, length })
        }
이재연's avatar
dd    
이재연 committed
213
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
214
        console.log(error)
kusang96's avatar
kusang96 committed
215
        res.status(500).send('상품을 불러오지 못했습니다.')
이재연's avatar
dd    
이재연 committed
216
217
218
    }
}

219
220
221
222
223
224
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(
박상호's avatar
박상호 committed
225
                { _id: products[i].productId._id }
226
227
            )
            const purchase = product.purchase
228
            const stock = product.stock
229
230
            await Product.updateOne(
                { _id: products[i].productId._id },
kusang96's avatar
kusang96 committed
231
232
233
234
235
236
                {
                    $set:
                    {
                        purchase: count + purchase,
                        stock: stock - count
                    }
237
                }
238
239
            )
        }
240
        res.send("구매수 늘리기, 재고수 줄이기 성공")
241
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
242
        console.log(error)
243
244
245
        res.status(500).send('구매숫자를 늘리지 못함')
    }
}
이재연's avatar
dd    
이재연 committed
246

kusang96's avatar
kusang96 committed
247
248
249
250
251
252
253
254
255
const deletePro = async (req, res) => {
    const pro_id = req.query.pro_id
    try {
        const productOne = await Product.findById(pro_id)
        if (productOne) {
            await Product.remove({ _id: pro_id })
        }
        res.send('삭제 성공')
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
256
        console.log(error)
kusang96's avatar
kusang96 committed
257
258
259
260
261
        res.status(500).send('삭제할 상품을 찾지 못하거나 삭제 중 문제가 발생했습니다.')
    }
}

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