diff --git a/src/repositories/shareRepository.js b/src/repositories/shareRepository.js new file mode 100644 index 0000000000000000000000000000000000000000..eec751ab77088881d537b6d8be49d963db2153de --- /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 f95edeb975ab5778db977e64c5eb84da44969078..ccd6fb2c65d2b6b2936525c18cf7c5f2f9c6f200 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 9bfcc07b5da973069235922d7b4aa66a04d4050c..c8bee06aa3a72722395314d5bdb40ce08c62ee70 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;