diff --git a/src/main/java/mops/gruppen2/infrastructure/GroupCache.java b/src/main/java/mops/gruppen2/infrastructure/GroupCache.java new file mode 100644 index 0000000..8b29962 --- /dev/null +++ b/src/main/java/mops/gruppen2/infrastructure/GroupCache.java @@ -0,0 +1,57 @@ +package mops.gruppen2.infrastructure; + +import lombok.RequiredArgsConstructor; +import mops.gruppen2.domain.model.group.Group; +import mops.gruppen2.domain.service.ProjectionService; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.annotation.Scope; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +@RequiredArgsConstructor +@Component +@Scope("singleton") +public class GroupCache { + + private final ProjectionService projectionService; + + private long version; + private boolean isValid; + private String principal; + + private Map groups; + + private List userGroups; + private List publics; + private List privates; + private List lectures; + + @EventListener(ApplicationReadyEvent.class) + public void init() { + groups = projectionService.projectAllGroups(); + + Group current; + UUID currentId; + for (Map.Entry entry : groups.entrySet()) { + current = entry.getValue(); + currentId = entry.getKey(); + + if (current.isMember(principal)) { + userGroups.add(currentId); + } + if (current.isPublic()) { + publics.add(currentId); + } + if (current.isPrivate()) { + privates.add(currentId); + } + if (current.isLecture()) { + lectures.add(currentId); + } + } + } +}