room.controller.js 1.9 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import Room from "../models/Room.js"
우지원's avatar
우지원 committed
2
import { customAlphabet } from 'nanoid'
Soo Hyun Kim's avatar
Soo Hyun Kim committed
3
4
import isLength from 'validator/lib/isLength.js'

우지원's avatar
우지원 committed
5
const nanoid = customAlphabet('1234567890abcdef', 10)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
6

Soo Hyun Kim's avatar
Soo Hyun Kim committed
7
8
const makeRoom = async (req, res) => {
    console.log(req.body)
우지원's avatar
e    
우지원 committed
9
10
    const { roomName, interest, isOpen, moderator, member } = req.body;
    console.log(roomName, interest, isOpen, moderator, member)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
11

우지원's avatar
우지원 committed
12
13
14
15
16
17
    const roomId = nanoid()
    const room = await Room.findOne({ roomId })
    while (room) {
        roomId = nanoid()
        room = await Room.findOne({ roomId })
    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
18

Soo Hyun Kim's avatar
Soo Hyun Kim committed
19
    try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
20
        if (!isLength(roomName, { min: 3, max: 20 })) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
21
            return res.status(422).send('채팅방의 이름은 3-20자여야 합니다.')
Choi Ga Young's avatar
Choi Ga Young committed
22
        } else if (interest == 'Choose...' || interest == '') {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
23
            return res.status(422).send('분야를 반드시 선택하여야 합니다.')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
        }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
25
        const newRoom = await new Room({
우지원's avatar
e    
우지원 committed
26
            roomId,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
27
            roomName,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
            interest,
우지원's avatar
수정    
우지원 committed
29
30
            isOpen,
            moderator,
우지원's avatar
e    
우지원 committed
31
            member,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
32
        }).save()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
33
34
        console.log(newRoom)
        res.json(newRoom)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
35
36
    } catch (error) {
        console.log(error)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
        res.status(500).send('방생성 에러')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
38
39
40
    }
}

Choi Ga Young's avatar
Choi Ga Young committed
41
42
const getClosedList = async (req, res) => {
    try {
43
44
45
46
        console.log('req확인',req.query._id)
        let list = await Room.find({ moderator
            : req.query._id })
        console.log('c_list가져오기', list)
우지원's avatar
e    
우지원 committed
47
48
49
50
51
52
        return res.json(list)
    } catch (error) {
        res.status(500).send('리스트 불러오기를 실패하였습니다!')
    }
}

53

우지원's avatar
e    
우지원 committed
54
55
56
const getOpenList = async (req, res) => {
    try {
        let list = await Room.find({ isOpen: true })
57
        console.log('o_list가져오기', list)
Choi Ga Young's avatar
Choi Ga Young committed
58
59
60
61
62
        return res.json(list)
    } catch (error) {
        res.status(500).send('리스트 불러오기를 실패하였습니다!')
    }
}
우지원's avatar
수정    
우지원 committed
63

우지원's avatar
e    
우지원 committed
64
export default { makeRoom, getClosedList, getOpenList }