1

refactor projection method

This commit is contained in:
Christoph
2020-03-12 14:37:32 +01:00
parent 31b9456892
commit f64bb0f578

View File

@ -1,11 +1,7 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import lombok.EqualsAndHashCode;
import mops.gruppen2.domain.Exceptions.EventException; import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -23,36 +19,23 @@ public class GroupService {
this.eventService = eventService; this.eventService = eventService;
} }
public List<Group> projectEventList(Map<Long, Group> groupMap, List<Event> events) throws EventException {
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<Long, Group> groupMap = new HashMap<>();
for (Event event : events) { for (Event event : events) {
if (event instanceof CreateGroupEvent) { getOrCreateGroup(groupMap, event.getGroup_id()).applyEvent(event);
groupMap.put(event.getGroup_id(), new Group());
}
if (event instanceof DeleteGroupEvent) {
groupMap.remove(event.getGroup_id());
} else {
try {
Group group = groupMap.get(event.getGroup_id());
if (group == null) {
throw new GroupDoesNotExistException("Gruppe " + event.getGroup_id() + " existiert nicht");
}
group.applyEvent(event);
} catch (EventException e) {
if (e instanceof GroupDoesNotExistException) {
throw e;
}
e.printStackTrace();
}
}
} }
return new ArrayList<>(groupMap.values()); return new ArrayList<>(groupMap.values());
} }
public List<Group> projectEventList(List<Event> events) throws EventException { //
return projectEventList(new HashMap<>(), events); private Group getOrCreateGroup(Map<Long, Group> groups, long group_id) {
if (!groups.containsKey(group_id)) {
groups.put(group_id, new Group());
}
return groups.get(group_id);
} }
} }