room.controller.js 3.35 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)
Choi Ga Young's avatar
Choi Ga Young committed
9
    const { roomName, interest, isOpen, member } = req.body;
우지원's avatar
우지원 committed
10
11
    console.log('콘솔확인', roomName, interest, isOpen, member)
    //console.log('member저장', member)
우지원'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
            isOpen,
우지원's avatar
e    
우지원 committed
30
            member,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
31
        }).save()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
32
33
        console.log(newRoom)
        res.json(newRoom)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
35
    } catch (error) {
        console.log(error)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
36
        res.status(500).send('방생성 에러')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
38
39
    }
}

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

우지원's avatar
우지원 committed
51

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
62
63
64
65
66
67
68
69
70
71
72
const getRoomName = async (req, res) => {
    const roomId = req.query.roomCode

    try {
        let roominfo = await Room.findOne({ roomId: roomId }).select('roomName')
        return res.json(roominfo.roomName)
    } catch (error) {
        res.status(500).send('리스트 불러오기를 실패하였습니다!')
    }
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
73
74
75
76
77
78
79
80
81
82
const changemember = async (req, res) => {
    const { userId, roomId } = req.body
    console.log(roomId)
    let room = await Room.findOne({ roomId: roomId }).select('member')
    const isPresent = room.member.indexOf(userId)
    try {
        if (isPresent<0) {
            const memberId = room.member.push(userId)
            await Room.updateOne({ 'roomId': roomId }, { 'member': room.member })
            console.log('room.member 업데이트 완료')
우지원's avatar
우지원 committed
83
84
85
86
            return res.json(true)
        }
        else {
            return res.json(false)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
87
88
89
90
91
92
93
        }
        res.end()
    } catch (error) {
        res.status(500).send('멤버 업데이트 실패')
    }
}

우지원's avatar
우지원 committed
94
95
96
97
98
99
100
101
102
103
104
105
106
const roomInf = async (req, res) => {
    try {
        console.log(req.query.roomId)
        // let roomInf = await Room.findOne({ member: req.query.roomId }).select('interest roomId member').exec()
        let roomInf = await Room.find({ roomId: req.query.roomId })
        console.log('room_member로 정보 가져오기:', roomInf)
        return res.json(roomInf)
    } catch (error) {
        alert('올바르지 못한 접근입니다.')
    }
}

export default { makeRoom, getClosedList, getOpenList, changemember, roomInf }