From bf7d3007f13df872c5558363917dff8b499938a6 Mon Sep 17 00:00:00 2001
From: Eunhak Lee <lee@enak.kr>
Date: Sun, 8 Dec 2024 02:06:48 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20=EB=82=B4=20PC=EC=9D=98=20=EA=B3=B5?=
 =?UTF-8?q?=EC=9C=A0=20uuid=20=EB=A7=8C=EB=93=A4=EA=B8=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/repositories/shareRepository.js | 30 +++++++++++++++++++++++++++++
 src/routes/my.js                    | 12 ++++++++++++
 src/services/myService.js           | 15 +++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 src/repositories/shareRepository.js

diff --git a/src/repositories/shareRepository.js b/src/repositories/shareRepository.js
new file mode 100644
index 0000000..eec751a
--- /dev/null
+++ b/src/repositories/shareRepository.js
@@ -0,0 +1,30 @@
+import pool from '../db.js';
+
+const ShareRepository = {
+  async createCombinationUuid(combinationId) {
+    const resp = await pool.query(
+      `INSERT INTO shares (combination_id) 
+      VALUES ($1)
+      RETURNING uuid;`,
+      [combinationId]
+    );
+    const [uuid] = resp.rows;
+    return uuid;
+  },
+  async getCombinationByUuid(uniqueId) {
+    const resp = await pool.query(
+      `SELECT
+        shares.combination_id,
+        array(SELECT relations.part_id FROM relations WHERE relations.combination_id = shares.combination_id) as parts,
+        shares.created_at,
+        (SELECT created_at FROM combinations WHERE combinations.id = shares.combination_id) as updated_at
+        FROM shares
+        WHERE uuid=$1;`,
+      [uniqueId]
+    );
+    const [row] = resp.rows;
+    return row;
+  },
+};
+
+export default ShareRepository;
diff --git a/src/routes/my.js b/src/routes/my.js
index f95edeb..ccd6fb2 100644
--- a/src/routes/my.js
+++ b/src/routes/my.js
@@ -43,6 +43,18 @@ myRouter.patch(
   })
 );
 
+myRouter.post(
+  '/combination/:combinationId/uuid',
+  authMiddleware,
+  wrapAsync(async (req, res) => {
+    const { combinationId } = req.params;
+    const userId = req.user?.userId;
+
+    const uuid = await myService.createCombinationUuid(userId, combinationId);
+    res.sendResponse('', 200, { uuid });
+  })
+);
+
 myRouter.get(
   '/registration-code',
   authMiddleware,
diff --git a/src/services/myService.js b/src/services/myService.js
index 9bfcc07..c8bee06 100644
--- a/src/services/myService.js
+++ b/src/services/myService.js
@@ -1,6 +1,7 @@
 import { Redis } from '../redis.js';
 import { ReportableError } from '../errors.js';
 import myRepository from '../repositories/myRepository.js';
+import ShareRepository from '../repositories/shareRepository.js';
 import crypto from 'crypto';
 
 const myService = {
@@ -163,6 +164,20 @@ const myService = {
       message: '조합 이름이 성공적으로 변경되었습니다.',
     };
   },
+
+  async createCombinationUuid(userId, combinationId) {
+    if (!userId || !combinationId)
+      throw new ReportableError(400, '올바르지 않은 요청입니다.');
+
+    const combination = await myRepository.getCombinationById(combinationId);
+    if (!combination)
+      throw new ReportableError(404, '조합을 찾을 수 없습니다.');
+
+    if (combination.userId !== userId)
+      throw new ReportableError(403, '해당 조합에 대한 권한이 없습니다.');
+
+    return await ShareRepository.createCombinationUuid(combinationId);
+  },
 };
 
 export default myService;
-- 
GitLab