room.controller.js 3.34 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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const joinRoom = async (req, res) => {
  console.log('req.body.value:', req.body)
  const { userId, roomId } = req.body
  console.log('userId:', userId)
  console.log('roomId:', roomId)
  const room_Id = await Room.findOne({ where: { id: roomId } });
  console.log('room_Id:', room_Id)
  if (room_Id) {
    //roomId에 일치하는 방의 member정보에 userId 저장하기
    console.log('room_Id.member1:', room_Id.member)
    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)
    // user_Id.roomNumber.push(roomId)
    // console.log('user_Id.roomNumber2:', user_Id.roomNumber)
    // await User.update({ 'roomNumber': roomId }, { where: { id: userId } })
    // const userID = await User.findOne({ where: { id: userId } });
    // console.log('user_Id2:', userID.roomNumber)

  } else {
    return res.status(422).send('참여코드와 일치하는 방이 존재하지 않습니다.')
Kim, Chaerin's avatar
Kim, Chaerin committed
35
36
37
  }
};

우지원's avatar
e    
우지원 committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// const changemember = async (req, res) => {
//   const { userId, roomId } = req.body
//   // console.log(roomId)
//   let room = await Room.findOne({ roomId: roomId }).select('member')
//   const isPresent = room.member.indexOf(userId)
//   indexOf : userId가 몇번째 인덱스인지 찾기
//   try {
//       if (isPresent < 0) {
//           const memberId = room.member.push(userId)
//           await Room.updateOne({ 'roomId': roomId }, { 'member': room.member })
//           // console.log('room.member 업데이트 완료')
//           return res.json(true)
//       }
//       else {
//           return res.json(false)
//       }
//   } catch (error) {
//       res.status(500).send('멤버 업데이트 실패')
//   }
// }

const createRoom = async (req, res) => {
  console.log('룸정보', req.body)
  const { name, owner, member, profileimg, channel } = req.body;
  console.log('owner:', owner)
우지원's avatar
0716    
우지원 committed
63
  const id = nanoid()
우지원's avatar
e    
우지원 committed
64
65
66
  console.log('id:', id)
  const Id = await Room.findOne({ where: { id: id } });
  console.log("Id중복확인:", Id)
우지원's avatar
0716    
우지원 committed
67
  while (Id) {
우지원's avatar
e    
우지원 committed
68
69
    const id = nanoid()
    const Id = await Room.findOne({ where: { id: id } });
우지원's avatar
0716    
우지원 committed
70
71
72
73
  }

  try {
    if (!isLength(name, { min: 3, max: 20 })) {
우지원's avatar
e    
우지원 committed
74
      console.log('방이름은 3-20자여야 합니다.')
우지원's avatar
0716    
우지원 committed
75
76
77
      return res.status(422).send('방이름은 3-20자여야 합니다.')
    }

우지원's avatar
e    
우지원 committed
78
79
80
81
82
83
84
85
86
    const newRoom = await Room.create({
      id: id,
      name: name,
      owner: owner,
      member: member,
      profileimg: profileimg,
      channel: channel,
    }).then(_ => console.log('room정보:', id, name, owner, member, profileimg, channel))
    console.log('newRoom:', newRoom)
우지원's avatar
0716    
우지원 committed
87
88
89
90
91
92
93
    res.json(newRoom)
  } catch (error) {
    console.log(error)
    res.status(500).send('방생성 에러')
  }
}

Kim, Chaerin's avatar
Kim, Chaerin committed
94
export default {
우지원's avatar
e    
우지원 committed
95
  joinRoom, createRoom
Kim, Chaerin's avatar
Kim, Chaerin committed
96
};