user.controller.js 2.64 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
6
7
8
import multer from "multer";

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

Jiwon Yoon's avatar
Jiwon Yoon committed
9
10
11
12
13
14
15
16
17
const getUser = async (req, res) => {
    try {
        const user = await User.findOne({ _id: req.id })
        res.json(user)
    } catch (error) {
        res.status(500).send('사용자 정보를 불러올 수 없습니다.')
    }
}

18
const imgUpload = uploadimg.fields([
박상호's avatar
박상호 committed
19
    { name: 'avatar', maxCount: 1 }
20
21
22
23
24
25
26
])

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

박상호's avatar
박상호 committed
27
const userById = async (req, res, next, id) => {
28
29
30
31
32
33
    try {
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다')
        }
        req.account = user
Jiwon Yoon's avatar
Jiwon Yoon committed
34
        req.id = id
박상호's avatar
박상호 committed
35
        next()
36
37
38
39
40
41
    } catch (error) {
        console.log(error);
        res.status(500).send('사용자 아이디 검색 실패')
    }
}

Kim, Subin's avatar
Kim, Subin committed
42
43

const signup = async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
44
45
46

    const { name, number1, number2, id, password, tel } = req.body

이재연's avatar
zz    
이재연 committed
47
    console.log(req.body)
Kim, Subin's avatar
Kim, Subin committed
48
    try {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
49
        if (!isLength(password, { min: 8, max: 15 })) {
이재연's avatar
aa    
이재연 committed
50
            return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.')
이재연's avatar
이재연 committed
51
        }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
52
53
        const user = await User.findOne({ id })
        if (user) {
이재연's avatar
aa    
이재연 committed
54
55
56
            return res.status(422).send(`${id}가 이미 사용중입니다.`)
        }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
57
58
59
        const hash = await bcrypt.hash(password, 10)

        const newUser = await new User({
Kim, Subin's avatar
Kim, Subin committed
60
            name,
이재연's avatar
이재연 committed
61
62
            number1,
            number2,
이재연's avatar
이재연 committed
63
            id,
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
64
            password: hash,
이재연's avatar
이재연 committed
65
            tel,
Kim, Subin's avatar
Kim, Subin committed
66
        }).save()
Jiwon Yoon's avatar
Jiwon Yoon committed
67
        await new Cart({ userId: newUser._id, role }).save()
Kim, Subin's avatar
Kim, Subin committed
68
69
        console.log(newUser)
        res.json(newUser)
kusang96's avatar
kusang96 committed
70

Kim, Subin's avatar
Kim, Subin committed
71
72
    } catch (error) {
        console.log(error)
이재연's avatar
aa    
이재연 committed
73
        res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
Kim, Subin's avatar
Kim, Subin committed
74
75
76
    }
}

77
const update = async (req, res) => {
박상호's avatar
박상호 committed
78
    console.log("req", req.body)
79
    try {
박상호's avatar
박상호 committed
80
81
82
83
84
85
86
87
88
89
90
91
        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)
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
92

93
94
95
96
97
98
    } catch (error) {
        console.log(error);
        res.status(500).send('이미지 업데이트 실패')
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
99
export default { getUser, signup, username, imgUpload, userById, update }