room.controller.js 6.28 KB
Newer Older
우지원's avatar
e    
우지원 committed
1
import { Room, User } from "../models/index.js";
Kim, Chaerin's avatar
Kim, Chaerin committed
2
3
4
import { customAlphabet } from "nanoid";
import isLength from "validator/lib/isLength.js";
import RoomModel from "../models/room.model.js";
이재연's avatar
aaaa    
이재연 committed
5
import multer from "multer";
Kim, Chaerin's avatar
Kim, Chaerin committed
6
const nanoid = customAlphabet("1234567890abcdef", 10);
우지원's avatar
0716    
우지원 committed
7

우지원's avatar
e    
우지원 committed
8
const joinRoom = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
9
  const { userId, roomId } = req.body;
우지원's avatar
e    
우지원 committed
10
  const room_Id = await Room.findOne({ where: { id: roomId } });
Kim, Chaerin's avatar
Kim, Chaerin committed
11
12
  if (Boolean(room_Id)) {
    //roomId에 일치하는 방이 존재할때
우지원's avatar
e    
우지원 committed
13
    //roomId에 일치하는 방의 member정보에 userId 저장하기
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
14
    //member정보에 userId가 이미 저장되어 있는지 확인 -> 이미 참여된 방인지 확인
이재연's avatar
이재연 committed
15
    const includeUserId = room_Id.member.includes(userId);
Kim, Chaerin's avatar
Kim, Chaerin committed
16
17
18
19
    if (!includeUserId) {
      //아직 참여되지 않은 방인경우
      room_Id.member.push(userId); //member에 userId추가
      await Room.update({ member: room_Id.member }, { where: { id: roomId } });
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
20
21
      //userId에 일치하는 사용자의 roomNumber에 roomId저장하기
      const user_Id = await User.findOne({ where: { id: userId } });
Kim, Chaerin's avatar
Kim, Chaerin committed
22
23
24
25
26
27
      if (Boolean(user_Id.roomNumber)) {
        //다른 roomNumber가 이미 들어가 있는 경우 roomId추가
        user_Id.roomNumber.push(roomId);
      } else {
        //첫 roomNumber인 경우
        user_Id.roomNumber = [roomId];
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
28
      }
Kim, Chaerin's avatar
Kim, Chaerin committed
29
30
31
32
      await User.update(
        { roomNumber: user_Id.roomNumber },
        { where: { id: userId } }
      );
33
      res.json(true);
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
34
    } else {
Kim, Chaerin's avatar
Kim, Chaerin committed
35
      return res.status(422).send("이미 참여된 방입니다.");
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
36
    }
우지원's avatar
e    
우지원 committed
37
  } else {
Kim, Chaerin's avatar
Kim, Chaerin committed
38
    return res.status(422).send("참여코드와 일치하는 방이 존재하지 않습니다.");
Kim, Chaerin's avatar
Kim, Chaerin committed
39
40
41
  }
};

Kim, Chaerin's avatar
Kim, Chaerin committed
42
const upLoadRoomImg = multer({ dest: "roomUploads/" });
43
44
45
const roomImgUpload = upLoadRoomImg.fields([
  { name: "profileimg", maxCount: 1 },
]);
Kim, Chaerin's avatar
Kim, Chaerin committed
46

권병윤's avatar
0807    
권병윤 committed
47
48
49
const update = async (req, res) => {
  try {
    console.log("id:", req.body.id);
50
51
    const id = req.body.id; //roomId
    const avatar = req.files["profileimg"][0]; //profileimg
권병윤's avatar
0807    
권병윤 committed
52
53
54
55
56
57
58
59
60
61
    const img = avatar.filename;
    console.log(img);
    await Room.update({ profileimg: img }, { where: { id: id } });
    res.json(img);
  } catch (error) {
    console.log(error);
    res.status(500).send("이미지 업데이트 실패");
  }
};

우지원's avatar
e    
우지원 committed
62
const createRoom = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
63
64
65
  const { userId, name } = req.body;
  const avatar = req.files["profileimg"][0];
  const img = avatar.filename;
66
  const id = nanoid();
우지원's avatar
e    
우지원 committed
67
  const Id = await Room.findOne({ where: { id: id } });
Kim, Chaerin's avatar
Kim, Chaerin committed
68
  while (Id) {
69
    const id = nanoid();
우지원's avatar
e    
우지원 committed
70
    const Id = await Room.findOne({ where: { id: id } });
우지원's avatar
0716    
우지원 committed
71
72
73
  }
  try {
    if (!isLength(name, { min: 3, max: 20 })) {
Kim, Chaerin's avatar
Kim, Chaerin committed
74
      return res.status(422).send("방이름은 3-20자여야 합니다.");
우지원's avatar
0716    
우지원 committed
75
    }
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
76
77
    //새로운 RoomDB생성
    const newRoom = {
우지원's avatar
e    
우지원 committed
78
79
      id: id,
      name: name,
Kim, Chaerin's avatar
Kim, Chaerin committed
80
81
82
      owner: userId,
      member: [userId],
      profileimg: img,
83
    };
Kim, Chaerin's avatar
Kim, Chaerin committed
84
    await Room.create(newRoom);
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
85
86

    //user.roomNumber에 id추가
Kim, Chaerin's avatar
Kim, Chaerin committed
87
    const user_Id = await User.findOne({ where: { id: userId } });
88
89
90
91
92
93
    if (user_Id.roomNumber) {
      //다른 roomNumber가 이미 들어가 있는 경우 id추가
      user_Id.roomNumber.push(id);
    } else {
      //첫 roomNumber인 경우
      user_Id.roomNumber = [id];
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
94
    }
95
96
97
98
99
    await User.update(
      { roomNumber: user_Id.roomNumber },
      { where: { id: userId } }
    );
    res.json(newRoom);
우지원's avatar
0716    
우지원 committed
100
  } catch (error) {
Kim, Chaerin's avatar
Kim, Chaerin committed
101
102
    console.log(error);
    res.status(500).send("방생성 에러");
우지원's avatar
0716    
우지원 committed
103
  }
