order.controller.js 1.3 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import Order from "../schemas/Order.js";
2
import User from "../schemas/User.js";
Jiwon Yoon's avatar
Jiwon Yoon committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

const addorder = async (req, res) => {
    const { userId, products, receiverInfo, total } = req.body
    try {
        const newOrder = await new Order({
            userId, products, receiverInfo, total
        }).save()
        res.status(200).send('Order DB에 저장 완료')
    } catch (error) {
        console.log(error)
        res.status(500).send('Order DB에 저장 실패')
    }
}

const showorder = async (req, res) => {
    try {
19
        const order = await Order.find({ userId: req.userId }).sort({_id:-1}).limit(1).populate({
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
            path: 'products.productId',
            model: 'Product'
        })
23
24
        console.log(order)
        res.status(200).json(order[0])
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
27
28
29
30
31
    } catch (error) {
        console.log(error)
        res.status(500).send('쇼핑카트를 불러오지 못했습니다.')
    }
}


32
33
34
35
36
37
38
39
40
41
42
43
44
const orderById = async (req, res, next, id) => {
    try {
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다')
        }
        req.userId = user
        next()
    } catch (error) {
        console.log(error);
        res.status(500).send('사용자 아이디 검색 실패')
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
45
46


47
export default { addorder, showorder, orderById }