Commit fd82fdde authored by Soo Hyun Kim's avatar Soo Hyun Kim
Browse files

soo0115 룸스키마변경

parent 1b0c048c
......@@ -17,8 +17,8 @@ import Chat from "../Components/Chat";
const socket = io();
const INIT_CHATR = {
name: '',
const INIT_ROOM = {
roomName: '',
interest: '',
isOpen: false
}
......@@ -27,7 +27,7 @@ function Home() {
const [show, setShow] = useState(false);
const [show2, setShow2] = useState(false);
const [chat, setChat] = useState(false);
const [chatR, setChatR] = useState(INIT_CHATR);
const [room, setRoom] = useState(INIT_ROOM);
const [disabled, setDisabled] = useState(true);
const [error, setError] = useState('');
......@@ -43,21 +43,22 @@ function Home() {
// variant="pills"
useEffect(() => {
const isChatR = Object.values(chatR).every(el => Boolean(el))
isChatR ? setDisabled(false) : setDisabled(true)
}, [chatR])
const isRoom = Object.values(room).every(el => Boolean(el))
isRoom ? setDisabled(false) : setDisabled(true)
}, [room])
function handleChange(event) {
const { name, value } = event.target
setChatR({ ...chatR, [name]: value })
setRoom({ ...room, [name]: value })
console.log(room)
}
async function handleSubmit(event) {
event.preventDefault()
try {
setError('')
const response = await axios.post('chat/makeChat', chatR)
setChatR(INIT_CHATR)
const response = await axios.post('/room/makeRoom', room)
setRoom(INIT_ROOM)
} catch (error){
catchErrors(error, setError)
}
......@@ -124,13 +125,13 @@ function Home() {
<Form.Group as={Row} controlId="chatName">
<Form.Label column sm={4}> 이름</Form.Label>
<Col>
<Form.Control name='name' type='text' value={chatR.name} onChange={handleChange} />
<Form.Control name='roomName' type='text' value={room.roomName} onChange={handleChange} />
</Col>
</Form.Group>
<Form.Group as={Row} controlId="chatInterest">
<Form.Label column sm={4}>관심 분야</Form.Label>
<Col>
<Form.Control as="select" defaultValue="Choose..." name='interest' type='text' value={chatR.interest} onChange={handleChange}>
<Form.Control as="select" defaultValue="Choose..." name='interest' type='text' value={room.interest} onChange={handleChange}>
<option>Choose...</option>
<option>과학</option>
<option>수학</option>
......@@ -146,13 +147,13 @@ function Home() {
<Col>
<Form.Check
type="checkbox"
checked={chatR.isOpen}
checked={room.isOpen}
name='isOpen'
onChange={() => setChatR({ ...chatR, isOpen: !chatR.isOpen })} />
onChange={() => setRoom({ ...room, isOpen: !room.isOpen })} />
</Col>
</Form.Group>
{
(chatR.isOpen)
(room.isOpen)
? (<p><b>공개방</b>으로 개설되어 공개방 목록에 공개되며, 코드를 공유하여 참가할 수도 있습니다.</p>)
: (<p><b>비밀방</b>으로 개설되며, 참여자들에게 코드를 공유해야합니다.</p>)
}
......
// import { useState } from 'react'
import Chat from "../models/Chat.js"
import Room from "../models/Room.js"
import { customAlphabet } from 'nanoid'
import isLength from 'validator/lib/isLength.js'
const nanoid = customAlphabet('1234567890abcdef', 10)
const makeChat = async (req, res) => {
const { name, interest, isOpen } = req.body;
const makeRoom = async (req, res) => {
console.log(req.body)
const { roomName, interest, isOpen } = req.body;
console.log(roomName, interest, isOpen)
const roomId = nanoid()
const chat = await Chat.findOne({ roomId })
while (chat) {
const room = await Room.findOne({ roomId })
while (room) {
roomId = nanoid()
chat = await Chat.findOne({ roomId })
room = await Room.findOne({ roomId })
}
try {
if (!isLength(name, { min: 3, max: 20 })) {
if (!isLength(roomName, { min: 3, max: 20 })) {
return res.status(422).send('채팅방의 이름은 3-20자여야 합니다.')
} else if (interest=='Choose...' || interest==''){
return res.status(422).send('분야를 반드시 선택하여야 합니다.')
}
const newChat = await new Chat({
const newRoom = await new Room({
roomId,
name,
roomName,
interest,
isOpen
}).save()
console.log(newChat)
res.json(newChat)
console.log(newRoom)
res.json(newRoom)
} catch (error) {
console.log(error)
res.status(500).send('방생성 에러')
}
}
const hello = (req, res) => {
res.send('Hello from users controller')
}
export default { makeChat, hello }
\ No newline at end of file
export default { makeRoom }
\ No newline at end of file
......@@ -2,13 +2,13 @@ import mongoose from 'mongoose'
const {String} = mongoose.Schema.Types
const ChatSchema = new mongoose.Schema({
const RoomSchema = new mongoose.Schema({
roomId: {
type: String,
// default:() => nanoid(),
unique: true
},
name: {
roomName: {
type: String,
required: true,
},
......@@ -25,4 +25,4 @@ const ChatSchema = new mongoose.Schema({
timestamps: true
})
export default mongoose.models.Chat || mongoose.model('Chat', ChatSchema)
\ No newline at end of file
export default mongoose.models.Room || mongoose.model('Room', RoomSchema)
\ No newline at end of file
import express from 'express'
import chatCtrl from '../controllers/chat.controller.js'
const router = express.Router()
router.route('/chat/makeChat')
.post(chatCtrl.makeChat)
.get(chatCtrl.hello)
export default router
\ No newline at end of file
import express from 'express'
import roomCtrl from '../controllers/room.controller.js'
const router = express.Router()
router.route('/room/makeRoom')
.post(roomCtrl.makeRoom)
export default router
\ No newline at end of file
import express from 'express'
import connectDb from './utils/connectDb.js'
import chatRouter from './routes/chat.routers.js'
import roomRouter from './routes/room.routers.js'
import cors from "cors"
import bodyParser from "body-parser"
import http from 'http'
......@@ -11,7 +11,7 @@ connectDb()
const app = express()
app.use(express.json())
app.use(chatRouter)
app.use(roomRouter)
const server = http.createServer(app);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment