Skip to content

Commit

Permalink
[BE][Feat] : #49 : 경로방 생성 api
Browse files Browse the repository at this point in the history
- uuid 생성을 위한 패키지 설치
- eslint 백엔드 설정 수정
- 채널 생성 api 구현 (router, controller, service, repository)
- guest 추가 api 구현
- postman 테스트
  • Loading branch information
happyhyep committed Nov 12, 2024
1 parent 4f606e5 commit a44fcaf
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"pg": "^8.13.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"uuid": "^11.0.3",
"ws": "^8.11.0"
}
}
21 changes: 21 additions & 0 deletions backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createChannelService } from '../services/channelService.js';

export const createChannelController = async (req, res) => {
try {
const { name, host_id, guests } = req.body;

const channel = await createChannelService(name, host_id, guests);

return res.status(201).json({
success: true,
message: 'Channel created successfully',
data: channel,
});
} catch (err) {
console.error(err);
return res.status(500).json({
success: false,
message: 'Server error',
});
}
};
2 changes: 2 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { pool } from './db/db.js';
import { PORT } from './constants/constants.js';
import { initializeWebSocketServer } from './websocketServer.js';
import { authRouter } from './routes/authRouter.js';
import { channelRouter } from './routes/channelRouter.js';

const app = express();
app.use(express.json());

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

app.use('/api/auth', authRouter);
app.use('/api/channel', channelRouter);

// TODO: 데이터베이스에서 데이터 가져오기 예시
app.get('/guests', async (req, res) => {
Expand Down
15 changes: 15 additions & 0 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { v4 as uuidv4 } from 'uuid';
import { pool } from '../db/db.js';

export const createChannelInDB = async (name, host_id) => {
const id = uuidv4();
const query = `
INSERT INTO "main"."channel" (id, name, host_id)
VALUES ($1, $2, $3)
RETURNING *;
`;
const values = [id, name, host_id];
const result = await pool.query(query, values);

return result.rows[0];
};
32 changes: 32 additions & 0 deletions backend/src/repositories/guestRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { v4 as uuidv4 } from 'uuid';
import { pool } from '../db/db.js';

export const addGuestInDB = async (
channel_id,
name,
start_location,
end_location,
path,
marker_style,
host_id,
) => {
const guest_id = uuidv4();
const query = `
INSERT INTO "main"."guest" (id, channel_id, name, start_location, end_location, path, marker_style, host_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
`;
const values = [
guest_id,
channel_id,
name,
start_location,
end_location,
path,
marker_style,
host_id,
];
const result = await pool.query(query, values);

return result.rows[0];
};
16 changes: 16 additions & 0 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express';
import { body } from 'express-validator';
import { createChannelController } from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';

export const channelRouter = express.Router();

channelRouter.post(
'/',
[
body('name').notEmpty().withMessage('Name is required'),
body('host_id').notEmpty().withMessage('Host ID is required'),
],
validationMiddleware,
createChannelController,
);
25 changes: 25 additions & 0 deletions backend/src/services/channelService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createChannelInDB } from '../repositories/channelRepository.js';
import { addGuestInDB } from '../repositories/guestRepository.js';

export const createChannelService = async (name, host_id, guests) => {
// 채널 생성
const channel = await createChannelInDB(name, host_id);

// 게스트 추가
const guestPromises = guests.map(guest => {
const { name, start_location, end_location, path, marker_style } = guest;
return addGuestInDB(
channel.id,
name,
start_location,
end_location,
path,
marker_style,
host_id,
);
});

await Promise.all(guestPromises);

return channel;
};
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export default [
'no-console': 'off',
'consistent-return': 'off',
'import/extensions': 'off',
camelcase: 'off',
'no-shadow': 'off',
},
},

Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a44fcaf

Please sign in to comment.