Skip to content

Commit

Permalink
✨ dynamically update conference sessions closes #14
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Mar 27, 2024
1 parent 2873d42 commit b378f33
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/main/java/swiss/fihlon/apus/service/ConferenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package swiss.fihlon.apus.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import swiss.fihlon.apus.conference.Session;

Expand All @@ -33,25 +34,43 @@
@Service
public final class ConferenceService {

private final List<Session> sessions;
private List<Session> sessions;

public ConferenceService() {
sessions = generateSampleData();
updateSessions();
}

@Scheduled(fixedRate = 300_000) // every five minutes
private void scheduler() {
updateSessions();
}

private void updateSessions() {
final var newSessions = generateSampleData().stream()
.sorted()
.toList();
synchronized (this) {
sessions = newSessions;
}
}

public List<Session> getAllSessions() {
synchronized (this) {
return List.copyOf(sessions);
}
}

public List<Session> getRunningSessions() {
final LocalDateTime now = LocalDateTime.now();
return sessions.stream()
return getAllSessions().stream()
.filter(session -> session.startDate().isBefore(now) && session.endDate().isAfter(now))
.sorted()
.toList();
}

public List<Session> getFutureSessions() {
final LocalDateTime now = LocalDateTime.now();
return sessions.stream()
return getAllSessions().stream()
.filter(session -> session.startDate().isAfter(now))
.sorted()
.toList();
}

Expand All @@ -70,6 +89,7 @@ public List<Session> getNextSessions() {
.toList();
}

@SuppressWarnings("java:S125")
private List<Session> generateSampleData() {
final int sampleDataSize = 100;
final int sampleSessionParallel = 15;
Expand Down

0 comments on commit b378f33

Please sign in to comment.