category.controller.js 2.04 KB
Newer Older
kusang96's avatar
kusang96 committed
1
2
3
4
import Category from "../schemas/Category.js";

const getCategory = async (req, res) => {
    try {
박상호's avatar
213    
박상호 committed
5
        const category = await Category.find({}, { _id: 0 })
박상호's avatar
박상호 committed
6
        console.log("main= ", category);
kusang96's avatar
kusang96 committed
7
8
9
10
11
12
13
        res.json(category)
    } catch (error) {
        console.log(error)
        res.status(500).send('카테고리 검색 실패')
    }
}

박상호's avatar
박상호 committed
14
const getSubCategory = async (req, res) => {
박상호's avatar
clg    
박상호 committed
15
    console.log("req.params=", req.params);
박상호's avatar
박상호 committed
16
17
18
19
20
21
22
23
    const { sub } = req.params
    try {
        const subcategory = await Category.findOne({}, { _id: 0}).select(`${sub}`)
        console.log("sub= ",subcategory);
        res.json(subcategory);
    } catch (error) {
        res.status(500).send('카테고리를 불러오지 못했습니다.')
    }
이재연's avatar
0115    
이재연 committed
24
25
}

박상호's avatar
박상호 committed
26
27
28
29
30
31
32
33
34
35
const getToHome = async (res, req) => {
    try {
        const bestProduct = await Product.find({}).sort({ purchase: 1 }).limit(6)
        const newProduct = await Product.find({}).sort({ createdAt: -1 }).limit(6)
        console.log("best=", bestProduct)
        console.log("new=", newProduct)
        res.json(bestProduct, newProduct)
    } catch {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
이재연's avatar
0115    
이재연 committed
36
37
}

박상호's avatar
박상호 committed
38
39
40
41
42
43
44
45
46
47
48
49
50
const getsubId = async (req, res, next, ele) => {
    try {
        const sub = await Category.find({ele})
        if (!sub) {
            res.status(404).send('카테고리가 존재하지 않습니다.')
        } req.category = sub
        req.subcategory = sub
        next()
    }
    catch (error) {
        console.log(error);
        res.status(500).send('카테고리를 불러오지 못했습니다.')
    }
이재연's avatar
0115    
이재연 committed
51
52
53
    next()
}

박상호's avatar
박상호 committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// const userById = async (req, res, next, id) => {
//     try {
//         const user = await User.findById(id)
//         if (!user) {
//             res.status(404).send('사용자를 찾을 수 없습니다')
//         }
//         req.account = user
//         next()
//     } catch (error) {
//         console.log(error);
//         res.status(500).send('사용자 아이디 검색 실패')
//     }
// }


이재연's avatar
0115    
이재연 committed
69

박상호's avatar
박상호 committed
70
export default { getCategory, getsubId, getSubCategory, getToHome }