Skip to content

Commit

Permalink
refactor: 모임원 답변 리스트 저장 메서드 분리 (리뷰 반영)
Browse files Browse the repository at this point in the history
  • Loading branch information
nahyeon99 committed Aug 7, 2024
1 parent 4c73d74 commit 159233c
Showing 1 changed file with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,36 @@ public class MeetingAnswerService {
public void save(Long userId, Long meetingId, Long meetingQuestionId, MeetingAnswerRequest request) {
MeetingMember loginMember = meetingMemberService.getByUserIdAndMeetingId(userId, meetingId);
MeetingQuestion meetingQuestion = meetingQuestionService.getById(meetingId, meetingQuestionId);
Long questionId = meetingQuestion.getQuestion().getId();

meetingQuestion.validateNotFinished(LocalDateTime.now(clock));
validateNonDuplicateMeetingAnswer(meetingQuestion.getId(), loginMember.getId());
meetingQuestion.validateMeetingAnswerCount(request.answerIds().size());

List<Long> answerIds = request.answerIds();
saveMeetingAnswers(meetingQuestion, loginMember, request.answerIds());

eventService.inactivateLastEventByType(userId, meetingId, QUESTION_REGISTERED);
advanceToNextQuestionIfAllAnswered(meetingId, meetingQuestion);
}

public MyMeetingAnswerListResponse getMyList(Long userId, Long meetingId) {
MeetingMember loginMember = meetingMemberService.getByUserIdAndMeetingId(userId, meetingId);
return meetingAnswerRepository.findAllByMeetingMemberId(loginMember.getId());
}

private void saveMeetingAnswers(MeetingQuestion meetingQuestion, MeetingMember loginMember, List<Long> answerIds) {
Long questionId = meetingQuestion.getQuestion().getId();
List<MeetingAnswer> meetingAnswers = answerIds.stream()
.map(answerId -> answerService.getById(questionId, answerId))
.map(answer -> new MeetingAnswer(meetingQuestion, answer, loginMember))
.toList();
meetingAnswers.forEach(meetingAnswerRepository::save);
}

eventService.inactivateLastEventByType(userId, meetingId, QUESTION_REGISTERED);
advanceToNextQuestionIfAllAnswered(meetingId, meetingQuestion);
private void validateNonDuplicateMeetingAnswer(Long meetingQuestionId, Long meetingMemberId) {
boolean exists = meetingAnswerRepository.existsByMeetingMember(meetingQuestionId, meetingMemberId);
if (exists) {
throw new DuplicateMeetingAnswerException();
}
}

private void advanceToNextQuestionIfAllAnswered(Long meetingId, MeetingQuestion currentQuestion) {
Expand All @@ -72,16 +87,4 @@ private void advanceToNextQuestionIfAllAnswered(Long meetingId, MeetingQuestion
});
}
}

public MyMeetingAnswerListResponse getMyList(Long userId, Long meetingId) {
MeetingMember loginMember = meetingMemberService.getByUserIdAndMeetingId(userId, meetingId);
return meetingAnswerRepository.findAllByMeetingMemberId(loginMember.getId());
}

private void validateNonDuplicateMeetingAnswer(Long meetingQuestionId, Long meetingMemberId) {
boolean exists = meetingAnswerRepository.existsByMeetingMember(meetingQuestionId, meetingMemberId);
if (exists) {
throw new DuplicateMeetingAnswerException();
}
}
}

0 comments on commit 159233c

Please sign in to comment.