user.controller.js 3.11 KB
Newer Older
1
import Cart from "../schemas/Cart.js";
kusang96's avatar
dd    
kusang96 committed
2
import User from "../schemas/User.js";
kusang96's avatar
kusang96 committed
3
4
import isLength from 'validator/lib/isLength.js';
import bcrypt from 'bcryptjs';
5
import multer from "multer";
이재연's avatar
이재연 committed
6
import Order from "../schemas/Order.js";
7
8
9
10

const uploadimg = multer({ dest: 'uploads/' });

const imgUpload = uploadimg.fields([
박상호's avatar
박상호 committed
11
    { name: 'avatar', maxCount: 1 }
12
13
14
15
16
17
])

const username = (req, res) => {
    res.json(req.account)
}

박상호's avatar
박상호 committed
18
const userById = async (req, res, next, id) => {
19
20
21
22
23
24
    try {
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다')
        }
        req.account = user
박상호's avatar
박상호 committed
25
        next()
26
27
28
29
30
31
    } catch (error) {
        console.log(error);
        res.status(500).send('사용자 아이디 검색 실패')
    }
}

Kim, Subin's avatar
Kim, Subin committed
32
const signup = async (req, res) => {
박상호's avatar
0120    
박상호 committed
33
    const { name, number1, number2, id, password, tel, email } = req.body
Kim, Subin's avatar
Kim, Subin committed
34
    try {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
35
36
        const user = await User.findOne({ id })
        if (user) {
이재연's avatar
aa    
이재연 committed
37
            return res.status(422).send(`${id}가 이미 사용중입니다.`)
Kim, Subin's avatar
Kim, Subin committed
38
39
40
41
42
43
        } else if (!isLength(password, { min: 8, max: 15 })) {
            return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.')
        } else if (number1.match('[^0-9]') || number2.match('[^0-9]')) {
            return res.status(422).send('주민등록번호는 숫자로 입력해주세요.')
        } else if (!/^[0-9]{2,3}-[0-9]{3,4}-[0-9]{4}/.test(tel)) {
            return res.status(422).send('유효한 휴대전화번호가 아닙니다. 정확히 입력해주세요.')
이재연's avatar
aa    
이재연 committed
44
        }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
45
46
        const hash = await bcrypt.hash(password, 10)
        const newUser = await new User({
Kim, Subin's avatar
Kim, Subin committed
47
            name,
이재연's avatar
이재연 committed
48
49
            number1,
            number2,
이재연's avatar
이재연 committed
50
            id,
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
51
            password: hash,
이재연's avatar
이재연 committed
52
            tel,
박상호's avatar
0120    
박상호 committed
53
            email
Kim, Subin's avatar
Kim, Subin committed
54
        }).save()
박상호's avatar
0120    
박상호 committed
55
        await new Cart({ userId: newUser._id }).save()
Kim, Subin's avatar
Kim, Subin committed
56
        res.status(200).json('회원가입 성공')
Kim, Subin's avatar
Kim, Subin committed
57
58
    } catch (error) {
        console.log(error)
이재연's avatar
aa    
이재연 committed
59
        res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
Kim, Subin's avatar
Kim, Subin committed
60
61
62
    }
}

63
64
const update = async (req, res) => {
    try {
박상호's avatar
박상호 committed
65
66
67
68
69
70
71
72
73
74
75
76
        if (req.body.avatar == '') {
            const user = req.account
            user.avatarUrl = req.body.avatar
            const updateUser = await user.save()
            res.json(updateUser)
        } else {
            const avatar = req.files['avatar'][0]
            const user = req.account
            user.avatarUrl = avatar.filename
            const updateUser = await user.save()
            res.json(updateUser)
        }
77
78
79
80
81
82
    } catch (error) {
        console.log(error);
        res.status(500).send('이미지 업데이트 실패')
    }
}

이재연's avatar
이재연 committed
83
const addorder = async (req, res) => {
kusang96's avatar
kusang96 committed
84
    const { userId } = req.body
이재연's avatar
이재연 committed
85
86
87
88
    try {
        const order = await Order.find({ userId: userId }).populate({
            path: 'products.productId',
            model: 'Product'
Kim, Subin's avatar
정리    
Kim, Subin committed
89
        }).sort({ createdAt: -1 })
이재연's avatar
이재연 committed
90
91
92
93
94
95
96
        res.status(200).json(order)
    } catch (error) {
        console.log(error)
        res.status(500).send('주문현황을 불러오지 못했습니다.')
    }
}

kusang96's avatar
kusang96 committed
97
export default { signup, username, imgUpload, userById, update, addorder }