cart.controller.js 3.12 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
const changeCart = async (req, res) => {
    const { userId, products } = req.body
    console.log(products)
    try {
        const cart = await Cart.findOne({ userId: userId })
Jiwon Yoon's avatar
Jiwon Yoon committed
23
        console.log(cart)
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
27
28
29
30
31
32
33
34
        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
35
    try {
36
        console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
37
38
        const cart = await Cart.findOne({ userId: req.id }).populate({
            path: 'products.productId',
Jiwon Yoon's avatar
Jiwon Yoon committed
39
            model: 'Product'
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
40
41
        })
        res.status(200).json(cart.products)
Jiwon Yoon's avatar
Jiwon Yoon committed
42
        console.log("cart-products : ", cart);
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
43
    } catch (error) {
44
        console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
45
46
47
        console.log(error)
        res.status(500).send('쇼핑카트를 불러오지 못했습니다.')
    }
48
49
}

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

    }
}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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'
            })
        }
83
        res.send("주문완료 및 쇼핑카트에서 삭제")
84
85
86
87
88
89
90
91
        // res.json(cart)
    } catch (error) {
        console.log(error)
        res.status(500).send('해당 카트를 삭제하지 못했습니다.')

    }
}

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


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