cart.controller.js 2.86 KB
Newer Older
1
2
import Cart from "../schemas/Cart.js";

Jiwon Yoon's avatar
Jiwon Yoon committed
3
4
const addCart = async (req, res) => {
    const { userId, products } = req.body
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
5
    try {
6
7
        const cart = await Cart.findOne({ userId: userId })
        await Cart.updateOne(
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
8
            { _id: cart._id },
Jiwon Yoon's avatar
Jiwon Yoon committed
9
            { $push: { products: products } }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
10
        )
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
11
        res.status(200).send('카트에 저장되었습니다.')
12
13
    } catch (error) {
        console.log(error)
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
14
        res.status(500).send('카트에 저장할 수 없습니다.')
15
    }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
16
17
}

Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
20
21
22
23
24
25
26
27
const changeCart = async (req, res) => {
    const { userId, products } = req.body
    try {
        const cart = await Cart.findOne({ userId: userId })
        await Cart.updateOne(
            { _id: cart._id },
            { $set: { products: products } }
        )
        res.send("카트에 체크가 활성화되었습니다")
    } catch (error) {
Kim, Subin's avatar
정리    
Kim, Subin committed
28
        console.log(error)
Jiwon Yoon's avatar
Jiwon Yoon committed
29
30
31
32
33
        res.send("카트 체인지 실패")
    }
}

const showCart = async (req, res) => {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
34
    try {
35
36
        const cart = await Cart.findOne({ userId: req.id }).populate({
            path: 'products.productId',
Jiwon Yoon's avatar
Jiwon Yoon committed
37
            model: 'Product'
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
38
39
40
41
42
43
        })
        res.status(200).json(cart.products)
    } catch (error) {
        console.log(error)
        res.status(500).send('쇼핑카트를 불러오지 못했습니다.')
    }
44
45
}

Jiwon Yoon's avatar
Jiwon Yoon committed
46
47
const deleteCart = async (req, res) => {
    const { userId, cartId } = req.body
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
48
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
49
50
        const cart = await Cart.findOneAndUpdate(
            { userId: userId },
Jiwon Yoon's avatar
Jiwon Yoon committed
51
            { $pull: { products: { _id: cartId } } },
Jiwon Yoon's avatar
Jiwon Yoon committed
52
            { new: true }
Jiwon Yoon's avatar
Jiwon Yoon committed
53
        ).populate({
Jiwon Yoon's avatar
Jiwon Yoon committed
54
55
            path: 'products.productId',
            model: 'Product'
Jiwon Yoon's avatar
Jiwon Yoon committed
56
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
57
        res.json(cart)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
58
59
60
61
62
    } catch (error) {
        console.log(error)
        res.status(500).send('해당 카트를 삭제하지 못했습니다.')
    }
}
Kim, Subin's avatar
정리    
Kim, Subin committed
63

64
65
66
const deleteCart2 = async (req, res) => {
    const { userId, cartId } = req.body
    try {
Kim, Subin's avatar
정리    
Kim, Subin committed
67
        for (let i = 0; i < cartId.length; i++) {
68
69
70
71
72
73
74
75
76
            await Cart.findOneAndUpdate(
                { userId: userId },
                { $pull: { products: { _id: cartId[i] } } },
                { new: true }
            ).populate({
                path: 'products.productId',
                model: 'Product'
            })
        }
77
        res.send("주문완료 및 쇼핑카트에서 삭제")
78
79
80
81
82
83
    } catch (error) {
        console.log(error)
        res.status(500).send('해당 카트를 삭제하지 못했습니다.')
    }
}

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
84
85
86
87
88
89
90
const userById = async (req, res, next, id) => {
    try {
        const cart = await Cart.findOne({ userId: id })
        if (!cart) {
            res.status(404).send("사용자를 찾을 수 없습니다.")
        }
        req.cart = cart
91
        req.id = id
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
92
93
94
95
96
97
        next()
    } catch (error) {
        res.status(500).send("사용자 아이디 검색 실패")
    }
}

Kim, Subin's avatar
정리    
Kim, Subin committed
98
export default { addCart, changeCart, showCart, deleteCart, deleteCart2, userById }