diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventQueryService.java b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventQueryService.java index a06b7c7cb..56c595b5b 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventQueryService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventQueryService.java @@ -4,12 +4,12 @@ import static com.emmsale.tag.exception.TagExceptionType.NOT_FOUND_TAG; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.toList; import com.emmsale.event.application.dto.EventResponse; import com.emmsale.event.application.dto.EventSearchRequest; import com.emmsale.event.domain.Event; import com.emmsale.event.domain.EventStatus; +import com.emmsale.event.domain.Events; import com.emmsale.event.domain.repository.EventRepository; import com.emmsale.event.domain.repository.EventSpecification; import com.emmsale.event.exception.EventException; @@ -21,11 +21,8 @@ import com.emmsale.tag.exception.TagException; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.ArrayList; import java.util.EnumMap; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; @@ -57,23 +54,19 @@ public List findEvents( ) { validateTags(request.getTags()); - final Specification spec = Specification - .where(EventSpecification.filterByCategory(request.getCategory())) - .and(EventSpecification.filterByTags(request.getTags())) - .and(EventSpecification.filterByPeriod(startDate, endDate)) - .and(EventSpecification.filterByNameContainsSearchKeywords(request.getKeyword())); - - final List events = eventRepository.findAll(spec); + final Specification spec = specificationEvents(request, startDate, endDate); - final EnumMap> eventsForEventStatus - = groupByEventStatus(nowDateTime, events); + final Events events = new Events(eventRepository.findAll(spec)); + final EnumMap> map = events.groupByEventStatus( + request.getStatuses(), + nowDateTime + ); + final List eventIds = events.getEventIds(); - final List eventIds = events.stream() - .map(Event::getId) - .collect(Collectors.toUnmodifiableList()); - - return filterByStatuses(request.getStatuses(), eventsForEventStatus, - imageQueryService.findImagesPerContentId(ImageType.EVENT, eventIds)); + return EventResponse.mergeEventResponses( + map, + imageQueryService.findImagesPerContentId(ImageType.EVENT, eventIds) + ); } private void validateTags(final List tagNames) { @@ -89,44 +82,12 @@ private void validateExistTags(final List tagNames, final List tags } } - private EnumMap> groupByEventStatus(final LocalDateTime nowDateTime, - final List events) { - return events.stream() - .sorted(comparing(event -> event.getEventPeriod().getStartDate())) - .collect( - groupingBy(event -> event.calculateStatus(nowDateTime), - () -> new EnumMap<>(EventStatus.class), toList()) - ); - } - - private List filterByStatuses( - final List statuses, - final EnumMap> eventsForEventStatus, - final Map imagesPerEventId - ) { - if (isExistStatusName(statuses)) { - return filterEventResponseByStatuses(statuses, eventsForEventStatus, imagesPerEventId); - } - return EventResponse.mergeEventResponses(eventsForEventStatus, imagesPerEventId); - } - - private boolean isExistStatusName(final List statuses) { - return statuses != null && !statuses.isEmpty(); - } - - private List filterEventResponseByStatuses( - final List statuses, - final EnumMap> eventsForEventStatus, - final Map imagesPerEventId - ) { - return eventsForEventStatus.entrySet() - .stream() - .filter(entry -> statuses.contains(entry.getKey())) - .map(entry -> EventResponse.makeEventResponsesByStatus(entry.getValue(), - imagesPerEventId)) - .reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> { - combinedEvents.addAll(eventsToAdd); - return combinedEvents; - }); + private static Specification specificationEvents(final EventSearchRequest request, + final LocalDate startDate, final LocalDate endDate) { + return Specification + .where(EventSpecification.filterByCategory(request.getCategory())) + .and(EventSpecification.filterByTags(request.getTags())) + .and(EventSpecification.filterByPeriod(startDate, endDate)) + .and(EventSpecification.filterByNameContainsSearchKeywords(request.getKeyword())); } } diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/application/dto/EventResponse.java b/backend/emm-sale/src/main/java/com/emmsale/event/application/dto/EventResponse.java index d693d6970..a34202e2f 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event/application/dto/EventResponse.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event/application/dto/EventResponse.java @@ -67,7 +67,19 @@ public static EventResponse from( ); } - public static List makeEventResponsesByStatus( + public static List mergeEventResponses( + final Map> groupByEventStatus, + final Map imagesPerEventId + ) { + return groupByEventStatus.values().stream() + .map(events -> makeEventResponsesByStatus(events, imagesPerEventId)) + .reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> { + combinedEvents.addAll(eventsToAdd); + return combinedEvents; + }); + } + + private static List makeEventResponsesByStatus( final List events, final Map imagesPerEventId ) { @@ -83,16 +95,4 @@ private static EventResponse toEventResponse( final AllImagesOfContent allImageUrls = imagesPerEventId.get(event.getId()); return EventResponse.from(event, allImageUrls); } - - public static List mergeEventResponses( - final Map> groupByEventStatus, - final Map imagesPerEventId - ) { - return groupByEventStatus.values().stream() - .map(events -> makeEventResponsesByStatus(events, imagesPerEventId)) - .reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> { - combinedEvents.addAll(eventsToAdd); - return combinedEvents; - }); - } } diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/domain/Events.java b/backend/emm-sale/src/main/java/com/emmsale/event/domain/Events.java new file mode 100644 index 000000000..b2f532664 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/event/domain/Events.java @@ -0,0 +1,53 @@ +package com.emmsale.event.domain; + +import static java.util.Comparator.comparing; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.toList; + +import java.time.LocalDateTime; +import java.util.EnumMap; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import lombok.Getter; + +@Getter +public class Events { + + private final List events; + + public Events(final List events) { + this.events = events; + } + + public EnumMap> groupByEventStatus( + final List eventStatuses, + final LocalDateTime nowDateTime + ) { + return events.stream() + .filter(isEventContainsStatus(eventStatuses, nowDateTime)) + .sorted(comparing(event -> event.getEventPeriod().getStartDate())) + .collect( + groupingBy(event -> event.calculateStatus(nowDateTime), + () -> new EnumMap<>(EventStatus.class), toList()) + ); + } + + private Predicate isEventContainsStatus( + final List eventStatuses, + final LocalDateTime nowDateTime + ) { + return event -> { + if (eventStatuses == null || eventStatuses.isEmpty()) { + return true; + } + return eventStatuses.contains(event.calculateStatus(nowDateTime)); + }; + } + + public List getEventIds() { + return events.stream() + .map(Event::getId) + .collect(Collectors.toUnmodifiableList()); + } +}