From a44fcaf12d56b5994ddc5cadd600415bc64a3fe3 Mon Sep 17 00:00:00 2001 From: Hyein Jeong Date: Tue, 12 Nov 2024 15:42:49 +0900 Subject: [PATCH] =?UTF-8?q?[BE][Feat]=20:=20#49=20:=20=EA=B2=BD=EB=A1=9C?= =?UTF-8?q?=EB=B0=A9=20=EC=83=9D=EC=84=B1=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - uuid 생성을 위한 패키지 설치 - eslint 백엔드 설정 수정 - 채널 생성 api 구현 (router, controller, service, repository) - guest 추가 api 구현 - postman 테스트 --- backend/package.json | 1 + backend/src/controllers/channelController.js | 21 ++++++++++++ backend/src/index.js | 2 ++ backend/src/repositories/channelRepository.js | 15 +++++++++ backend/src/repositories/guestRepository.js | 32 +++++++++++++++++++ backend/src/routes/channelRouter.js | 16 ++++++++++ backend/src/services/channelService.js | 25 +++++++++++++++ eslint.config.mjs | 2 ++ pnpm-lock.yaml | 9 ++++++ 9 files changed, 123 insertions(+) create mode 100644 backend/src/controllers/channelController.js create mode 100644 backend/src/repositories/channelRepository.js create mode 100644 backend/src/repositories/guestRepository.js create mode 100644 backend/src/routes/channelRouter.js create mode 100644 backend/src/services/channelService.js diff --git a/backend/package.json b/backend/package.json index eb7ad713..3e06776f 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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" } } diff --git a/backend/src/controllers/channelController.js b/backend/src/controllers/channelController.js new file mode 100644 index 00000000..da24d063 --- /dev/null +++ b/backend/src/controllers/channelController.js @@ -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', + }); + } +}; diff --git a/backend/src/index.js b/backend/src/index.js index 0f772826..6e822161 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -6,6 +6,7 @@ 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()); @@ -13,6 +14,7 @@ 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) => { diff --git a/backend/src/repositories/channelRepository.js b/backend/src/repositories/channelRepository.js new file mode 100644 index 00000000..335d134b --- /dev/null +++ b/backend/src/repositories/channelRepository.js @@ -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]; +}; diff --git a/backend/src/repositories/guestRepository.js b/backend/src/repositories/guestRepository.js new file mode 100644 index 00000000..6f29611b --- /dev/null +++ b/backend/src/repositories/guestRepository.js @@ -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]; +}; diff --git a/backend/src/routes/channelRouter.js b/backend/src/routes/channelRouter.js new file mode 100644 index 00000000..d2733243 --- /dev/null +++ b/backend/src/routes/channelRouter.js @@ -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, +); diff --git a/backend/src/services/channelService.js b/backend/src/services/channelService.js new file mode 100644 index 00000000..4c2ecc03 --- /dev/null +++ b/backend/src/services/channelService.js @@ -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; +}; diff --git a/eslint.config.mjs b/eslint.config.mjs index f2f552a9..ecb18e3e 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -113,6 +113,8 @@ export default [ 'no-console': 'off', 'consistent-return': 'off', 'import/extensions': 'off', + camelcase: 'off', + 'no-shadow': 'off', }, }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index de1686db..be3cc0be 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -129,6 +129,9 @@ importers: swagger-ui-express: specifier: ^5.0.1 version: 5.0.1(express@4.21.1) + uuid: + specifier: ^11.0.3 + version: 11.0.3 ws: specifier: ^8.11.0 version: 8.11.0 @@ -7301,6 +7304,10 @@ packages: resolution: {integrity: sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==} engines: {node: '>= 0.4.0'} + uuid@11.0.3: + resolution: {integrity: sha512-d0z310fCWv5dJwnX1Y/MncBAqGMKEzlBb1AOf7z9K8ALnd0utBX/msg/fA0+sbyN1ihbMsLhrBlnl1ak7Wa0rg==} + hasBin: true + uuid@8.3.2: resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==} hasBin: true @@ -16580,6 +16587,8 @@ snapshots: utils-merge@1.0.1: {} + uuid@11.0.3: {} + uuid@8.3.2: {} uuid@9.0.1: {}