roomEnter.controller.js 1.72 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import Room from "../models/Room.js"
2
3
import Chat from "../models/Chat.js";
import User from "../models/User.js";
Soo Hyun Kim's avatar
Soo Hyun Kim committed
4
5

const roomEnter = async (req, res) => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
6
7
    const roomId = req.body.enterCode
    // console.log(req)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
8
9
10
11
12
13

    try {
        const room = await Room.findOne({ roomId })
        if (!room) {
            return res.status(404).send(`참여코드가 존재하지 않습니다.`)
        }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
14
15
16
17
18
19

        console.log('roomId존재')

        const name = await Room.findOne({ roomId: roomId }).select('roomName')
        return res.json(name.roomName)

Soo Hyun Kim's avatar
Soo Hyun Kim committed
20
21
22
    } catch (error) {
        //알수없는 모든 에러발생시 처리
        console.log(error)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
23
        res.status(500).send('방 참여 에러')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
25
26
    }
}

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const getChatInfo = async (req, res) => {
    const roomId = req.query.roomCode
    try {
        const room_id = await Room.find({ roomId: roomId }).select('_id')  //roomId로 해당 방의 _id를 찾아옴
        let infolist = await Chat.find({ room: room_id }).select('message username createdAt')
        res.json(infolist)
    } catch (error) {
        res.status(500).send('이전 채팅 정보 불러오기를 실패하였습니다!')
    }
}

const getProfileImage = async (req, res) => {
    const nicknamelist = req.query.userlist
    try {
        let imglist = []
JeongYeonwoo's avatar
JeongYeonwoo committed
42
43
44
        for (let prop in nicknamelist) {
            const getimg = await User.find({ nickname: nicknamelist[prop] }).select('profileimg')
            const eachimg = { profileimg: getimg[0].profileimg }
45
46
47
48
49
50
51
52
53
54
            imglist = [...imglist, eachimg]
        }
        res.json(imglist)

    } catch (error) {
        res.status(500).send('프로필 사진 정보 불러오기를 실패하였습니다!')
    }
}

export default { roomEnter, getChatInfo, getProfileImage }