order.controller.js 2.73 KB
Newer Older
kusang96's avatar
card    
kusang96 committed
1
import mongoose from 'mongoose';
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import Order from "../schemas/Order.js";
3
import User from "../schemas/User.js";
Jiwon Yoon's avatar
Jiwon Yoon committed
4
import Product from "../schemas/Product.js";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
6
7
8

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

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

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

40
41
42
43
44
45
46
47
48
49
50
51
52
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
53

kusang96's avatar
card    
kusang96 committed
54
const recommendPro = async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
55
    const productId = req.query.products
이재연's avatar
aaaa    
이재연 committed
56
57
    try {
        const recommend = await Order.aggregate([
kusang96's avatar
card    
kusang96 committed
58
59
            {
                $match: {
Jiwon Yoon's avatar
Jiwon Yoon committed
60
                    'products.productId': mongoose.Types.ObjectId(productId)
kusang96's avatar
card    
kusang96 committed
61
62
                }
            },
Jiwon Yoon's avatar
Jiwon Yoon committed
63
            { $unwind: "$products" },
이재연's avatar
aaaa    
이재연 committed
64
            {
kusang96's avatar
card    
kusang96 committed
65
                $group: {
Jiwon Yoon's avatar
Jiwon Yoon committed
66
67
                    _id: "$products.productId",
                    count: { $sum: 1 }
이재연's avatar
aaaa    
이재연 committed
68
                }
69
70
71
72
73
74
75
76
77
78
79
            },
            { $sort: { count: -1 } },
            { $limit: 5 },
            {
                $lookup:
                {
                    from: "products",
                    localField: "_id",
                    foreignField: "_id",
                    as: "product"
                }
이재연's avatar
aaaa    
이재연 committed
80
81
            }
        ])
kusang96's avatar
card    
kusang96 committed
82
        console.log('recommend=', recommend)
Jiwon Yoon's avatar
Jiwon Yoon committed
83
84
        const filteredRecommend = recommend.filter((el) => String(el._id) !== String(productId))
        console.log('filtering=', filteredRecommend)
85
        res.json(filteredRecommend)
이재연's avatar
aaaa    
이재연 committed
86
    } catch (error) {
kusang96's avatar
card    
kusang96 committed
87
        console.log('error in order ', error)
이재연's avatar
aaaa    
이재연 committed
88
89
90
    }
}

kusang96's avatar
card    
kusang96 committed
91
export default { addorder, showorder, orderById, Ordered, recommendPro }