product.controller.js 5.69 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) {
kusang96's avatar
dd    
kusang96 committed
26
        res.status(500).send('제품 정보 등록에 실패하였습니다. 다시 진행해 주십시오.')
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
    }
}

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

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

kusang96's avatar
kusang96 committed
64
65
const getlist = (req, res) => {
    try {
66
67
68
        const productsPiece = req.productsPiece
        const length = req.length
        res.json({ productsPiece, length })
kusang96's avatar
kusang96 committed
69
70
71
72
    } catch (error) {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}
이재연's avatar
dd    
이재연 committed
73

박상호's avatar
박상호 committed
74

이재연's avatar
0113    
이재연 committed
75
const categoryId = async (req, res, next, category) => {
76
    const per = 9;
이재연's avatar
0113    
이재연 committed
77
    try {
kusang96's avatar
kusang96 committed
78
79
        if (req.query.product) {
            const productslist = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } })
80
81
            const length = productslist.length
            const productsPiece = await Product.find({ main_category: category, pro_name: { $regex: new RegExp(req.query.product) } }).skip((req.query.page - 1) * per).limit(per) 
kusang96's avatar
kusang96 committed
82
83
84
            if (productslist.length == 0) {
                res.status(404).send('상품을 찾을 수 없습니다.')
            } else {
85
86
                req.length = length
                req.productsPiece = productsPiece
kusang96's avatar
kusang96 committed
87
88
89
            }
        } else {
            const productslist = await Product.find({ main_category: category })
90
91
92
93
            const length = productslist.length
            req.length = length
            const productsPiece = await Product.find({ main_category: category }).skip((req.query.page - 1) * per).limit(per)
            req.productsPiece = productsPiece
kusang96's avatar
kusang96 committed
94
        }
이재연's avatar
0113    
이재연 committed
95
96
97
98
99
        next()
    } catch (error) {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
}
이재연's avatar
dd    
이재연 committed
100

박상호's avatar
박상호 committed
101
const subname = async (req, res) => {
102
    const per = 9;
kusang96's avatar
kusang96 committed
103
    try {
104
105
106
107
        const productslist = await Product.find({ sub_category: req.query.subname })
        const length = productslist.length
        const productsPiece = await Product.find({ sub_category: req.query.subname }).skip((req.query.page - 1) * per).limit(per)
        res.send({ productsPiece, length })
이재연's avatar
dd    
이재연 committed
108
    } catch (error) {
kusang96's avatar
kusang96 committed
109
        res.status(500).send('상품을 불러오지 못했습니다.')
이재연's avatar
dd    
이재연 committed
110
111
112
    }
}

113
114
115
116
117
118
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
119
                { _id: products[i].productId._id }
120
121
            )
            const purchase = product.purchase
122
            const stock = product.stock
123
124
            await Product.updateOne(
                { _id: products[i].productId._id },
kusang96's avatar
kusang96 committed
125
126
127
128
129
130
                {
                    $set:
                    {
                        purchase: count + purchase,
                        stock: stock - count
                    }
131
                }
132
133
            )
        }
134
        res.send("구매수 늘리기, 재고수 줄이기 성공")
135
136
137
138
    } catch (error) {
        res.status(500).send('구매숫자를 늘리지 못함')
    }
}
이재연's avatar
dd    
이재연 committed
139

kusang96's avatar
kusang96 committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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) {
        res.status(500).send('삭제할 상품을 찾지 못하거나 삭제 중 문제가 발생했습니다.')
    }
}

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