cart.controller.js 2.93 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
28
29
30
31
32
33
const changeCart = async (req, res) => {
    const { userId, products } = req.body
    console.log(products)
    try {
        const cart = await Cart.findOne({ userId: userId })
        await Cart.updateOne(
            { _id: cart._id },
            { $set: { products: products } }
        )
        res.send("카트에 체크가 활성화되었습니다")
    } catch (error) {
        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
const deleteCart = async (req, res) => {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
47
    console.log(req.body)
Jiwon Yoon's avatar
Jiwon Yoon committed
48
    const { userId, cartId } = req.body
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
49
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
50
51
        const cart = await Cart.findOneAndUpdate(
            { userId: userId },
Jiwon Yoon's avatar
Jiwon Yoon committed
52
            { $pull: { products: { _id: cartId } } },
Jiwon Yoon's avatar
Jiwon Yoon committed
53
            { new: true }
Jiwon Yoon's avatar
Jiwon Yoon committed
54
        ).populate({
Jiwon Yoon's avatar
Jiwon Yoon committed
55
56
            path: 'products.productId',
            model: 'Product'
Jiwon Yoon's avatar
Jiwon Yoon committed
57
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
58
        res.json(cart)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
59
60
61
62
63
64
    } catch (error) {
        console.log(error)
        res.status(500).send('해당 카트를 삭제하지 못했습니다.')

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

    }
}

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


103
export default { addCart, changeCart, showCart, deleteCart,deleteCart2, userById }