From d66c6b6c369b4eec8cf8015870a34572188a1dc8 Mon Sep 17 00:00:00 2001 From: SHEOMM Date: Mon, 1 Apr 2024 20:01:28 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20API=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AttendanceController.java | 3 +- .../attendee/service/AttendeeServiceImpl.java | 47 ++++++++----------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/waruru/areyouhere/attendance/controller/AttendanceController.java b/src/main/java/com/waruru/areyouhere/attendance/controller/AttendanceController.java index ba87676..e22a10b 100644 --- a/src/main/java/com/waruru/areyouhere/attendance/controller/AttendanceController.java +++ b/src/main/java/com/waruru/areyouhere/attendance/controller/AttendanceController.java @@ -46,7 +46,8 @@ public class AttendanceController { private final String COOKIE_ENCODE = "SRCT"; - //FIXME: 중복 인원인 경우 코드 중복이 생겨도 API 분리가 나아 보인다. 한 API에서 너무 많은 일을 하는 중. + //FIXME: 중복 인원인 경우 코드 중복이 생겨도 API 분리가 나아 보인다. 한 API에서 너무 많은 일을 하는 중이다. + // FIXME: 사실상 다른 형태의 response를 억지로 하나의 controller에서 반환한다고 봐도 무방하다. -> 프론트와 합의 후 변경. @PostMapping public ResponseEntity attend(HttpServletRequest request, @RequestBody AttendRequestDto attendRequestDto){ String attendeeName = attendRequestDto.getAttendeeName(); 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 8a19406..2c80a87 100644 --- a/src/main/java/com/waruru/areyouhere/attendee/service/AttendeeServiceImpl.java +++ b/src/main/java/com/waruru/areyouhere/attendee/service/AttendeeServiceImpl.java @@ -18,6 +18,7 @@ import com.waruru.areyouhere.course.domain.repository.CourseRepository; import com.waruru.areyouhere.attendee.exception.AttendeesNotUniqueException; import com.waruru.areyouhere.course.exception.CourseNotFoundException; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -27,7 +28,6 @@ import java.util.function.Function; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -37,7 +37,6 @@ @Service @RequiredArgsConstructor -@Slf4j @Transactional public class AttendeeServiceImpl implements AttendeeService{ @@ -50,7 +49,7 @@ public void createAll(Long courseId, List newAttendees){ Course course = courseRepository.findById(courseId) .orElseThrow(CourseNotFoundException::new); - throwIfNameAndNoteNotUnique(newAttendees, courseId); + throwIfAttendeesNameAndNoteNotUnique(newAttendees, courseId); List attendeeToUpdate = new LinkedList<>(); List attendeesToSave = new LinkedList<>(); @@ -205,7 +204,7 @@ public AttendeeDetailDto getAttendanceCount(Long attendeeId){ @Override public void updateAll(Long courseId, List updatedAttendees){ - throwIfNameAndNoteNotUnique(updatedAttendees, courseId); + throwIfAttendeesNameAndNoteNotUnique(updatedAttendees, courseId); updatedAttendees.stream() .map(attendee -> @@ -219,15 +218,15 @@ public void updateAll(Long courseId, List updatedAttendees){ } - // Name에 index가 없다면 위에 비해 그리 빠를 지 모르겠다. - private void throwIfNameAndNoteNotUnique(List newAttendees, Long courseId) { - List newAttendeeNames = getAttendeeNames(newAttendees); + // TODO: Name에 index가 없다면 위에 비해 그리 빠를 지 모르겠다. + // FIXME: Map> 같은 것 대신 그냥 class 선언해서 쓰는게 + private void throwIfAttendeesNameAndNoteNotUnique(List newAttendees, Long courseId) { + List newAttendeeNames = getAttendeeUniqueNames(newAttendees); Map existingAttendees = getExistingAttendeesMap(courseId, newAttendeeNames); - Set uniqueAttendees = checkNewAttendees(newAttendees, existingAttendees); - checkExistingAttendees(existingAttendees, uniqueAttendees); + checkNameSake(newAttendees, existingAttendees); } - private List getAttendeeNames(List attendees) { + private List getAttendeeUniqueNames(List attendees) { return attendees.stream() .map(AttendeeInfo::getName) .distinct() @@ -240,43 +239,37 @@ private Map getExistingAttendeesMap(Long courseId, List .collect(Collectors.toMap(Attendee::getId, Function.identity())); } - private Set checkNewAttendees(List newAttendees, Map existingAttendees) { - Set uniqueAttendees = new HashSet<>(); + private void checkNameSake(List newAttendees, Map existingAttendees) { + Map> uniqueAttendees = new HashMap<>(); + + newAttendees.forEach(attendeeInfo -> uniqueAttendees.put(attendeeInfo.getName(), new HashSet<>())); + newAttendees.forEach(attendeeInfo -> { - String key = concatNameAndNote(attendeeInfo); if (attendeeInfo.getId() != null) { - existingAttendees.computeIfAbsent(attendeeInfo.getId(), id -> { throw new IllegalArgumentException("Attendee not found"); }).update(attendeeInfo.getName(), attendeeInfo.getNote()); - } else if (!uniqueAttendees.add(key)) { + } else if (!checkOneAttendeeUnique(attendeeInfo, uniqueAttendees)) { throw new AttendeesNotUniqueException(); } }); - return uniqueAttendees; - } - private void checkExistingAttendees(Map existingAttendees, Set uniqueAttendees) { existingAttendees.values() .stream() - .filter(attendee -> !uniqueAttendees.add(concatNameAndNote(attendee))) + .filter(attendee -> !checkOneAttendeeUnique(attendee, uniqueAttendees)) .findAny() .ifPresent(attendee -> { throw new AttendeesNotUniqueException(); }); } - private String concatNameAndNote(AttendeeInfo attendeeInfo) { - return attendeeInfo.getName() + Optional.ofNullable(attendeeInfo.getNote()).orElse(""); + private boolean checkOneAttendeeUnique(AttendeeInfo attendeeInfo, Map> uniqueAttendees) { + return uniqueAttendees.get(attendeeInfo.getName()).add(Optional.ofNullable(attendeeInfo.getNote()).orElse("")); } - private String concatNameAndNote(Attendee attendee) { - return attendee.getName() + Optional.ofNullable(attendee.getNote()).orElse(""); + private boolean checkOneAttendeeUnique(Attendee attendee, Map> uniqueAttendees) { + return uniqueAttendees.get(attendee.getName()).add(Optional.ofNullable(attendee.getNote()).orElse("")); } - - - - }