From 309010e61b47bba4c96061b789dbcdbea3e8d0a7 Mon Sep 17 00:00:00 2001
From: LeeWxx <dnckd0903@naver.com>
Date: Sat, 7 Dec 2024 22:58:32 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20pc=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?=
 =?UTF-8?q?=EA=B2=BD=EA=B8=B0=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                 | 18 +++++++++++++++
 src/services/myService.js        | 39 ++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/src/repositories/myRepository.js b/src/repositories/myRepository.js
index 983814b..b9996fb 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 bcf9439..0fe315b 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 7fb02af..b7b7319 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;
-- 
GitLab