diff --git a/src/repositories/myRepository.js b/src/repositories/myRepository.js index 983814b1ab17a37e0cccaa5eb74fec134cac47ff..b9996fb2370f938acae08aced61476f40c9c77e0 100644 --- a/src/repositories/myRepository.js +++ b/src/repositories/myRepository.js @@ -45,6 +45,18 @@ const myRepository = { return rows.length > 0 ? rows[0].combination_id : null; }, + + async updateCombinationName(combinationId, newName) { + const query = ` + UPDATE combinations + SET name = $1, updated_at = NOW() + WHERE id = $2 + `; + const values = [newName, 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 bcf9439c6dc103141e348f811fb12000804fd1e6..0fe315b19ae431d9d0bca0ae00bf3f8d8b296b3e 100644 --- a/src/routes/my.js +++ b/src/routes/my.js @@ -21,6 +21,24 @@ myRouter.get( }) ); +myRouter.patch( + '/pc/:combinationId/name', + authMiddleware, + wrapAsync(async (req, res) => { + const { pcId } = req.params; + const { newName } = req.body; + const userId = req.user.id; + + if (!newName || typeof newName !== 'string') { + throw new ReportableError(400, '유효한 새로운 이름이 필요합니다.'); + } + + const result = await myService.updateCombinationName(userId, pcId, newName); + + return res.status(200).json(result); + }) +); + myRouter.get( '/registration-code', authMiddleware, diff --git a/src/services/myService.js b/src/services/myService.js index 7fb02afe57e0b1c453b23b3e7c4a4ea216ea1e2a..b7b7319be1704a1f76fd9ce409a6aecbeb34f828 100644 --- a/src/services/myService.js +++ b/src/services/myService.js @@ -124,6 +124,45 @@ const myService = { return pcs; }, + + async updateCombinationName(userId, combinationId, newName) { + if (!userId || !combinationId || !newName) { + throw new ReportableError( + 400, + '유효한 사용자 ID, 조합 ID, 및 새로운 이름이 필요합니다.' + ); + } + + const userExists = await myRepository.checkUserExists(userId); + if (!userExists) { + throw new ReportableError(404, '사용자를 찾을 수 없습니다.'); + } + + const combination = await myRepository.getCombinationById(combinationId); + if (!combination) { + throw new ReportableError(404, '조합을 찾을 수 없습니다.'); + } + + if (combination.userId !== userId) { + throw new ReportableError(403, '해당 조합에 대한 권한이 없습니다.'); + } + + const updated = await myRepository.updateCombinationName( + combinationId, + newName + ); + if (!updated) { + throw new ReportableError( + 500, + '조합 이름을 업데이트하는 중 문제가 발생했습니다.' + ); + } + + return { + success: true, + message: '조합 이름이 성공적으로 변경되었습니다.', + }; + }, }; export default myService;