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'
Kim, Chaerin's avatar
Kim, Chaerin committed
3
import config from "../config/app.config.js";
우지원's avatar
0716    
우지원 committed
4
import isLength from 'validator/lib/isLength.js'
Kim, Chaerin's avatar
Kim, Chaerin committed
5

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

우지원's avatar
e    
우지원 committed
8
9
10
11
const joinRoom = async (req, res) => {
  console.log('req.body.value:', req.body)
  const { userId, roomId } = req.body
  const room_Id = await Room.findOne({ where: { id: roomId } });
우지원's avatar
0726    
우지원 committed
12
13
14
  console.log('room_Id1:', room_Id)
  console.log('room_Id있는지:', Boolean(room_Id))
  if (Boolean(room_Id)) {
우지원's avatar
e    
우지원 committed
15
    //roomId에 일치하는 방의 member정보에 userId 저장하기
우지원's avatar
0726    
우지원 committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    const userInter = await Room.findOne({
      where: { id: roomId },
      Include: { member: userId }
    }).then(_ => console.log('userInter되었습니다!'));
    //const userInter = await Room.findOne({ Include: [{ id: roomId}, {member: userId}] });
    console.log('userInter:',userInter)
    console.log('userId:',userId)
    if (Boolean(userInter)) {
      return res.status(422).send('이미 참여된 방입니다.')
    }
    else {
      room_Id.member.push(userId)
      console.log('room_Id.member2:', room_Id.member)
      await Room.update({ 'member': room_Id.member }, { where: { id: roomId } })
      const roomID = await Room.findOne({ where: { id: roomId } });
      console.log('room_Id2:', roomID.member)
      // }
      //userId에 일치하는 사용자의 roomNumber에 roomId저장하기
      const user_Id = await User.findOne({ where: { id: userId } });
      console.log('user_Id:', user_Id)
      console.log('user_Id.roomNumber1:', user_Id.roomNumber)
      if (user_Id.roomNumber) {
        user_Id.roomNumber.push(roomId)
      }
      else {
        user_Id.roomNumber = roomId
      }
      console.log('user_Id.roomNumber2:', [user_Id.roomNumber])
      await User.update({ 'roomNumber': [user_Id.roomNumber] }, { where: { id: userId } })
      // .then(_ => res.json(true))
      // const userID = await User.findOne({ where: { id: userId } });
      // console.log('user_Id2:', userID.roomNumber)
    }
우지원's avatar
e    
우지원 committed
49
50
  } else {
    return res.status(422).send('참여코드와 일치하는 방이 존재하지 않습니다.')
Kim, Chaerin's avatar
Kim, Chaerin committed
51
52
53
  }
};

우지원's avatar
e    
우지원 committed
54
55
const createRoom = async (req, res) => {
  console.log('룸정보', req.body)
우지원's avatar
0726    
우지원 committed
56
  const { name, owner, member, profileimg } = req.body;
우지원's avatar
0716    
우지원 committed
57
  const id = nanoid()
우지원's avatar
e    
우지원 committed
58
  const Id = await Room.findOne({ where: { id: id } });
우지원's avatar
0726    
우지원 committed
59
60
  console.log('id:', id)
  while (Boolean(Id)) {
우지원's avatar
e    
우지원 committed
61
62
    const id = nanoid()
    const Id = await Room.findOne({ where: { id: id } });
우지원's avatar
0716    
우지원 committed
63
64
65
  }
  try {
    if (!isLength(name, { min: 3, max: 20 })) {
우지원's avatar
e    
우지원 committed
66
      console.log('방이름은 3-20자여야 합니다.')
우지원's avatar
0716    
우지원 committed
67
68
      return res.status(422).send('방이름은 3-20자여야 합니다.')
    }
우지원's avatar
0726    
우지원 committed
69
    const newRoom = {
우지원's avatar
e    
우지원 committed
70
71
72
      id: id,
      name: name,
      owner: owner,
우지원's avatar
0726    
우지원 committed
73
      member: [member],
우지원's avatar
e    
우지원 committed
74
      profileimg: profileimg,
우지원's avatar
0726    
우지원 committed
75
    }
우지원's avatar
e    
우지원 committed
76
    console.log('newRoom:', newRoom)
우지원's avatar
0726    
우지원 committed
77
    await Room.create(newRoom).then(_ => console.log('room정보:', id, name, owner, [member], profileimg))
우지원's avatar
0716    
우지원 committed
78
79
80
81
82
83
84
    res.json(newRoom)
  } catch (error) {
    console.log(error)
    res.status(500).send('방생성 에러')
  }
}

Kim, Chaerin's avatar
Kim, Chaerin committed
85
export default {
우지원's avatar
e    
우지원 committed
86
  joinRoom, createRoom
Kim, Chaerin's avatar
Kim, Chaerin committed
87
};