Commit a75aa18e authored by 우지원's avatar 우지원
Browse files

0726

parent 3963ae7c
import axios from "axios"; import axios from "axios";
const getUser = async (payload) => {
const { data } = await axios.get("/api/user", payload);
return data;
};
const login = async (payload) => { const login = async (payload) => {
const { data } = await axios.post("/api/login", payload); const { data } = await axios.post("/api/login", payload);
return data; return data;
...@@ -10,6 +15,6 @@ const signup = async (payload) => { ...@@ -10,6 +15,6 @@ const signup = async (payload) => {
return data; return data;
}; };
const userApi = { login, signup }; const userApi = { getUser, login, signup };
export default userApi; export default userApi;
...@@ -45,6 +45,7 @@ const AddRoom = () => { ...@@ -45,6 +45,7 @@ const AddRoom = () => {
className="p-3" className="p-3"
data-bs-toggle="modal" data-bs-toggle="modal"
data-bs-target="#joinModal" data-bs-target="#joinModal"
// data-bs-dismiss="modal"
style={{ style={{
display: "flex", display: "flex",
flexDirection: "column", flexDirection: "column",
...@@ -73,6 +74,7 @@ const AddRoom = () => { ...@@ -73,6 +74,7 @@ const AddRoom = () => {
className="p-3" className="p-3"
data-bs-toggle="modal" data-bs-toggle="modal"
data-bs-target="#makeModal" data-bs-target="#makeModal"
// data-bs-dismiss="modal"
style={{ style={{
display: "flex", display: "flex",
flexDirection: "column", flexDirection: "column",
......
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Redirect } from "react-router-dom";
import roomApi from "../../apis/room.api"; import roomApi from "../../apis/room.api";
const userId = localStorage.getItem('user'); const userId = localStorage.getItem('user');
console.log(userId) console.log(userId)
const INIT_ROOM = { const INIT_ROOM = {
name: '', name: '',
owner: userId,
member: userId,
profileimg: '', profileimg: '',
channel: ['회의'],
} }
const CreateRoom = () => { const CreateRoom = () => {
...@@ -20,11 +22,6 @@ const CreateRoom = () => { ...@@ -20,11 +22,6 @@ const CreateRoom = () => {
isRoom ? setDisabled(false) : setDisabled(true); isRoom ? setDisabled(false) : setDisabled(true);
}, [room]); }, [room]);
// const userId = localStorage.getItem('user');
// setRoom({...room, owner: userId})
// setRoom({...room, member: userId})
function handleChange(event) { function handleChange(event) {
console.log(room) console.log(room)
const { name, value } = event.target const { name, value } = event.target
...@@ -39,9 +36,9 @@ const CreateRoom = () => { ...@@ -39,9 +36,9 @@ const CreateRoom = () => {
const res = await roomApi.create(room) const res = await roomApi.create(room)
// console.log(res) // console.log(res)
const Id = res.id const Id = res.id
// console.log(Id) console.log(Id)
//alert(`방참여코드는 ${Id}입니다`) alert(`방참여코드는 ${Id}입니다`)
//setSuccess(true) setSuccess(true)
} catch (error) { } catch (error) {
// catchErrors(error, setError); // catchErrors(error, setError);
} finally { } finally {
...@@ -52,7 +49,7 @@ const CreateRoom = () => { ...@@ -52,7 +49,7 @@ const CreateRoom = () => {
if (success) { if (success) {
console.log('success', success) console.log('success', success)
alert('룸생성이 완료되었습니다!') alert('룸생성이 완료되었습니다!')
//return <Redirect to='/' /> return <Redirect to='/user' />
} }
const { name, owner, member, profileimg } = room; const { name, owner, member, profileimg } = room;
......
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { useState, useEffect } from "react";
import userApi from "../../apis/user.api";
const INIT_USER = {
id: '',
name: '',
img: '',
}
const HomeProfile = () => { const HomeProfile = () => {
const id = 1; const [user, setUser] = useState(INIT_USER)
const id = localStorage.getItem('user');
async function getUser(id) {
try {
// `/users/${userId}`랑 userId랑 같은지 확인
const User = await userApi.user(id)
console.log(User)
setUser(User.data)
console.log(user)
} catch (error) {
// catchErrors(error, setError)
}
}
useEffect(() => {
getUser(id)
}, [id])
return ( return (
<Link to={`/profile/${id}`} className="text-decoration-none text-dark"> <Link to={`/profile/${id}`} className="text-decoration-none text-dark">
<form className="flex-column align-items-center justify-content-center m-2"> <form className="flex-column align-items-center justify-content-center m-2">
<div className="d-flex justify-content-center"> <div className="d-flex justify-content-center">
<img <img
src="cherry.jpg" // src="cherry.jpg"
className="rounded-circle" className="rounded-circle"
style={{ style={{
width: "157px", width: "157px",
height: "157px", height: "157px",
}} }}
value={user.img}
/> />
</div> </div>
<h1 className="d-flex justify-content-center"> CHERRY </h1> <h1 className="d-flex justify-content-center"> {user.name} </h1>
<h2 className="d-flex justify-content-center"> #0805 </h2> <h2 className="d-flex justify-content-center"> #{user.id} </h2>
</form> </form>
<div <div
style={{ backgroundColor: "#262626", width: "auto", height: "2px" }} style={{ backgroundColor: "#262626", width: "auto", height: "2px" }}
......
...@@ -26,9 +26,9 @@ const JoinRoom = () => { ...@@ -26,9 +26,9 @@ const JoinRoom = () => {
try { try {
// setLoading(true); // setLoading(true);
// setError(""); // setError("");
console.log('userId:',userId) console.log('userId:', userId)
console.log('roomId:',roomId) console.log('roomId:', roomId)
const data = await roomApi.join({userId: userId, roomId: roomId}); const data = await roomApi.join({ userId: userId, roomId: roomId });
console.log(data); console.log(data);
setSuccess(true); setSuccess(true);
} catch (error) { } catch (error) {
...@@ -38,6 +38,7 @@ const JoinRoom = () => { ...@@ -38,6 +38,7 @@ const JoinRoom = () => {
} }
} }
if (success) { if (success) {
return <Redirect to="/user" />;
} }
return ( return (
......
...@@ -8,82 +8,73 @@ const nanoid = customAlphabet('1234567890abcdef', 10) ...@@ -8,82 +8,73 @@ const nanoid = customAlphabet('1234567890abcdef', 10)
const joinRoom = async (req, res) => { const joinRoom = async (req, res) => {
console.log('req.body.value:', req.body) console.log('req.body.value:', req.body)
const { userId, roomId } = req.body const { userId, roomId } = req.body
console.log('userId:', userId)
console.log('roomId:', roomId)
const room_Id = await Room.findOne({ where: { id: roomId } }); const room_Id = await Room.findOne({ where: { id: roomId } });
console.log('room_Id:', room_Id) console.log('room_Id1:', room_Id)
if (room_Id) { console.log('room_Id있는지:', Boolean(room_Id))
if (Boolean(room_Id)) {
//roomId에 일치하는 방의 member정보에 userId 저장하기 //roomId에 일치하는 방의 member정보에 userId 저장하기
console.log('room_Id.member1:', room_Id.member) 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) room_Id.member.push(userId)
console.log('room_Id.member2:', room_Id.member) console.log('room_Id.member2:', room_Id.member)
await Room.update({ 'member': room_Id.member }, { where: { id: roomId } }) await Room.update({ 'member': room_Id.member }, { where: { id: roomId } })
const roomID = await Room.findOne({ where: { id: roomId } }); const roomID = await Room.findOne({ where: { id: roomId } });
console.log('room_Id2:', roomID.member) console.log('room_Id2:', roomID.member)
// }
//userId에 일치하는 사용자의 roomNumber에 roomId저장하기 //userId에 일치하는 사용자의 roomNumber에 roomId저장하기
// const user_Id = await User.findOne({ where: { id: userId } }); const user_Id = await User.findOne({ where: { id: userId } });
// console.log('user_Id:', user_Id) console.log('user_Id:', user_Id)
// console.log('user_Id.roomNumber1:', user_Id.roomNumber) console.log('user_Id.roomNumber1:', user_Id.roomNumber)
// user_Id.roomNumber.push(roomId) if (user_Id.roomNumber) {
// console.log('user_Id.roomNumber2:', user_Id.roomNumber) user_Id.roomNumber.push(roomId)
// await User.update({ 'roomNumber': roomId }, { where: { id: userId } }) }
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 } }); // const userID = await User.findOne({ where: { id: userId } });
// console.log('user_Id2:', userID.roomNumber) // console.log('user_Id2:', userID.roomNumber)
}
} else { } else {
return res.status(422).send('참여코드와 일치하는 방이 존재하지 않습니다.') return res.status(422).send('참여코드와 일치하는 방이 존재하지 않습니다.')
} }
}; };
// 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) => { const createRoom = async (req, res) => {
console.log('룸정보', req.body) console.log('룸정보', req.body)
const { name, owner, member, profileimg, channel } = req.body; const { name, owner, member, profileimg } = req.body;
console.log('owner:', owner)
const id = nanoid() const id = nanoid()
console.log('id:', id)
const Id = await Room.findOne({ where: { id: id } }); const Id = await Room.findOne({ where: { id: id } });
console.log("Id중복확인:", Id) console.log('id:', id)
while (Id) { while (Boolean(Id)) {
const id = nanoid() const id = nanoid()
const Id = await Room.findOne({ where: { id: id } }); const Id = await Room.findOne({ where: { id: id } });
} }
try { try {
if (!isLength(name, { min: 3, max: 20 })) { if (!isLength(name, { min: 3, max: 20 })) {
console.log('방이름은 3-20자여야 합니다.') console.log('방이름은 3-20자여야 합니다.')
return res.status(422).send('방이름은 3-20자여야 합니다.') return res.status(422).send('방이름은 3-20자여야 합니다.')
} }
const newRoom = {
const newRoom = await Room.create({
id: id, id: id,
name: name, name: name,
owner: owner, owner: owner,
member: member, member: [member],
profileimg: profileimg, profileimg: profileimg,
channel: channel, }
}).then(_ => console.log('room정보:', id, name, owner, member, profileimg, channel))
console.log('newRoom:', newRoom) console.log('newRoom:', newRoom)
await Room.create(newRoom).then(_ => console.log('room정보:', id, name, owner, [member], profileimg))
res.json(newRoom) res.json(newRoom)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
......
...@@ -4,14 +4,10 @@ import config from "../config/app.config.js"; ...@@ -4,14 +4,10 @@ import config from "../config/app.config.js";
import isLength from 'validator/lib/isLength.js' import isLength from 'validator/lib/isLength.js'
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
const test = async (req, res) => { const user = async (req, res) => {
try { console.log(req.body)
console.log(req); const user = await User.findAll({ where: { id: req.body } });
res.json("안녕"); req.json(user)
} catch (error) {
console.log(error);
return res.status(500).send("test 중 에러");
}
}; };
const login = async (req, res) => { const login = async (req, res) => {
...@@ -71,7 +67,7 @@ const signup = async (req, res) => { ...@@ -71,7 +67,7 @@ const signup = async (req, res) => {
} }
} }
export default { export default {
test, user,
login, login,
signup, signup,
}; };
...@@ -25,8 +25,8 @@ sequelize ...@@ -25,8 +25,8 @@ sequelize
await Room.create({ await Room.create({
id: "1234567890", id: "1234567890",
name: "room", name: "room",
owner: [ '600112d16d09ac6b7892d900' ], owner: "600112d16d09ac6b7892d900",
member: [ '600112d16d09ac6b7892d900' ], member: ['600112d16d09ac6b7892d900'],
profileimg: "C:\fakepath\스크린샷(1).png", profileimg: "C:\fakepath\스크린샷(1).png",
channel: [ '회의' ], channel: [ '회의' ],
}); });
......
...@@ -12,7 +12,7 @@ const RoomModel = (sequelize) => { ...@@ -12,7 +12,7 @@ const RoomModel = (sequelize) => {
type: DataTypes.STRING, type: DataTypes.STRING,
}, },
owner: { owner: {
type: DataTypes.ARRAY(DataTypes.STRING), type: DataTypes.STRING,
}, },
member: { member: {
type: DataTypes.ARRAY(DataTypes.STRING), type: DataTypes.ARRAY(DataTypes.STRING),
...@@ -22,6 +22,7 @@ const RoomModel = (sequelize) => { ...@@ -22,6 +22,7 @@ const RoomModel = (sequelize) => {
}, },
channel: { channel: {
type: DataTypes.ARRAY(DataTypes.JSON), type: DataTypes.ARRAY(DataTypes.JSON),
defaultValue: ['회의'],
}, },
}, },
{ timestamps: true } { timestamps: true }
......
...@@ -3,6 +3,7 @@ import userCtrl from "../controllers/user.controller.js"; ...@@ -3,6 +3,7 @@ import userCtrl from "../controllers/user.controller.js";
const router = express.Router(); const router = express.Router();
router.route("/getUser").get(userCtrl.user);
router.route("/login").post(userCtrl.login); router.route("/login").post(userCtrl.login);
router.route("/signup").post(userCtrl.signup); router.route("/signup").post(userCtrl.signup);
......
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