refactor projection method
This commit is contained in:
@ -1,11 +1,7 @@
|
||||
package mops.gruppen2.service;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import mops.gruppen2.domain.Exceptions.EventException;
|
||||
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
|
||||
import mops.gruppen2.domain.Group;
|
||||
import mops.gruppen2.domain.event.CreateGroupEvent;
|
||||
import mops.gruppen2.domain.event.DeleteGroupEvent;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -23,36 +19,23 @@ public class GroupService {
|
||||
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) {
|
||||
if (event instanceof CreateGroupEvent) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
getOrCreateGroup(groupMap, event.getGroup_id()).applyEvent(event);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user