Merge pull request #57 from hhu-propra2/GroupLogicProjection
Group logic projection
This commit is contained in:
@ -77,8 +77,4 @@ public class Group extends Aggregate {
|
||||
throw new UserNotFoundException("Nutzer wurde nicht gefunden!");
|
||||
}
|
||||
}
|
||||
|
||||
private void applyEvent(DeleteGroupEvent event) {
|
||||
this.id = this.id * -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,9 @@ import mops.gruppen2.domain.event.Event;
|
||||
import mops.gruppen2.repository.EventRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EventService {
|
||||
private final SerializationService serializationService;
|
||||
@ -48,4 +51,18 @@ public class EventService {
|
||||
}
|
||||
return tmpId;
|
||||
}
|
||||
|
||||
public List<Event> findAllEvents() {
|
||||
Iterable<EventDTO> eventDTOS = eventStore.findAll();
|
||||
List<Event> events = new ArrayList<>();
|
||||
eventDTOS.forEach(eventDTO -> {
|
||||
try {
|
||||
events.add(serializationService.deserializeEvent(eventDTO.getEvent_payload()));
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return events;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,49 +1,58 @@
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class GroupService {
|
||||
|
||||
/**
|
||||
* Konstruiert eine vollständige Gruppe aus Events, welche dieselbe Gruppe betreffen.
|
||||
*
|
||||
* @param eventList Die restlichen Events für diese Gruppe
|
||||
* @return Gruppe auf aktuellem Stand
|
||||
*/
|
||||
Group buildGroupFromEvents(List<Event> eventList) throws EventException {
|
||||
Group newGroup = new Group();
|
||||
private final EventService eventService;
|
||||
|
||||
try {
|
||||
if (!(eventList.get(0) instanceof CreateGroupEvent)) {
|
||||
throw new GroupDoesNotExistException("Die Gruppe existiert nicht");
|
||||
public GroupService(EventService eventService) {
|
||||
this.eventService = eventService;
|
||||
}
|
||||
|
||||
public List<Group> projectEventList(Map<Long, Group> groupMap, List<Event> events) throws EventException {
|
||||
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 {
|
||||
newGroup.applyEvent(eventList.get(0));
|
||||
eventList.remove(0);
|
||||
}
|
||||
for (Event event : eventList) {
|
||||
if (!(newGroup.getId() > 0)) {
|
||||
throw new GroupDoesNotExistException("Die Gruppe existiert nicht");
|
||||
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();
|
||||
}
|
||||
newGroup.applyEvent(event);
|
||||
}
|
||||
} catch (EventException e) {
|
||||
if (e instanceof GroupDoesNotExistException) {
|
||||
throw e;
|
||||
}
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
if (newGroup.getId() < 0) {
|
||||
return null;
|
||||
}
|
||||
return newGroup;
|
||||
return new ArrayList<>(groupMap.values());
|
||||
}
|
||||
|
||||
public List<Group> projectEventList(List<Event> events) throws EventException {
|
||||
return projectEventList(new HashMap<>(), events);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user