Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package io.autoinvestor.application;

import io.autoinvestor.domain.events.Event;
import io.autoinvestor.domain.events.EventPublisher;
import io.autoinvestor.domain.events.EventStoreRepository;
import io.autoinvestor.domain.model.Decision;
import io.autoinvestor.domain.model.DecisionId;
import io.autoinvestor.domain.model.RiskLevel;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RegisterDecisionCommandHandler {

private final EventStoreRepository eventStore;
private final ReadModelRepository readModel;
private final EventPublisher eventPublisher;

public RegisterDecisionCommandHandler(EventStoreRepository eventStore, ReadModelRepository readModel) {
public RegisterDecisionCommandHandler(EventStoreRepository eventStore, ReadModelRepository readModel, EventPublisher eventPublisher) {
this.eventStore = eventStore;
this.readModel = readModel;
this.eventPublisher = eventPublisher;
}

public void handle(RegisterDecisionCommand command) {
Expand All @@ -37,6 +42,8 @@ public void handle(RegisterDecisionCommand command) {
risklevel
);

List<Event<?>> events = decision.getUncommittedEvents();

this.eventStore.save(decision);

DecisionDTO dto = new DecisionDTO(
Expand All @@ -48,6 +55,8 @@ public void handle(RegisterDecisionCommand command) {
);
this.readModel.save(dto);

this.eventPublisher.publish(events);

decision.markEventsAsCommitted();
}
}
Expand Down