room.controller.js 5.32 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
15
    //member정보에 userId가 이미 저장되어 있는지 확인 -> 이미 참여된 방인지 확인
    const includeUserId = room_Id.member.includes(parseInt(userId));
Kim, Chaerin's avatar
Kim, Chaerin committed
16
17
18
19
    // console.log('Include확인:',includeUserId)
    if (!includeUserId) {
      //아직 참여되지 않은 방인경우
      room_Id.member.push(userId); //member에 userId추가
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
20
      // console.log('room_Id.member2:', room_Id.member)
Kim, Chaerin's avatar
Kim, Chaerin committed
21
      await Room.update({ member: room_Id.member }, { where: { id: roomId } });
우지원's avatar
e    
우지원 committed
22

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
23
24
      //userId에 일치하는 사용자의 roomNumber에 roomId저장하기
      const user_Id = await User.findOne({ where: { id: userId } });
Kim, Chaerin's avatar
Kim, Chaerin committed
25
26
27
28
29
30
      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
31
32
      }
      // console.log('user_Id.roomNumber2:', user_Id.roomNumber)
Kim, Chaerin's avatar
Kim, Chaerin committed
33
34
35
36
37
      await User.update(
        { roomNumber: user_Id.roomNumber },
        { where: { id: userId } }
      );
      res.json(true)
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
38
    } else {
Kim, Chaerin's avatar
Kim, Chaerin committed
39
      return res.status(422).send("이미 참여된 방입니다.");
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
40
    }
우지원's avatar
e    
우지원 committed
41
  } else {
Kim, Chaerin's avatar
Kim, Chaerin committed
42
    return res.status(422).send("참여코드와 일치하는 방이 존재하지 않습니다.");
Kim, Chaerin's avatar
Kim, Chaerin committed
43
44
45
  }
};

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

우지원's avatar
e    
우지원 committed
49
const createRoom = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
50
51
52
  const { userId, name } = req.body;
  const avatar = req.files["profileimg"][0];
  const img = avatar.filename;
우지원's avatar
0716    
우지원 committed
53
  const id = nanoid()
우지원's avatar
e    
우지원 committed
54
  const Id = await Room.findOne({ where: { id: id } });
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
55
  // console.log('id:', id)
Kim, Chaerin's avatar
Kim, Chaerin committed
56
  while (Id) {
우지원's avatar
e    
우지원 committed
57
58
    const id = nanoid()
    const Id = await Room.findOne({ where: { id: id } });
우지원's avatar
0716    
우지원 committed
59
60
61
  }
  try {
    if (!isLength(name, { min: 3, max: 20 })) {
Kim, Chaerin's avatar
Kim, Chaerin committed
62
      return res.status(422).send("방이름은 3-20자여야 합니다.");
우지원's avatar
0716    
우지원 committed
63
    }
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
64
65
    //새로운 RoomDB생성
    const newRoom = {
우지원's avatar
e    
우지원 committed
66
67
      id: id,
      name: name,
Kim, Chaerin's avatar
Kim, Chaerin committed
68
69
70
      owner: userId,
      member: [userId],
      profileimg: img,
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
71
72
    }
    // console.log('newRoom:', newRoom)
Kim, Chaerin's avatar
Kim, Chaerin committed
73
    await Room.create(newRoom);
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
74
75

    //user.roomNumber에 id추가
Kim, Chaerin's avatar
Kim, Chaerin committed
76
    const user_Id = await User.findOne({ where: { id: userId } });
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
77
78
79
80
81
82
83
    if (user_Id.roomNumber) { //다른 roomNumber가 이미 들어가 있는 경우 id추가
      user_Id.roomNumber.push(id)
    }
    else { //첫 roomNumber인 경우 
      user_Id.roomNumber = [id]
    }
    // console.log('user_Id.roomNumber2:', user_Id.roomNumber)
Kim, Chaerin's avatar
Kim, Chaerin committed
84
    await User.update({ 'roomNumber': user_Id.roomNumber }, { where: { id: userId } })
우지원's avatar
0716    
우지원 committed
85
86
    res.json(newRoom)
  } catch (error) {
Kim, Chaerin's avatar
Kim, Chaerin committed
87
88
    console.log(error);
    res.status(500).send("방생성 에러");
우지원's avatar
0716    
우지원 committed
89
  }
Kim, Chaerin's avatar
Kim, Chaerin committed
90
};
우지원's avatar
0716    
우지원 committed
91

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
92
const getRoom = async (req, res) => {
우지원's avatar
우지원 committed
93
  // console.log("fhgfghdgfdgf:", req.body);
Kim, Chaerin's avatar
Kim, Chaerin committed
94
  try {
우지원's avatar
우지원 committed
95
96
    const roomlist = await Room.findAll({ where: { id: req.body } });
    // console.log(roomlist);
Kim, Chaerin's avatar
Kim, Chaerin committed
97
98
99
100
101
102
    res.json(roomlist)
  } catch (error) {
    console.log(error);
    res.status(500).send("에러");
  }
};
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
103

우지원's avatar
우지원 committed
104
105
106
107
108
109
const exitRoom = async (req, res) => {
  const { id, roomId } = req.params
  console.log(id, roomId)
  const room = await Room.findOne({ where: { id: roomId } });
  console.log(room.member)
  const index = room.member.indexOf(id)
110
111
  console.log('index', index)
  room.member.splice(index, 1)
우지원's avatar
우지원 committed
112
113
114
115
116
  await Room.update({ member: room.member }, { where: { id: roomId } });

  const user = await User.findOne({ where: { id: id } });
  console.log(user.roomNumber)
  const index2 = user.roomNumber.indexOf(id)
117
118
  console.log('index', index2)
  user.roomNumber.splice(index2, 1)
우지원's avatar
우지원 committed
119
120
121
  await User.update({ roomNumber: user.roomNumber }, { where: { id: id } });
}

122
123
const joinChannel = async (req, res) => {
  const { roomId, channelName, plusUser, index } = req.body
우지원's avatar
우지원 committed
124
  console.log('연결성공')
125
126
  const room = await Room.findOne({ where: { id: roomId } });
  room.channel[index][channelName].push(plusUser)
우지원's avatar
우지원 committed
127
128
  console.log('확인2', room.channel[index])
  console.log('확인2', room.channel)
129
130
131
132
  await Room.update({ channel: room.channel }, { where: { id: roomId } });
  return res.json(true)
}

우지원's avatar
우지원 committed
133
134
135
136
137
138
139
140
141
142
143
const doubleJoin = async (req, res) => {
  console.log('연결성공', req.body)
  const { roomId, index1, index2, joinChName } = req.body
  console.log(index1)
  const room = await Room.findOne({ where: { id: roomId } });
  console.log(room.channel[index1][joinChName])
  room.channel[index1][joinChName].splice(index2, 1)
  console.log(room.channel[index1][joinChName])
  console.log('room.channel', room.channel)
  await Room.update({ channel: room.channel }, { where: { id: roomId } });
  return res.json(true)
우지원's avatar
0805    
우지원 committed
144
145
}

우지원's avatar
우지원 committed
146
147
148
149
150
// const makeChannel = async (req, res) => {
//   const { roomId, channelName } = req.body
//   console.log(roomId, channelName)
// }

Kim, Chaerin's avatar
Kim, Chaerin committed
151
export default {
우지원's avatar
우지원 committed
152
  joinRoom, roomImgUpload, createRoom, getRoom, exitRoom, joinChannel, doubleJoin,
Kim, Chaerin's avatar
Kim, Chaerin committed
153
};