|
| 1 | +package daggerok.domain; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 4 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 5 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 6 | +import daggerok.eventstore.events.CounterCreated; |
| 7 | +import daggerok.eventstore.events.CounterIncremented; |
| 8 | +import daggerok.eventstore.events.CounterSuspended; |
| 9 | +import daggerok.eventstore.events.DomainEvent; |
| 10 | +import io.vavr.API; |
| 11 | +import io.vavr.collection.List; |
| 12 | +import lombok.Getter; |
| 13 | +import lombok.ToString; |
| 14 | +import lombok.extern.log4j.Log4j2; |
| 15 | + |
| 16 | +import java.time.ZonedDateTime; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import static io.vavr.API.$; |
| 24 | +import static io.vavr.API.Case; |
| 25 | +import static io.vavr.Predicates.instanceOf; |
| 26 | + |
| 27 | +@Getter |
| 28 | +@Log4j2 |
| 29 | +@ToString |
| 30 | +public class Counter implements Function<DomainEvent, Counter> { |
| 31 | + |
| 32 | + @JsonIgnore |
| 33 | + private final Collection<DomainEvent> eventStream = new CopyOnWriteArrayList<>(); |
| 34 | + |
| 35 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 36 | + private UUID aggregateId; |
| 37 | + |
| 38 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 39 | + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") |
| 40 | + private ZonedDateTime createdAt; |
| 41 | + |
| 42 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 43 | + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") |
| 44 | + private ZonedDateTime modifiedAt; |
| 45 | + |
| 46 | + @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| 47 | + private String name; |
| 48 | + |
| 49 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 50 | + private Long counter; |
| 51 | + |
| 52 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 53 | + private boolean suspended; |
| 54 | + |
| 55 | + public Counter() { |
| 56 | + this(null, null, null, null, null, true); |
| 57 | + } |
| 58 | + |
| 59 | + public Counter(UUID aggregateId, |
| 60 | + ZonedDateTime createdAt, |
| 61 | + ZonedDateTime modifiedAt, |
| 62 | + String name, |
| 63 | + Long counter, |
| 64 | + boolean suspended) { |
| 65 | + |
| 66 | + this.aggregateId = aggregateId; |
| 67 | + this.createdAt = createdAt; |
| 68 | + this.modifiedAt = modifiedAt; |
| 69 | + this.name = name; |
| 70 | + this.counter = counter; |
| 71 | + this.suspended = suspended; |
| 72 | + } |
| 73 | + |
| 74 | + /* commands */ |
| 75 | + |
| 76 | + public void create(UUID id, String counterName) { |
| 77 | + if (Objects.nonNull(this.aggregateId)) |
| 78 | + throw new IllegalStateException("current aggregate already initialized with some aggregateId"); |
| 79 | + if (Objects.nonNull(this.name)) |
| 80 | + throw new IllegalStateException("current aggregate already initialized with some name"); |
| 81 | + if (Objects.isNull(id)) throw new IllegalStateException("id may not be null"); |
| 82 | + if (Objects.isNull(counterName)) throw new IllegalStateException("counterName may not be null"); |
| 83 | + on(new CounterCreated(id, counterName, ZonedDateTime.now())); |
| 84 | + } |
| 85 | + |
| 86 | + public void increment(String byWhom, Long withValue) { |
| 87 | + if (Objects.isNull(this.aggregateId)) throw new IllegalStateException("counter has not been created."); |
| 88 | + if (withValue < 1) throw new IllegalStateException("counter can be incremented only with positive numbers."); |
| 89 | + on(new CounterIncremented(aggregateId, byWhom, withValue, ZonedDateTime.now())); |
| 90 | + } |
| 91 | + |
| 92 | + public void suspend(String byWhom, String reason) { |
| 93 | + if (Objects.isNull(this.aggregateId)) |
| 94 | + throw new IllegalStateException("counter has not been created."); |
| 95 | + on(new CounterSuspended(aggregateId, byWhom, reason, ZonedDateTime.now())); |
| 96 | + } |
| 97 | + |
| 98 | + /* event sourcing */ |
| 99 | + |
| 100 | + public static Counter rebuild(Counter snapshot, Collection<DomainEvent> domainEvents) { |
| 101 | + Counter counter = List.ofAll(domainEvents) |
| 102 | + .foldLeft(snapshot, Counter::apply); |
| 103 | + counter.eventStream.clear(); |
| 104 | + return counter; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Counter apply(DomainEvent domainEvent) { |
| 109 | + return API.Match(domainEvent).of( |
| 110 | + Case($(instanceOf(CounterCreated.class)), this::on), |
| 111 | + Case($(instanceOf(CounterIncremented.class)), this::on), |
| 112 | + Case($(instanceOf(CounterSuspended.class)), this::on), |
| 113 | + Case($(), this::onFallback) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /* events */ |
| 118 | + |
| 119 | + private Counter on(CounterCreated event) { |
| 120 | + eventStream.add(event); |
| 121 | + aggregateId = event.getAggregateId(); |
| 122 | + createdAt = modifiedAt = event.getAt(); |
| 123 | + name = event.getCounterName(); |
| 124 | + counter = 0L; |
| 125 | + suspended = false; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + private Counter on(CounterIncremented event) { |
| 130 | + eventStream.add(event); |
| 131 | + counter += event.getWithValue(); |
| 132 | + modifiedAt = event.getAt(); |
| 133 | + log.debug("{} counter incremented by {} with {}", name, event.getBy(), event.getWithValue()); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + private Counter on(CounterSuspended event) { |
| 138 | + eventStream.add(event); |
| 139 | + suspended = true; |
| 140 | + modifiedAt = event.getAt(); |
| 141 | + log.debug("{} counter suspended by {} with reason: {}", name, event.getBy(), event.getReason()); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + private <EVENT extends DomainEvent> Counter onFallback(EVENT event) { |
| 146 | + log.warn("unexpected event occurred: {}", event); |
| 147 | + return this; |
| 148 | + } |
| 149 | +} |
0 commit comments