room.controller.js 5.6 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
import isLength from 'validator/lib/isLength.js'
Choi Ga Young's avatar
x    
Choi Ga Young committed
4
//import AccessInfo from '../models/AccessInfo.js'
5
import Chat from "../models/Chat.js"
Soo Hyun Kim's avatar
Soo Hyun Kim committed
6

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
9
10
const makeRoom = async (req, res) => {
    console.log(req.body)
Choi Ga Young's avatar
Choi Ga Young committed
11
    const { roomName, interest, isOpen, member } = req.body;
Choi Ga Young's avatar
Choi Ga Young committed
12
    console.log('콘솔확인', roomName, interest, isOpen, member)
우지원's avatar
우지원 committed
13
14
15
16
17
18
    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
19

Soo Hyun Kim's avatar
Soo Hyun Kim committed
20
    try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
21
        if (!isLength(roomName, { min: 3, max: 20 })) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
22
            return res.status(422).send('채팅방의 이름은 3-20자여야 합니다.')
Choi Ga Young's avatar
Choi Ga Young committed
23
        } else if (interest == 'Choose...' || interest == '') {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
            return res.status(422).send('분야를 반드시 선택하여야 합니다.')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
25
        }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
26
        const newRoom = await new Room({
우지원's avatar
e    
우지원 committed
27
            roomId,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
            roomName,
Soo Hyun Kim's avatar
Soo Hyun Kim committed
29
            interest,
우지원's avatar
수정    
우지원 committed
30
            isOpen,
우지원'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 {
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
43
        let list = await Room.find({ member: req.query._id })
Choi Ga Young's avatar
x    
Choi Ga Young committed
44
        // console.log('c_list가져오기', list)
우지원's avatar
e    
우지원 committed
45
46
47
48
49
50
51
52
53
        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
x    
Choi Ga Young committed
54
        // console.log('o_list가져오기', list)
Choi Ga Young's avatar
Choi Ga Young committed
55
56
57
58
59
        return res.json(list)
    } catch (error) {
        res.status(500).send('리스트 불러오기를 실패하였습니다!')
    }
}
우지원's avatar
수정    
우지원 committed
60

Soo Hyun Kim's avatar
Soo Hyun Kim committed
61
62
const getRoomName = async (req, res) => {
    const roomId = req.query.roomCode
63
    console.log('getRoomName', req.query.roomCode)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
64
65
66

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

Choi Ga Young's avatar
x    
Choi Ga Young committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// const sysMsg = async (req, res) => {
//     try {
//         console.log('sysreq', req.query)
//         let rmif = await Room.find({ roomId: req.query.roomCode })
//         console.log('rmif', rmif)
//         let rmid = await AccessInfo.find({ room: rmif._id })
//         console.log('rmid', rmid)

//         if (rmid.isEnt) {
//             let msg = `${rmif.nickname}이 들어왔습니다`
//         } else {

//         }

//     } catch (error) {
//         res.status(500).send('')
//     }
// }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
93
94
95
96
97
98
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 {
Choi Ga Young's avatar
x    
Choi Ga Young committed
99
        if (isPresent < 0) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
100
101
102
            const memberId = room.member.push(userId)
            await Room.updateOne({ 'roomId': roomId }, { 'member': room.member })
            console.log('room.member 업데이트 완료')
우지원's avatar
우지원 committed
103
104
105
106
            return res.json(true)
        }
        else {
            return res.json(false)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
107
108
109
110
111
112
113
        }
        res.end()
    } catch (error) {
        res.status(500).send('멤버 업데이트 실패')
    }
}

우지원's avatar
우지원 committed
114
115
116
117
118
119
120
121
122
123
124
125
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('올바르지 못한 접근입니다.')
    }
}

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// const unreadMessage = async (req, res) => {
//     let leaveInfo = req.query.leaveInfo
//     // leaveInfo.shift()
//     const roomId = req.query.roomId
//     let leaveTime = ''
//     for (let i = 1; i < leaveInfo.length - 1; i++) {    //일단 형식좀 맞추고
//         leaveInfo[i] = JSON.parse(leaveInfo[i])
//     }


//     for (let i = 1; i <= leaveInfo.length - 1; i++) {   //그 방의 id와 나간기록의 방과 일치하는지 확인하고
//         if (leaveInfo[i].roomName === roomId) {
//             leaveTime = leaveInfo[i].leaveTime  //그 방에서 나간 시간을 찾아옴
//             console.log('서버의 포문', i, leaveTime)
//             break;
//         }
//     }
//     // console.log('서버에서 unreadMessage', leaveInfo, roomId)
//     const room_id = await Room.find({ roomId: roomId }).select('_id')   //id로 _id를 찾아와서
//     let unreadMsg = await Chat.find({ room: room_id }).select('message createdAt')  //그 방의 메세지와 전송시간을 가져옴
//     // console.log('서버에서 두번재 언리드', unreadMsg)
//     console.log('서버에서 언리드', leaveTime)
//     let count = 0
//     for (let i = 0; i <= unreadMsg.length - 1; i++) {
//         const dbtime = Date.parse(unreadMsg[i].createdAt)
//         const parsedleaveTime = Date.parse(leaveTime)
//         if (parsedleaveTime > dbtime) {
//             console.log('이번째부터 나중에온 메세지', i)
//             count += 1
//         }
//     }
//     console.log('카운트 세버', count)
//     return count
// }

// const dbChat = async (req, res) => {
//     const chatlist = await Chat
//     console.log(chatlist)

// }

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