Kim, Chaerin's avatar
Kim, Chaerin committed
104
};
우지원's avatar
0716    
우지원 committed
105

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
106
const getRoom = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
107
  try {
우지원's avatar
우지원 committed
108
    const roomlist = await Room.findAll({ where: { id: req.body } });
109
    res.json(roomlist);
Kim, Chaerin's avatar
Kim, Chaerin committed
110
111
112
113
  } catch (error) {
    res.status(500).send("에러");
  }
};
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
114

우지원's avatar
우지원 committed
115
const exitRoom = async (req, res) => {
116
  const { id, roomId } = req.params;
이재연's avatar
이재연 committed
117
118
119
120
121
122
123
124
  try {
    const room = await Room.findOne({ where: { id: roomId } });
    const index = room.member.indexOf(id);
    room.member.splice(index, 1);
    const newRoom = await Room.update(
      { member: room.member },
      { where: { id: roomId } }
    );
우지원's avatar
우지원 committed
125

이재연's avatar
이재연 committed
126
127
128
129
130
131
132
133
134
135
136
    const user = await User.findOne({ where: { id: id } });
    const index2 = user.roomNumber.indexOf(id);
    user.roomNumber.splice(index2, 1);
    const newUser = await User.update(
      { roomNumber: user.roomNumber },
      { where: { id: id } }
    );
    return res.json(room);
  } catch (error) {
    res.status(500).send("에러");
  }
137
};
우지원's avatar
우지원 committed
138

139
140
const changename = async (req, res) => {
  const { id, name } = req.body;
권병윤's avatar
0806    
권병윤 committed
141
  try {
142
143
144
    await Room.update({ name: name }, { where: { id: id } });
    const room1 = await Room.findOne({ where: { id: id } });
  } catch (error) {
권병윤's avatar
0806    
권병윤 committed
145
146
147
148
    res.status(500).send("에러");
  }
};

149
const joinChannel = async (req, res) => {
150
  const { roomId, channelName, plusUser, index } = req.body;
이재연's avatar
이재연 committed
151
152
153
154
  try {
    const room = await Room.findOne({ where: { id: roomId } });
    room.channel[index][channelName].push(plusUser);
    await Room.update({ channel: room.channel }, { where: { id: roomId } });
이재연's avatar
aa    
이재연 committed
155
    return res.json(true)
이재연's avatar
이재연 committed
156
157
158
  } catch (error) {
    res.status(500).send("error");
  }
159
};
160

우지원's avatar
우지원 committed
161
const doubleJoin = async (req, res) => {
162
  const { roomId, index1, index2, joinChName } = req.body;
이재연's avatar
이재연 committed
163
164
165
166
  try {
    const room = await Room.findOne({ where: { id: roomId } });
    room.channel[index1][joinChName].splice(index2, 1);
    await Room.update({ channel: room.channel }, { where: { id: roomId } });
이재연's avatar
aa    
이재연 committed
167
    return res.json(true)
이재연's avatar
이재연 committed
168
169
170
171
172
173
174
175
176
177
178
179
180
  } catch (error) {
    res.status(500).send("error");
  }
};

const removeRoom = async (req, res) => {
  const { roomId } = req.params;
  console.log('서버연결성공!!!!',roomId)
  try {
    const room = await Room.destroy({ where: { id: roomId } });
  } catch (error) {
    res.status(500).send("error");
  }
181
};
우지원's avatar
0805    
우지원 committed
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const makeChannel = async (req, res) => {
  const { id, channelName, number } = req.body;
  console.log(req.body);
  const room = await Room.findOne({ where: { id: id } });
  room.channel.push({[channelName] : []});
  await Room.update({channel:room.channel},{where: {id: id}});
  return res.json(true);
};

const channelDelete = async (req, res) => {
  const { id,  number } = req.body;
  const room = await Room.findOne({ where: { id: id } });
  room.channel.splice(number, 1 );
  await Room.update({channel:room.channel},{where: {id: id}});
  return res.json(true);
};
우지원's avatar
우지원 committed
199

Kim, Chaerin's avatar
Kim, Chaerin committed
200
export default {
201
202
203
204
205
206
207
208
  joinRoom,
  roomImgUpload,
  createRoom,
  getRoom,
  exitRoom,
  changename,
  joinChannel,
  doubleJoin,
209
210
  update,
  makeChannel,
211
  channelDelete,
이재연's avatar
이재연 committed
212
  removeRoom,
Kim, Chaerin's avatar
Kim, Chaerin committed
213
};