room.controller.js 1.1 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
import { Room } from "../models/index.js";
우지원's avatar
0716    
우지원 committed
2
//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
0716    
우지원 committed
6
7
8
//const nanoid = customAlphabet('1234567890abcdef', 10)

const roomJoin = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
9
  try {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
10
    console.log("room= ", req.body);
Kim, Chaerin's avatar
Kim, Chaerin committed
11
12
13
14
15
16
17
    res.json("안녕");
  } catch (error) {
    console.log(error);
    return res.status(500).send("안녕 중 에러");
  }
};

우지원's avatar
0716    
우지원 committed
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
const roomCreate = async (req, res) => {
  const { name, owner, member, profileimg } = req.body;
  const id = nanoid()
  const Id = await Room.findOne({ id })
  while (Id) {
    id = nanoid()
    Id = await Room.findOne({ id })
  }

  try {
    if (!isLength(name, { min: 3, max: 20 })) {
      return res.status(422).send('방이름은 3-20자여야 합니다.')
    }

    const newRoom = await new Room({
      id,
      name,
      owner,
      member,
      profileimg,
    }).save()
    // console.log(newRoom)
    res.json(newRoom)
  } catch (error) {
    console.log(error)
    res.status(500).send('방생성 에러')
  }
}

Kim, Chaerin's avatar
Kim, Chaerin committed
47
export default {
우지원's avatar
0716    
우지원 committed
48
  roomJoin, roomCreate
Kim, Chaerin's avatar
Kim, Chaerin committed
49
};