room.controller.js 3.08 KB
Newer Older
우지원's avatar
e    
우지원 committed
1
2
import { Room, User } from "../models/index.js";
import { customAlphabet } from 'nanoid'
우지원's avatar
0716    
우지원 committed
3
import isLength from 'validator/lib/isLength.js'
Kim, Chaerin's avatar
Kim, Chaerin committed
4

우지원's avatar
e    
우지원 committed
5
const nanoid = customAlphabet('1234567890abcdef', 10)
우지원's avatar
0716    
우지원 committed
6

우지원's avatar
e    
우지원 committed
7
8
9
const joinRoom = async (req, res) => {
  const { userId, roomId } = req.body
  const room_Id = await Room.findOne({ where: { id: roomId } });
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
10
  if (Boolean(room_Id)) { //roomId에 일치하는 방이 존재할때
우지원's avatar
e    
우지원 committed
11
    //roomId에 일치하는 방의 member정보에 userId 저장하기
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
12
13
14
15
16
17
18
    //member정보에 userId가 이미 저장되어 있는지 확인 -> 이미 참여된 방인지 확인
    const includeUserId = room_Id.member.includes(parseInt(userId));
    // console.log('Include확인:',includeUserId) 
    if (!includeUserId) { //아직 참여되지 않은 방인경우
      room_Id.member.push(userId) //member에 userId추가
      // console.log('room_Id.member2:', room_Id.member)
      await Room.update({ 'member': room_Id.member }, { where: { id: roomId } })
우지원's avatar
e    
우지원 committed
19

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
20
21
22
23
24
25
26
27
28
29
30
31
32
      //userId에 일치하는 사용자의 roomNumber에 roomId저장하기
      const user_Id = await User.findOne({ where: { id: userId } });
      if (Boolean(user_Id.roomNumber)) { //다른 roomNumber가 이미 들어가 있는 경우 roomId추가
        user_Id.roomNumber.push(roomId)
      }
      else { //첫 roomNumber인 경우 
        user_Id.roomNumber = [roomId]
      }
      // console.log('user_Id.roomNumber2:', user_Id.roomNumber)
      await User.update({ 'roomNumber': user_Id.roomNumber }, { where: { id: userId } })
    } else {
      return res.status(422).send('이미 참여된 방입니다.')
    }
우지원's avatar
e    
우지원 committed
33
34
  } else {
    return res.status(422).send('참여코드와 일치하는 방이 존재하지 않습니다.')
Kim, Chaerin's avatar
Kim, Chaerin committed
35
36
37
  }
};

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

    //user.roomNumber에 id추가
    const user_Id = await User.findOne({ where: { id: owner } });
    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)
    await User.update({ 'roomNumber': user_Id.roomNumber }, { where: { id: owner } })
우지원's avatar
0716    
우지원 committed
73
74
75
76
77
78
79
    res.json(newRoom)
  } catch (error) {
    console.log(error)
    res.status(500).send('방생성 에러')
  }
}

Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
80
81
82
83
const getRoom = async (req, res) => {
  console.log('req.body:', req.body)
}

Kim, Chaerin's avatar
Kim, Chaerin committed
84
export default {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
85
  joinRoom, createRoom, getRoom
Kim, Chaerin's avatar
Kim, Chaerin committed
86
};