room.controller.js 3.82 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;
Choi Ga Young's avatar
Choi Ga Young committed
10
    console.log('콘솔확인', roomName, interest, isOpen, member)
우지원's avatar
우지원 committed
11
12
13
14
15
16
    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
17

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

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

const getOpenList = async (req, res) => {
    try {
        let list = await Room.find({ isOpen: true })
Choi Ga Young's avatar
Choi Ga Young committed
51
52
53
54
55
        return res.json(list)
    } catch (error) {
        res.status(500).send('리스트 불러오기를 실패하였습니다!')
    }
}
우지원's avatar
수정    
우지원 committed
56

Soo Hyun Kim's avatar
Soo Hyun Kim committed
57
58
const getRoomName = async (req, res) => {
    const roomId = req.query.roomCode
Soo Hyun Kim's avatar
Soo Hyun Kim committed
59
    console.log(req.query.roomCode)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
60
61
62

    try {
        let roominfo = await Room.findOne({ roomId: roomId }).select('roomName')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
63
        console.log(roominfo.roomName)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
64
65
66
67
68
69
        return res.json(roominfo.roomName)
    } catch (error) {
        res.status(500).send('리스트 불러오기를 실패하였습니다!')
    }
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
70
71
72
73
74
75
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 {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
76
        if (isPresent < 0) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
77
78
79
            const memberId = room.member.push(userId)
            await Room.updateOne({ 'roomId': roomId }, { 'member': room.member })
            console.log('room.member 업데이트 완료')
우지원's avatar
우지원 committed
80
81
82
83
            return res.json(true)
        }
        else {
            return res.json(false)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
84
85
86
87
88
89
90
        }
        res.end()
    } catch (error) {
        res.status(500).send('멤버 업데이트 실패')
    }
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const deleteUserId = async (req, res) => {
    console.log(req.body)
    const { userId, roomId } = req.body
    let room = await Room.findOne({ roomId: roomId }).select('member')
    console.log('deletetest', room)
    const memIndex = room.member.indexOf(userId)
    try {
        room.member.splice(memIndex, 1)
        await Room.updateOne({ 'roomId': roomId }, { 'member': room.member })
        console.log(`${roomId}${userId}삭제완료`)
        return res.json(true)
    } catch (error) {
        res.status(500).send('멤버 업데이트 실패')
    }
}

우지원's avatar
우지원 committed
107
108
109
110
111
112
113
114
115
116
117
118
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('올바르지 못한 접근입니다.')
    }
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
119
export default { makeRoom, getClosedList, getOpenList, getRoomName, changemember, deleteUserId, roomInf }