diff --git a/controllers/participant.js b/controllers/participant.js index a60d093e554e233fb9575c21142831fdb4b04d3d..5571823c1e3aa985e2e1e32507da339348730996 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 f02070d5003b6411e9cc136d0840d92a85d81763..08e3cab1fa1b5fa3e3b88c52a272f4100db8ac3f 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 9f68d11bb2e542aac4bf52e75410285aa3abda8a..06c4737ddcb9e57aaf2033d209acb76a4bfa0f51 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;