From f9fe2c5f5f11dbee1b3468cefe335d527d581eb9 Mon Sep 17 00:00:00 2001 From: Wo-ogie <siwall0105@gmail.com> Date: Fri, 24 Nov 2023 18:44:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20id,=20=EC=9D=B4=EB=A6=84=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=B0=B8=EA=B0=80=EC=9E=90=20=EB=8B=A8=EA=B1=B4=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=ED=95=98=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 | 42 +++++++++++++++++++++++++++++++++++++ errors/participantErrors.js | 6 ++++++ routes/participant.js | 6 ++++++ 3 files changed, 54 insertions(+) diff --git a/controllers/participant.js b/controllers/participant.js index a60d093..5571823 100644 --- a/controllers/participant.js +++ b/controllers/participant.js @@ -2,6 +2,7 @@ const bcrypt = require('bcrypt'); const { createPasswordIsNullError } = require('../errors/meetingErrors'); const { createParticipantIsAlreadyExistError, + createParticipantNotFoundError, } = require('../errors/participantErrors'); const ParticipantResponse = require('../dto/response/participantResponse'); const { Participant } = require('../models'); @@ -17,6 +18,18 @@ async function createParticipant(name, password, email, meetingId) { }); } +async function getParticipantById(participantId) { + const participant = await Participant.findOne({ + where: { + id: participantId, + }, + }); + if (!participant) { + throw createParticipantNotFoundError(); + } + return participant; +} + async function findParticipantByMeetingIdAndName(meetingId, name) { return Participant.findOne({ where: { @@ -26,6 +39,14 @@ async function findParticipantByMeetingIdAndName(meetingId, name) { }); } +async function getParticipantByMeetingIdAndName(meetingId, name) { + const participant = await findParticipantByMeetingIdAndName(meetingId, name); + if (!participant) { + throw createParticipantNotFoundError(); + } + return participant; +} + async function encryptPassword(password, next) { if (!password) { return next(createPasswordIsNullError()); @@ -69,6 +90,27 @@ exports.createParticipant = async (req, res, next) => { } }; +exports.getParticipantById = async (req, res, next) => { + try { + const participant = await getParticipantById(req.params.participantId); + return res.json(ParticipantResponse.from(participant)); + } catch (error) { + return next(error); + } +}; + +exports.getParticipantByName = async (req, res, next) => { + try { + const participant = await getParticipantByMeetingIdAndName( + req.params.meetingId, + req.query.name, + ); + return res.json(ParticipantResponse.from(participant)); + } catch (error) { + return next(error); + } +}; + exports.getParticipantExistence = async (req, res, next) => { try { const participant = await findParticipantByMeetingIdAndName( diff --git a/errors/participantErrors.js b/errors/participantErrors.js index f02070d..08e3cab 100644 --- a/errors/participantErrors.js +++ b/errors/participantErrors.js @@ -5,3 +5,9 @@ exports.createParticipantIsAlreadyExistError = () => { error.status = 409; return error; }; + +exports.createParticipantNotFoundError = () => { + const error = new Error('참가자 정보를 찾을 수 없습니다.'); + error.status = 404; + return error; +}; diff --git a/routes/participant.js b/routes/participant.js index 9f68d11..06c4737 100644 --- a/routes/participant.js +++ b/routes/participant.js @@ -1,6 +1,8 @@ const express = require('express'); const { createParticipant, + getParticipantByName, + getParticipantById, getParticipantExistence, } = require('../controllers/participant'); @@ -8,6 +10,10 @@ const router = express.Router(); router.post('/:meetingId/participants', createParticipant); +router.get('/:meetingId/participants', getParticipantByName); + +router.get('/:meetingId/participants/:participantId', getParticipantById); + router.get('/:meetingId/participants/existence', getParticipantExistence); module.exports = router; -- GitLab