File tree Expand file tree Collapse file tree
main/java/com/dddheroes/heroesofddd/astrologers/write
test/java/com/dddheroes/heroesofddd/astrologers/write Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+ import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .OnlyOneSymbolPerWeek ;
4+ import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .ProclaimWeekSymbol ;
5+ import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .WeekSymbolProclaimed ;
6+ import org .axonframework .commandhandling .CommandHandler ;
7+ import org .axonframework .eventsourcing .EventSourcingHandler ;
8+ import org .axonframework .modelling .command .AggregateCreationPolicy ;
9+ import org .axonframework .modelling .command .AggregateIdentifier ;
10+ import org .axonframework .modelling .command .CreationPolicy ;
11+ import org .axonframework .spring .stereotype .Aggregate ;
12+
13+ import static org .axonframework .modelling .command .AggregateLifecycle .apply ;
14+
15+ @ Aggregate
16+ class Astrologers {
17+
18+ @ AggregateIdentifier
19+ private AstrologersId astrologersId ;
20+ private MonthWeek week ;
21+
22+ @ CommandHandler
23+ @ CreationPolicy (AggregateCreationPolicy .CREATE_IF_MISSING )
24+ void decide (ProclaimWeekSymbol command ) {
25+ new OnlyOneSymbolPerWeek (command , week ).verify ();
26+
27+ apply (
28+ WeekSymbolProclaimed .event (
29+ command .astrologersId (),
30+ command .week (),
31+ command .symbol ()
32+ )
33+ );
34+ }
35+
36+ @ EventSourcingHandler
37+ void evolve (WeekSymbolProclaimed event ) {
38+ this .astrologersId = new AstrologersId (event .astrologersId ());
39+ this .week = new MonthWeek (event .month (), event .day ());
40+ }
41+
42+ Astrologers () {
43+ // required by Axon
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+
4+ public interface AstrologersCommand {
5+
6+ AstrologersId astrologersId ();
7+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+ public interface AstrologersEvent {
4+
5+ String astrologersId ();
6+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+ import java .util .UUID ;
4+
5+ public record AstrologersId (String raw ) {
6+
7+ public AstrologersId {
8+ if (raw == null || raw .isBlank ()) {
9+ throw new IllegalArgumentException ("Astrologers id cannot be null or empty" );
10+ }
11+ }
12+
13+ public static AstrologersId of (String raw ) {
14+ return new AstrologersId (raw );
15+ }
16+
17+ public static AstrologersId random () {
18+ return new AstrologersId (UUID .randomUUID ().toString ());
19+ }
20+
21+ @ Override
22+ public String toString () {
23+ return raw ;
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+ public record MonthWeek (Integer month , Integer week ) {
4+
5+ public static MonthWeek of (Integer month , Integer week ) {
6+ // todo: validation
7+ return new MonthWeek (month , week );
8+ }
9+
10+ public int weekNumber () {
11+ return ((month - 1 ) * 4 ) + week ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+ import com .dddheroes .heroesofddd .shared .CreatureId ;
4+
5+ // todo: polymorphism, support symbols which are not creatures
6+ public record WeekSymbol (CreatureId weekOf , Integer growth ) {
7+
8+ public static WeekSymbol of (CreatureId weekOf , Integer growth ) {
9+ return new WeekSymbol (weekOf , growth );
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol ;
2+
3+ import com .dddheroes .heroesofddd .astrologers .write .MonthWeek ;
4+ import com .dddheroes .heroesofddd .calendar .write .Day ;
5+ import com .dddheroes .heroesofddd .calendar .write .Month ;
6+ import com .dddheroes .heroesofddd .calendar .write .Week ;
7+ import com .dddheroes .heroesofddd .calendar .write .finishday .FinishDay ;
8+ import com .dddheroes .heroesofddd .shared .DomainRule ;
9+
10+ public record OnlyOneSymbolPerWeek (
11+ ProclaimWeekSymbol command ,
12+ MonthWeek lastlyProclaimed
13+ ) implements DomainRule {
14+
15+ @ Override
16+ public boolean isViolated () {
17+ return lastlyProclaimed != null && lastlyProclaimed .weekNumber () >= command .week ().weekNumber ();
18+ }
19+
20+ @ Override
21+ public String message () {
22+ return "Only one symbol can be proclaimed per week" ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol ;
2+
3+ import com .dddheroes .heroesofddd .astrologers .write .AstrologersCommand ;
4+ import com .dddheroes .heroesofddd .astrologers .write .AstrologersId ;
5+ import com .dddheroes .heroesofddd .astrologers .write .MonthWeek ;
6+ import com .dddheroes .heroesofddd .astrologers .write .WeekSymbol ;
7+ import com .dddheroes .heroesofddd .shared .CreatureId ;
8+ import org .axonframework .modelling .command .TargetAggregateIdentifier ;
9+
10+ public record ProclaimWeekSymbol (
11+ @ TargetAggregateIdentifier
12+ AstrologersId astrologersId ,
13+ MonthWeek week ,
14+ WeekSymbol symbol
15+ ) implements AstrologersCommand {
16+
17+ public static ProclaimWeekSymbol command (
18+ String astrologersId ,
19+ Integer month ,
20+ Integer week ,
21+ String creatureId ,
22+ Integer growth
23+ ) {
24+ return new ProclaimWeekSymbol (
25+ AstrologersId .of (astrologersId ),
26+ MonthWeek .of (month , week ),
27+ WeekSymbol .of (CreatureId .of (creatureId ), growth )
28+ );
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol ;
2+
3+ import com .dddheroes .heroesofddd .astrologers .write .AstrologersEvent ;
4+ import com .dddheroes .heroesofddd .astrologers .write .AstrologersId ;
5+ import com .dddheroes .heroesofddd .astrologers .write .MonthWeek ;
6+ import com .dddheroes .heroesofddd .astrologers .write .WeekSymbol ;
7+
8+ public record WeekSymbolProclaimed (
9+ String astrologersId ,
10+ Integer month ,
11+ Integer day ,
12+ String weekOf ,
13+ Integer growth
14+ ) implements AstrologersEvent {
15+
16+ public static WeekSymbolProclaimed event (
17+ AstrologersId astrologersId ,
18+ MonthWeek monthWeek ,
19+ WeekSymbol symbol
20+ ) {
21+ return new WeekSymbolProclaimed (
22+ astrologersId .raw (),
23+ monthWeek .month (),
24+ monthWeek .week (),
25+ symbol .weekOf ().raw (),
26+ symbol .growth ()
27+ );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .dddheroes .heroesofddd .astrologers .write ;
2+
3+ import org .axonframework .test .aggregate .AggregateTestFixture ;
4+ import org .junit .jupiter .api .*;
5+
6+ public class AstrologersTest {
7+
8+ protected AggregateTestFixture <?> fixture ;
9+
10+ @ BeforeEach
11+ void setUp () {
12+ fixture = new AggregateTestFixture <>(Astrologers .class );
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments