From 4d2055137119604b9abe79246e3e864457f02cfa Mon Sep 17 00:00:00 2001 From: Hyein Jeong Date: Tue, 12 Nov 2024 18:54:18 +0900 Subject: [PATCH] =?UTF-8?q?[BE][Feat]=20#45=20:=20=EC=B1=84=EB=84=90=20id?= =?UTF-8?q?=EB=A1=9C=20=EC=B1=84=EB=84=90=20=EC=A0=95=EB=B3=B4,=20guest=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20get=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 채널 id로 채널 정보, guest 정보 get API에 대한 swagger 문서 추가 - 채널 id로 채널 정보, guest 정보 get API 구현 --- backend/src/controllers/channelController.js | 25 ++++- backend/src/repositories/channelRepository.js | 43 +++++++- backend/src/routes/channelRouter.js | 42 +++++++- backend/src/services/channelService.js | 17 ++- backend/swaggerConfig.js | 100 ++++++++++++++++++ 5 files changed, 220 insertions(+), 7 deletions(-) diff --git a/backend/src/controllers/channelController.js b/backend/src/controllers/channelController.js index 4be62cc7..da3157fc 100644 --- a/backend/src/controllers/channelController.js +++ b/backend/src/controllers/channelController.js @@ -1,4 +1,8 @@ -import { addGuestService, createChannelService } from '../services/channelService.js'; +import { + addGuestService, + createChannelService, + getChannelByIdService, +} from '../services/channelService.js'; export const createChannelController = async (req, res) => { try { @@ -46,3 +50,22 @@ export const addGuestController = async (req, res) => { }); } }; + +export const getChannelInfoController = async (req, res) => { + const { id } = req.params; + + try { + const channel = await getChannelByIdService(id); + if (!channel) { + return res.status(404).json({ success: false, message: 'Channel not found' }); + } + return res.status(200).json({ + success: true, + message: 'Get channel successfully', + data: channel, + }); + } catch (err) { + console.error(err); + return res.status(500).json({ success: false, message: 'Server error' }); + } +}; diff --git a/backend/src/repositories/channelRepository.js b/backend/src/repositories/channelRepository.js index 89619a21..8877125b 100644 --- a/backend/src/repositories/channelRepository.js +++ b/backend/src/repositories/channelRepository.js @@ -14,10 +14,10 @@ export const createChannelInDB = async (name, host_id) => { return result.rows[0]; }; -export const getChannelById = async channelId => { +export const getChannelInfoByIdInDB = async channelId => { const query = ` SELECT * - FROM "main"."channel" + FROM "main"."channel" WHERE id = $1; `; const values = [channelId]; @@ -29,3 +29,42 @@ export const getChannelById = async channelId => { return result.rows[0]; }; + +export const getChannelWithGuestsByIdFromDB = async id => { + try { + const channelQuery = 'SELECT * FROM "main"."channel" WHERE id = $1'; + const guestQuery = 'SELECT * FROM "main"."guest" WHERE channel_id = $1'; + + const channelResult = await pool.query(channelQuery, [id]); + if (channelResult.rows.length === 0) { + return null; + } + + const channel = channelResult.rows[0]; + + const guestResult = await pool.query(guestQuery, [id]); + + const guests = guestResult.rows; + + return { + id: channel.id, + name: channel.name, + host_id: channel.host_id, + guests: guests.map(guest => ({ + id: guest.id, + name: guest.name, + start_location: { + lat: guest.start_location.lat, + lng: guest.start_location.lng, + }, + end_location: { + lat: guest.end_location.lat, + lng: guest.end_location.lng, + }, + })), + }; + } catch (error) { + console.error('Database error:', error); + throw error; + } +}; diff --git a/backend/src/routes/channelRouter.js b/backend/src/routes/channelRouter.js index 0738bb36..9f9ca076 100644 --- a/backend/src/routes/channelRouter.js +++ b/backend/src/routes/channelRouter.js @@ -1,6 +1,10 @@ import express from 'express'; -import { body } from 'express-validator'; -import { addGuestController, createChannelController } from '../controllers/channelController.js'; +import { body, param } from 'express-validator'; +import { + addGuestController, + createChannelController, + getChannelInfoController, +} from '../controllers/channelController.js'; import { validationMiddleware } from '../middleware/validationMiddleware.js'; export const channelRouter = express.Router(); @@ -133,3 +137,37 @@ channelRouter.post( validationMiddleware, addGuestController, ); + +/** + * @swagger + * /channels/{id}: + * get: + * summary: 'Get channel information' + * description: 'Retrieve information about a specific channel by ID.' + * operationId: 'getChannelInfo' + * parameters: + * - in: path + * name: id + * required: true + * description: 'ID of the channel to retrieve' + * schema: + * type: string + * example: '123e4567-e89b-12d3-a456-426614174000' + * responses: + * 200: + * description: 'Channel information retrieved successfully' + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/ChannelInfo' + * 404: + * description: 'Channel not found' + * 500: + * description: 'Server error' + */ +channelRouter.get( + '/:id', + [param('id').notEmpty().withMessage('Channel ID is required')], + validationMiddleware, + getChannelInfoController, +); diff --git a/backend/src/services/channelService.js b/backend/src/services/channelService.js index 062a1013..69e06501 100644 --- a/backend/src/services/channelService.js +++ b/backend/src/services/channelService.js @@ -1,4 +1,8 @@ -import { createChannelInDB, getChannelById } from '../repositories/channelRepository.js'; +import { + createChannelInDB, + getChannelInfoByIdInDB, + getChannelWithGuestsByIdFromDB, +} from '../repositories/channelRepository.js'; import { addGuestToChannel } from '../repositories/guestRepository.js'; export const createChannelService = async (name, host_id, guests) => { @@ -23,7 +27,7 @@ export const createChannelService = async (name, host_id, guests) => { }; export const addGuestService = async (channelId, guests) => { - const channel = await getChannelById(channelId); + const channel = await getChannelInfoByIdInDB(channelId); if (!channel) return null; const guestPromises = guests.map(guest => { @@ -43,3 +47,12 @@ export const addGuestService = async (channelId, guests) => { return channel; }; + +export const getChannelByIdService = async id => { + try { + return await getChannelWithGuestsByIdFromDB(id); + } catch (error) { + console.error('Error fetching channel:', error); + throw error; + } +}; diff --git a/backend/swaggerConfig.js b/backend/swaggerConfig.js index 9a18f563..c8a32a8c 100644 --- a/backend/swaggerConfig.js +++ b/backend/swaggerConfig.js @@ -249,6 +249,66 @@ const swaggerDefinition = { }, }, }, + ChannelResponse: { + type: 'object', + properties: { + id: { + type: 'string', + description: '채널 ID (UUID 형태)', + }, + name: { + type: 'string', + description: '채널 이름', + }, + host_id: { + type: 'string', + description: '채널의 호스트 ID', + }, + guests: { + type: 'array', + items: { + type: 'object', + properties: { + id: { + type: 'string', + description: '게스트 ID (UUID 형태)', + }, + name: { + type: 'string', + description: '게스트 이름', + }, + start_location: { + type: 'object', + properties: { + lat: { + type: 'number', + description: '출발지 마커의 위도', + }, + lng: { + type: 'number', + description: '출발지 마커의 경도', + }, + }, + }, + end_location: { + type: 'object', + properties: { + lat: { + type: 'number', + description: '도착지 마커의 위도', + }, + lng: { + type: 'number', + description: '도착지 마커의 경도', + }, + }, + }, + }, + }, + description: '해당 채널의 게스트 목록', + }, + }, + }, }, }, paths: { @@ -366,6 +426,46 @@ const swaggerDefinition = { }, }, }, + '/channel/{id}': { + get: { + summary: '채널 정보 조회 API', + description: + '채널의 ID를 통해 해당 채널에 속한 게스트들의 정보를 포함한 채널 정보를 조회합니다.', + operationId: 'getChannelInfo', + parameters: [ + { + name: 'id', + in: 'path', + description: '채널의 고유 ID (UUID 형식)', + required: true, + schema: { + type: 'string', + }, + }, + ], + responses: { + 200: { + description: '채널 정보 조회 성공', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/ChannelResponse', + }, + }, + }, + }, + 400: { + description: '잘못된 요청, 잘못된 채널 ID 형식', + }, + 404: { + description: '채널을 찾을 수 없음', + }, + 500: { + description: '서버 에러', + }, + }, + }, + }, }, };