From a16664772af52aea20f9700d1ed6c779c6dfa138 Mon Sep 17 00:00:00 2001 From: Wo-ogie <siwall0105@gmail.com> Date: Fri, 24 Nov 2023 18:19:45 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90=20=EC=A1=B4?= =?UTF-8?q?=EC=9E=AC=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/participant.js | 66 ++++++++++++++++++++++++++----------- errors/participantErrors.js | 7 ++++ routes/participant.js | 7 +++- 3 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 errors/participantErrors.js diff --git a/controllers/participant.js b/controllers/participant.js index 55929a2..a60d093 100644 --- a/controllers/participant.js +++ b/controllers/participant.js @@ -1,10 +1,31 @@ const bcrypt = require('bcrypt'); -const { Participant } = require('../models'); const { createPasswordIsNullError } = require('../errors/meetingErrors'); +const { + createParticipantIsAlreadyExistError, +} = require('../errors/participantErrors'); const ParticipantResponse = require('../dto/response/participantResponse'); +const { Participant } = require('../models'); const HASHING_ROUND = 12; +async function createParticipant(name, password, email, meetingId) { + return Participant.create({ + name, + password, + email, + MeetingId: meetingId, + }); +} + +async function findParticipantByMeetingIdAndName(meetingId, name) { + return Participant.findOne({ + where: { + MeetingId: meetingId, + name, + }, + }); +} + async function encryptPassword(password, next) { if (!password) { return next(createPasswordIsNullError()); @@ -18,24 +39,17 @@ async function encryptPassword(password, next) { } exports.createParticipant = async (req, res, next) => { - console.log(req.params); const { meetingId } = req.params; const reqName = req.body.name; const reqPassword = req.body.password || null; const reqEmail = req.body.email || null; - console.log(meetingId); - console.log(reqName); - console.log(reqPassword); - console.log(reqEmail); try { - const existingParticipant = await Participant.findOne({ - where: { - MeetingId: meetingId, - name: reqName, - }, - }); + const existingParticipant = await findParticipantByMeetingIdAndName( + meetingId, + reqName, + ); if (existingParticipant) { - throw new Error(); + throw createParticipantIsAlreadyExistError; } let passwordEncrypted = null; @@ -43,14 +57,28 @@ exports.createParticipant = async (req, res, next) => { passwordEncrypted = await encryptPassword(reqPassword, next); } - const participantCreated = await Participant.create({ - name: reqName, - password: passwordEncrypted, - email: reqEmail, - MeetingId: meetingId, - }); + const participantCreated = await createParticipant( + reqName, + passwordEncrypted, + reqEmail, + meetingId, + ); return res.status(201).json(ParticipantResponse.from(participantCreated)); } catch (error) { return next(error); } }; + +exports.getParticipantExistence = async (req, res, next) => { + try { + const participant = await findParticipantByMeetingIdAndName( + req.params.meetingId, + req.query.name, + ); + return res.json({ + exist: !!participant, + }); + } catch (error) { + return next(error); + } +}; diff --git a/errors/participantErrors.js b/errors/participantErrors.js new file mode 100644 index 0000000..f02070d --- /dev/null +++ b/errors/participantErrors.js @@ -0,0 +1,7 @@ +exports.createParticipantIsAlreadyExistError = () => { + const error = new Error( + '이미 존재하는 참가자입니다. 같은 이름의 참가자를 중복 생성할 수 없습니다.', + ); + error.status = 409; + return error; +}; diff --git a/routes/participant.js b/routes/participant.js index 6d8787e..9f68d11 100644 --- a/routes/participant.js +++ b/routes/participant.js @@ -1,8 +1,13 @@ const express = require('express'); -const { createParticipant } = require('../controllers/participant'); +const { + createParticipant, + getParticipantExistence, +} = require('../controllers/participant'); const router = express.Router(); router.post('/:meetingId/participants', createParticipant); +router.get('/:meetingId/participants/existence', getParticipantExistence); + module.exports = router; -- GitLab