Skip to content

Commit

Permalink
refactor : 일급 컬렉션 도입
Browse files Browse the repository at this point in the history
  • Loading branch information
java-saeng committed Dec 4, 2023
1 parent 724ebec commit 9b6f11a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -57,23 +54,19 @@ public List<EventResponse> findEvents(
) {
validateTags(request.getTags());

final Specification<Event> spec = Specification
.where(EventSpecification.filterByCategory(request.getCategory()))
.and(EventSpecification.filterByTags(request.getTags()))
.and(EventSpecification.filterByPeriod(startDate, endDate))
.and(EventSpecification.filterByNameContainsSearchKeywords(request.getKeyword()));

final List<Event> events = eventRepository.findAll(spec);
final Specification<Event> spec = specificationEvents(request, startDate, endDate);

final EnumMap<EventStatus, List<Event>> eventsForEventStatus
= groupByEventStatus(nowDateTime, events);
final Events events = new Events(eventRepository.findAll(spec));
final EnumMap<EventStatus, List<Event>> map = events.groupByEventStatus(
request.getStatuses(),
nowDateTime
);
final List<Long> eventIds = events.getEventIds();

final List<Long> 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<String> tagNames) {
Expand All @@ -89,44 +82,12 @@ private void validateExistTags(final List<String> tagNames, final List<Tag> tags
}
}

private EnumMap<EventStatus, List<Event>> groupByEventStatus(final LocalDateTime nowDateTime,
final List<Event> events) {
return events.stream()
.sorted(comparing(event -> event.getEventPeriod().getStartDate()))
.collect(
groupingBy(event -> event.calculateStatus(nowDateTime),
() -> new EnumMap<>(EventStatus.class), toList())
);
}

private List<EventResponse> filterByStatuses(
final List<EventStatus> statuses,
final EnumMap<EventStatus, List<Event>> eventsForEventStatus,
final Map<Long, AllImagesOfContent> imagesPerEventId
) {
if (isExistStatusName(statuses)) {
return filterEventResponseByStatuses(statuses, eventsForEventStatus, imagesPerEventId);
}
return EventResponse.mergeEventResponses(eventsForEventStatus, imagesPerEventId);
}

private boolean isExistStatusName(final List<EventStatus> statuses) {
return statuses != null && !statuses.isEmpty();
}

private List<EventResponse> filterEventResponseByStatuses(
final List<EventStatus> statuses,
final EnumMap<EventStatus, List<Event>> eventsForEventStatus,
final Map<Long, AllImagesOfContent> 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<Event> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,19 @@ public static EventResponse from(
);
}

public static List<EventResponse> makeEventResponsesByStatus(
public static List<EventResponse> mergeEventResponses(
final Map<EventStatus, List<Event>> groupByEventStatus,
final Map<Long, AllImagesOfContent> imagesPerEventId
) {
return groupByEventStatus.values().stream()
.map(events -> makeEventResponsesByStatus(events, imagesPerEventId))
.reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> {
combinedEvents.addAll(eventsToAdd);
return combinedEvents;
});
}

private static List<EventResponse> makeEventResponsesByStatus(
final List<Event> events,
final Map<Long, AllImagesOfContent> imagesPerEventId
) {
Expand All @@ -83,16 +95,4 @@ private static EventResponse toEventResponse(
final AllImagesOfContent allImageUrls = imagesPerEventId.get(event.getId());
return EventResponse.from(event, allImageUrls);
}

public static List<EventResponse> mergeEventResponses(
final Map<EventStatus, List<Event>> groupByEventStatus,
final Map<Long, AllImagesOfContent> imagesPerEventId
) {
return groupByEventStatus.values().stream()
.map(events -> makeEventResponsesByStatus(events, imagesPerEventId))
.reduce(new ArrayList<>(), (combinedEvents, eventsToAdd) -> {
combinedEvents.addAll(eventsToAdd);
return combinedEvents;
});
}
}
Original file line number Diff line number Diff line change
@@ -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<Event> events;

public Events(final List<Event> events) {
this.events = events;
}

public EnumMap<EventStatus, List<Event>> groupByEventStatus(
final List<EventStatus> 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<Event> isEventContainsStatus(
final List<EventStatus> eventStatuses,
final LocalDateTime nowDateTime
) {
return event -> {
if (eventStatuses == null || eventStatuses.isEmpty()) {
return true;
}
return eventStatuses.contains(event.calculateStatus(nowDateTime));
};
}

public List<Long> getEventIds() {
return events.stream()
.map(Event::getId)
.collect(Collectors.toUnmodifiableList());
}
}

0 comments on commit 9b6f11a

Please sign in to comment.