category.controller.js 1.62 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 })
kusang96's avatar
kusang96 committed
6
7
8
9
10
11
12
        res.json(category)
    } catch (error) {
        console.log(error)
        res.status(500).send('카테고리 검색 실패')
    }
}

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

박상호's avatar
박상호 committed
25
26
27
28
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)
29
30
        // console.log("best=", bestProduct)
        // console.log("new=", newProduct)
박상호's avatar
박상호 committed
31
32
33
34
        res.json(bestProduct, newProduct)
    } catch {
        res.status(500).send('상품을 불러오지 못했습니다.')
    }
이재연's avatar
0115    
이재연 committed
35
36
}

박상호's avatar
박상호 committed
37
38
39
40
41
42
43
44
45
46
47
48
49
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
50
51
52
    next()
}

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