user.controller.js 2.46 KB
Newer Older
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
1
2
3
4
5
6
import User from "../models/User.js"
import isLength from 'validator/lib/isLength.js'
import isEmail from "validator/lib/isEmail.js"
import bcrypt from "bcryptjs";

const signup = async (req, res) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
7
    const { name, email, password } = req.body //구조분해해서 하나씩
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
8
    console.log(name, email, password)
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
9
    try {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
10
        if (!isLength(name, { min: 3, max: 10 })) {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
11
            return res.status(422).send('이름은 3-10자 사이입니다')
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
12
        } else if (!isLength(password, { min: 6 })) {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
13
            return res.status(422).send('비밀번호는 6자 이상입니다')
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
14
        } else if (!isEmail(email)) {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
15
16
            return res.status(422).send('유효하지 않은 이메일 형식입니다')
        }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
17
        const user = await User.findOne({ email })
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
18
19
20
21
        if (user) {
            return res.status(422).send(`${email}이 이미 사용중입니다`)
        }
        const hash = await bcrypt.hash(password, 10)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
22
        const newUser = await new User({
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
23
24
25
            name,
            email,
            password: hash
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
26
        }).save() //save하면 몽고 db에 들어간다 save method가 promise
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
27
        console.log(newUser)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
28
29
        res.json(newUser) //json형식으로 바꿔서 문자열로 보낸다 client쪽으로
    } catch (error) { //다른 과정에서 에러가 나면 실행 
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
30
31
32
33
        console.log(error)
        res.status(500).send('회원가입 에러')
    }
}
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
34

Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
35
const userById = async (req, res, next, id) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
36
    try {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
37
38
39
40
41
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다')
        }
        req.profile = user
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
42
        next()
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
43
44
45
46
47
48
    } catch (error) {
        console.log(error)
        res.status(500).send('사용자 아이디 검색 실패')
    }
}

Lee SeoYeon's avatar
..    
Lee SeoYeon committed
49
const getBookmark = async (req, res) => {
Lee SeoYeon's avatar
0127    
Lee SeoYeon committed
50
51
52
53
    await User.findOne({ _id: req.query.ID }).populate('bookmark').exec((err, bookmark) => {
        console.log(bookmark, "dkssud")
        res.send(bookmark)
    })
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
54
55
}

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
56
57
58
59
60
61
62
63
64
65
66
67
68
const setBookmark = async (req, res) => {
    console.log(req.query.ID, req.query.place, "여기에요 여기!!!!")
    await User.updateOne({ _id: req.query.ID }, { $push: { bookmark: req.query.place } })
    res.send("추가완료")
}

const deleteBookmark = async (req, res) => {
    console.log(req.query.ID, req.query.place)
    await User.updateOne({ _id: req.query.ID }, { $pull: { bookmark: req.query.place } })
    res.send("삭제완료")
}

export default { signup, userById, getBookmark, setBookmark, deleteBookmark }