Skip to content
Snippets Groups Projects
Commit f9fe2c5f authored by Wo-ogie's avatar Wo-ogie
Browse files

feat: id, 이름으로 참가자 단건 조회하기 API 구현

parent a1666477
Branches
No related tags found
No related merge requests found
......@@ -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(
......
......@@ -5,3 +5,9 @@ exports.createParticipantIsAlreadyExistError = () => {
error.status = 409;
return error;
};
exports.createParticipantNotFoundError = () => {
const error = new Error('참가자 정보를 찾을 수 없습니다.');
error.status = 404;
return error;
};
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;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment