From a71d1779582b5f0cad1e90a4986296318f5356d5 Mon Sep 17 00:00:00 2001 From: LeeWxx <dnckd0903@naver.com> Date: Sun, 8 Dec 2024 15:33:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20pc=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repositories/myRepository.js | 12 ++++++++++++ src/routes/my.js | 25 +++++++++++++++++++++++++ src/services/myService.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/repositories/myRepository.js b/src/repositories/myRepository.js index d4af715..213a49f 100644 --- a/src/repositories/myRepository.js +++ b/src/repositories/myRepository.js @@ -81,6 +81,18 @@ const myRepository = { const { rowCount } = await pool.query(query, values); return rowCount > 0; }, + + async deleteCombinationById(combinationId) { + const query = ` + UPDATE combinations + SET owner_id = NULL + WHERE id = $1 + `; + const values = [combinationId]; + + const { rowCount } = await pool.query(query, values); + return rowCount > 0; +} }; export default myRepository; diff --git a/src/routes/my.js b/src/routes/my.js index ef5e99e..aa98c7a 100644 --- a/src/routes/my.js +++ b/src/routes/my.js @@ -21,6 +21,31 @@ myRouter.get( }) ); +myRouter.delete( + '/pc/:pcId', + authMiddleware, + wrapAsync(async (req, res) => { + const { pcId } = req.params; + const userId = req.user?.userId; + + if (!userId) { + throw new ReportableError(400, '유효한 사용자 정보가 없습니다.'); + } + + if (!pcId) { + throw new ReportableError(400, 'PC ID가 필요합니다.'); + } + + const result = await myService.deleteUserPC(userId, pcId); + + if (result.success) { + return res.sendResponse(result.message, 200, {}); + } else { + throw new ReportableError(500, 'PC 삭제 중 문제가 발생했습니다.'); + } + }) +); + myRouter.patch( '/pc/:combinationId/name', authMiddleware, diff --git a/src/services/myService.js b/src/services/myService.js index c8bee06..1295252 100644 --- a/src/services/myService.js +++ b/src/services/myService.js @@ -178,6 +178,36 @@ const myService = { return await ShareRepository.createCombinationUuid(combinationId); }, + + async deleteUserPC(userId, pcId) { + if (!userId || !pcId) { + throw new ReportableError(400, '유효한 사용자 ID 및 PC ID가 필요합니다.'); + } + + const userExists = await myRepository.checkUserExists(userId); + if (!userExists) { + throw new ReportableError(404, '사용자를 찾을 수 없습니다.'); + } + + const combination = await myRepository.getCombinationById(pcId); + if (!combination) { + throw new ReportableError(404, '해당 PC 조합을 찾을 수 없습니다.'); + } + + if (combination.userId !== userId) { + throw new ReportableError(403, '해당 PC 조합에 대한 권한이 없습니다.'); + } + + const deleted = await myRepository.deleteCombinationById(pcId); + if (!deleted) { + throw new ReportableError(500, 'PC 조합 삭제 중 문제가 발생했습니다.'); + } + + return { + success: true, + message: 'PC 조합이 성공적으로 삭제되었습니다.', + }; + }, }; export default myService; -- GitLab