From 34111fb3affed6e70ad6e8cf5aa7da1aa955306e Mon Sep 17 00:00:00 2001 From: Hyein Jeong Date: Wed, 4 Dec 2024 15:13:07 +0900 Subject: [PATCH] =?UTF-8?q?[BE][Feat]=20#415=20:=20=EC=B1=84=EB=84=90=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 채널 삭제 api 구현 - swagger 적용 --- backend/src/controllers/channelController.js | 23 +++++++++++++ backend/src/repositories/channelRepository.js | 14 ++++++++ backend/src/routes/channelRouter.js | 33 +++++++++++++++++++ backend/src/services/channelService.js | 14 ++++++++ 4 files changed, 84 insertions(+) diff --git a/backend/src/controllers/channelController.js b/backend/src/controllers/channelController.js index 619ad529..d83858fc 100644 --- a/backend/src/controllers/channelController.js +++ b/backend/src/controllers/channelController.js @@ -1,6 +1,7 @@ import { addGuestService, createChannelService, + deleteChannelService, getChannelByIdService, getChannelGuestInfoService, getUserChannels, @@ -109,3 +110,25 @@ export const getUserChannelsController = async (req, res) => { return res.status(500).json(new ErrorResponseDto({ message: 'Server error occurred' })); } }; + +/** + * @description 채널 삭제 컨트롤러 + */ +export const deleteChannelController = async (req, res) => { + const { id } = req.params; + + try { + const result = await deleteChannelService(id); + + if (!result) { + return res.status(404).json(new ErrorResponseDto({ message: 'Channel not found' })); + } + + return res + .status(200) + .json(new ResponseDto({ resultMsg: 'Channel deleted successfully', data: { id } })); + } catch (err) { + console.error(err); + return res.status(500).json(new ErrorResponseDto({ message: 'Server error occurred' })); + } +}; diff --git a/backend/src/repositories/channelRepository.js b/backend/src/repositories/channelRepository.js index 647c6e49..34862e74 100644 --- a/backend/src/repositories/channelRepository.js +++ b/backend/src/repositories/channelRepository.js @@ -155,3 +155,17 @@ export const getChannelsByUserIdFromDB = async userId => { throw error; } }; + +export const deleteChannelByIdFromDB = async id => { + try { + const query = 'DELETE FROM "main"."channel" WHERE id = $1 RETURNING id'; + const values = [id]; + + const result = await pool.query(query, values); + + return result.rowCount > 0; + } catch (error) { + console.error('Database error:', error); + throw error; + } +}; diff --git a/backend/src/routes/channelRouter.js b/backend/src/routes/channelRouter.js index 0725f449..cbc0ae00 100644 --- a/backend/src/routes/channelRouter.js +++ b/backend/src/routes/channelRouter.js @@ -3,6 +3,7 @@ import { body, param } from 'express-validator'; import { addGuestController, createChannelController, + deleteChannelController, getChannelGuestInfoController, getChannelInfoController, getUserChannelsController, @@ -190,3 +191,35 @@ channelRouter.get( validationMiddleware, getUserChannelsController, ); + +// 채널 삭제 API 경로 +/** + * @swagger + * paths: + * /channel/{id}: + * delete: + * summary: '채널 삭제 API' + * description: '채널 ID를 사용하여 특정 채널을 삭제합니다.' + * tags: [Channel] + * parameters: + * - name: 'id' + * in: 'path' + * required: true + * schema: + * type: 'string' + * description: '삭제할 채널의 고유 ID' + * responses: + * 200: + * description: '채널 삭제 성공' + * 404: + * description: '채널을 찾을 수 없음' + * 500: + * description: '서버 오류' + */ +channelRouter.delete( + '/:id', + [param('id').notEmpty().withMessage('Channel ID is required')], + authenticateJWT, + validationMiddleware, + deleteChannelController, +); diff --git a/backend/src/services/channelService.js b/backend/src/services/channelService.js index d6f1a2c0..e7b05fe4 100644 --- a/backend/src/services/channelService.js +++ b/backend/src/services/channelService.js @@ -1,5 +1,6 @@ import { createChannelInDB, + deleteChannelByIdFromDB, getChannelInfoByIdInDB, getChannelsByUserIdFromDB, getChannelWithGuestsByIdFromDB, @@ -105,3 +106,16 @@ export const getUserChannels = async userId => { throw new Error('Failed to fetch channels', error); } }; + +/** + * @description 채널 삭제 서비스 + * @param {string} id - 삭제할 채널의 ID + * @returns {boolean} 삭제 성공 여부 + */ +export const deleteChannelService = async id => { + try { + return await deleteChannelByIdFromDB(id); + } catch (error) { + throw new Error('Failed to delete channel', error); + } +};