order.controller.js 2.2 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

const addorder = async (req, res) => {
    const { userId, products, receiverInfo, total } = req.body
    try {
kusang96's avatar
kusang96 committed
7
        const newOrder = await new Order({ userId, products, receiverInfo, total }).save()
Jiwon Yoon's avatar
Jiwon Yoon committed
8
9
10
11
12
13
14
        res.status(200).send('Order DB에 저장 완료')
    } catch (error) {
        console.log(error)
        res.status(500).send('Order DB에 저장 실패')
    }
}

15
16
17
const Ordered = async (req, res) => {
    const { db } = req.body
    try {
kusang96's avatar
kusang96 committed
18
        const ordered = await req.body.findOne({}, { _id: 0 }).select(`${db}`)
19
20
21
22
23
24
        res.json(ordered);
    } catch (error) {
        res.status(500).send('카테고리를 불러오지 못했습니다.')
    }
}

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

kusang96's avatar
kusang96 committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const recommendPro = async (req, res) => {
    const { productId } = req.body
    console.log(productId)
    try {
        const findedorder = await Order.find({ 'products.productId': productId })
        console.log('findedouder=', findedorder)
        const recommend = await         
        const recommend = await Order.aggregate([
            // { $project: {}},
            {$match: {"products":{"productId": productId}}},
            {$group: {
                    _id: "$products.productId",
                    num_tutorial: { $sum: 1 }
                }
            }
        ])
        console.log('recommend=', recommend)
    } catch (error) {

    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
59

60
61
62
63
64
65
66
67
68
69
70
71
72
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
73

kusang96's avatar
kusang96 committed
74
export default { addorder, showorder, orderById, Ordered }