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

우지원's avatar
우지원 committed
20
21
22
23
24
25
26
27
      //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]
      }
우지원's avatar
0727    
우지원 committed
28
      // console.log('user_Id.roomNumber2:', user_Id.roomNumber)
우지원's avatar
우지원 committed
29
30
31
      await User.update({ 'roomNumber': user_Id.roomNumber }, { where: { id: userId } })
    } else {
      return res.status(422).send('이미 참여된 방입니다.')
우지원's avatar
0726    
우지원 committed
32
    }
우지원'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
39
const createRoom = async (req, res) => {
  console.log('룸정보', req.body)
우지원's avatar
0726    
우지원 committed
40
  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 } });
우지원's avatar
0727    
우지원 committed
43
  // console.log('id:', id)
우지원's avatar
0726    
우지원 committed
44
  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자여야 합니다.')
    }
우지원's avatar
0727    
우지원 committed
52
    //새로운 RoomDB생성
우지원's avatar
0726    
우지원 committed
53
    const newRoom = {
우지원's avatar
e    
우지원 committed
54
55
56
      id: id,
      name: name,
      owner: owner,
우지원's avatar
0726    
우지원 committed
57
      member: [member],
우지원's avatar
e    
우지원 committed
58
      profileimg: profileimg,
우지원's avatar
0726    
우지원 committed
59
    }
우지원's avatar
0727    
우지원 committed
60
    // console.log('newRoom:', newRoom)
우지원's avatar
0726    
우지원 committed
61
    await Room.create(newRoom)
우지원's avatar
0727    
우지원 committed
62
63
64
65
66
67
68
69
70
71
72

    //user.roomNumber에 id추가
    const user_Id = await User.findOne({ where: { id: owner } });
    if (Boolean(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('방생성 에러')
  }
}

우지원's avatar
e    
우지원 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 {
우지원's avatar
e    
우지원 committed
85
  joinRoom, createRoom, getRoom
Kim, Chaerin's avatar
Kim, Chaerin committed
86
};