1

Merge remote-tracking branch 'origin/performance-tests' into updateGroupsAPI

This commit is contained in:
LukasEttel
2020-03-12 14:41:39 +01:00
2 changed files with 98 additions and 47 deletions

View File

@ -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);
}
}