Skip to content

Commit

Permalink
[BE][Feat] #415 : 채널 삭제 api 구현
Browse files Browse the repository at this point in the history
- 채널 삭제 api 구현
- swagger 적용
  • Loading branch information
happyhyep committed Dec 4, 2024
1 parent 55c9375 commit 34111fb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
23 changes: 23 additions & 0 deletions backend/src/controllers/channelController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addGuestService,
createChannelService,
deleteChannelService,
getChannelByIdService,
getChannelGuestInfoService,
getUserChannels,
Expand Down Expand Up @@ -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' }));
}
};
14 changes: 14 additions & 0 deletions backend/src/repositories/channelRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
33 changes: 33 additions & 0 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { body, param } from 'express-validator';
import {
addGuestController,
createChannelController,
deleteChannelController,
getChannelGuestInfoController,
getChannelInfoController,
getUserChannelsController,
Expand Down Expand Up @@ -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,
);
14 changes: 14 additions & 0 deletions backend/src/services/channelService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
createChannelInDB,
deleteChannelByIdFromDB,
getChannelInfoByIdInDB,
getChannelsByUserIdFromDB,
getChannelWithGuestsByIdFromDB,
Expand Down Expand Up @@ -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);
}
};

0 comments on commit 34111fb

Please sign in to comment.