Skip to content

Commit

Permalink
Merge pull request #18 from PizzaPickle/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
sooyeon-kr authored Sep 7, 2024
2 parents 5200b06 + f1b4547 commit 0457755
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pickle",
"version": "1.0.0",
"type": "module",
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
Expand All @@ -14,11 +14,17 @@
"@babel/node": "^7.25.0",
"@babel/preset-env": "^7.25.4",
"amqplib": "^0.10.4",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"nodemon": "^3.1.4",
"pug": "^3.0.3",
"redis": "^4.7.0",
"socket.io": "^4.7.5"
}
},
"proxy": [
"https://pickle.my",
"http://127.0.0.1:5173",
"https://api.dicebear.com/9.x/fun-emoji/svg"
]
}
34 changes: 27 additions & 7 deletions src/controllers/consultingController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { getRoomList as getRoomListFromRedis } from '../redis_client.js';
export function getConsultingRoom(req, res) {
const { roomId } = req.params;
const { userName, userId } = req.body;
const { roomId } = req.params;
const { userName, userId } = req.body;

if (!roomId || !userName || !userId) {
return res.status(400).json({ message: '모든 필드가 필요합니다.' });
}
if (!roomId || !userName || !userId) {
return res.status(400).json({ message: '모든 필드가 필요합니다.' });
}

console.log(roomId, userName, userId);
res.render('consultingRoom', { roomId, userName, userId });
console.log(roomId, userName, userId);
res.render('consultingRoom', { roomId, userName, userId });
}

export async function getRoomList(req, res) {
try {
const { userId } = req.query;
console.log('Requested User ID:', userId);

const roomList = await getRoomListFromRedis(userId);
console.log('Fetched room list from Redis:', roomList);

if (!roomList) {
return res.status(404).json({ error: 'Room list not found' });
}

res.json(roomList); // JSON 형식으로 응답 전송
} catch (error) {
console.error('Error fetching room list:', error);
res.status(500).json({ error: 'Failed to fetch room list' });
}
}
13 changes: 11 additions & 2 deletions src/redis_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ async function getRoomList(userId) {
for (const key of keys) {
const customerId = await redisClient.hGet(key, 'customerId');
const pbId = await redisClient.hGet(key, 'pbId');

if (userId === customerId || userId === pbId) {
roomList.push(await redisClient.hGetAll(key));
const roomData = await redisClient.hGetAll(key);
roomList.push({
roomId: roomData.roomId,
date: roomData.date,
customerId: roomData.customerId,
customerName: roomData.customerName,
pbId: roomData.pbId,
pbName: roomData.pbName,
pbImage: roomData.pbImage,
pbBranchOffice: roomData.pbBranchOffice,
});
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/routes/consultingRoutes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import express from 'express';
import { getConsultingRoom } from '../controllers/consultingController.js'; // .js 확장자 포함
import { getConsultingRoom, getRoomList } from '../controllers/consultingController.js'; // .js 확장자 포함

const router = express.Router();

router.get('/api/consulting-room', getRoomList);
router.post('/api/consulting-room/:roomId', getConsultingRoom);

export default router;
5 changes: 1 addition & 4 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import consultingRoutes from './consultingRoutes.js';

export default function setupRoutes(app) {
app.use(consultingRoutes);

// 기본 라우트
app.get('/', (req, res) => res.send('Welcome to the Consulting API'));
app.use(consultingRoutes);
}
11 changes: 11 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import setupRoutes from './routes/index.js';
import bodyParser from 'body-parser';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import cors from 'cors';
import consultingRoutes from './routes/consultingRoutes.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -19,11 +21,20 @@ app.set('view engine', 'pug');
app.set('views', join(__dirname, 'views'));
app.use('/public', express.static(join(__dirname, 'public')));

app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', ENV.REACT_APP_ORIGIN);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});

// 기타 설정 및 소켓 설정
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: ENV.REACT_APP_ORIGIN,
methods: ['GET', 'POST'],
credentials: true, // 소켓 통신을 위한 CORS 설정
},
});
setupRoutes(app);
Expand Down

0 comments on commit 0457755

Please sign in to comment.