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