-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
724ebec
commit 9b6f11a
Showing
3 changed files
with
85 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
backend/emm-sale/src/main/java/com/emmsale/event/domain/Events.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |