diff --git a/src/main/java/com/waruru/areyouhere/attendee/service/AttendeeServiceImpl.java b/src/main/java/com/waruru/areyouhere/attendee/service/AttendeeServiceImpl.java index 5fd6029..508baa6 100644 --- a/src/main/java/com/waruru/areyouhere/attendee/service/AttendeeServiceImpl.java +++ b/src/main/java/com/waruru/areyouhere/attendee/service/AttendeeServiceImpl.java @@ -16,6 +16,7 @@ import com.waruru.areyouhere.course.domain.repository.CourseRepository; import com.waruru.areyouhere.attendee.exception.AttendeesNotUniqueException; import com.waruru.areyouhere.session.exception.CourseIdNotFoundException; +import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -46,17 +47,18 @@ public void createAttendees(Long courseId, List newAttendees){ attendeeData.getName() + (attendeeData.getNote() == null ? "" : attendeeData.getNote()) ).toList(); - if(!isAttendeesUnique(attendeeUniqueCheck)){ + if(!isAttendeesUnique(attendeeUniqueCheck, courseId)){ throw new AttendeesNotUniqueException("참여자 이름이 중복되었습니다."); } List attendees = newAttendees.stream() .map(newAttendee -> Attendee.builder() - .course(course) - .name(newAttendee.getName()) - .note(newAttendee.getNote()).build()) - .toList(); + .course(course) + .name(newAttendee.getName()) + .note(newAttendee.getNote()) + .build()) + .toList(); attendeeBatchRepository.insertAttendeesBatch(attendees); } @@ -78,6 +80,7 @@ public DuplicateAttendees getDuplicateAttendees(Long courseId, List newA }) .forEach(newAttendee -> duplicateAttendees.addDuplicateAttendee(null, newAttendee, null)); + duplicated.forEach(newAttendee -> duplicateAttendees.addDuplicateAttendee(null, newAttendee, null)); getAlreadyExistsAttendees(courseId, newAttendees, duplicateAttendees); @@ -159,9 +162,13 @@ public int getAttendeeByCourseId(Long courseId){ return attendeeRepository.findAttendeesByCourse_Id(courseId).size(); } - private boolean isAttendeesUnique(List attendees) { - Set uniqueAttendees = Set.copyOf(attendees); - return uniqueAttendees.size() == attendees.size(); + private boolean isAttendeesUnique(List attendees, Long courseId) { + Set uniqueAttendees = new HashSet<>(Set.copyOf(attendees)); + List attendeesByCourseId = attendeeRepository.findAttendeesByCourse_Id(courseId); + uniqueAttendees.addAll(attendeesByCourseId.stream().map(attendeeData -> + attendeeData.getName() + (attendeeData.getNote() == null ? "" : attendeeData.getNote())).toList()); + + return uniqueAttendees.size() == attendees.size() + attendeesByCourseId.size(); }