roomEnter.controller.js 1.17 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
    const roomId = req.body.enterCode
Soo Hyun Kim's avatar
Soo Hyun Kim committed
7
8
9
10
11
12

    try {
        const room = await Room.findOne({ roomId })
        if (!room) {
            return res.status(404).send(`참여코드가 존재하지 않습니다.`)
        }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
13
14
15
16
        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
17
18
19
    } catch (error) {
        //알수없는 모든 에러발생시 처리
        console.log(error)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
20
        res.status(500).send('방 참여 에러')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
21
22
23
    }
}

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



37
export default { roomEnter, getChatInfo }