Skip to content

Commit 6ce3ae1

Browse files
✨ feat: Resources Pool | write slices: DepositResources and WithdrawResources (#13)
+ `Cost` value object renamed to `Resources`
1 parent be017c6 commit 6ce3ae1

19 files changed

Lines changed: 389 additions & 45 deletions

File tree

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/Dwelling.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import com.dddheroes.heroesofddd.creaturerecruitment.write.recruitcreature.RecruitCreature;
1111
import com.dddheroes.heroesofddd.creaturerecruitment.write.recruitcreature.RecruitCreaturesNotExceedAvailableCreatures;
1212
import com.dddheroes.heroesofddd.shared.Amount;
13-
import com.dddheroes.heroesofddd.shared.Cost;
13+
import com.dddheroes.heroesofddd.shared.Resources;
1414
import com.dddheroes.heroesofddd.shared.CreatureId;
15-
import com.dddheroes.heroesofddd.shared.DomainRule;
1615
import org.axonframework.commandhandling.CommandHandler;
1716
import org.axonframework.eventsourcing.EventSourcingHandler;
1817
import org.axonframework.modelling.command.AggregateCreationPolicy;
@@ -28,7 +27,7 @@ class Dwelling {
2827
@AggregateIdentifier
2928
private DwellingId dwellingId;
3029
private CreatureId creatureId;
31-
private Cost costPerTroop;
30+
private Resources costPerTroop;
3231
private Amount availableCreatures;
3332

3433
@CommandHandler
@@ -49,7 +48,7 @@ void decide(BuildDwelling command) {
4948
void evolve(DwellingBuilt event) {
5049
this.dwellingId = new DwellingId(event.dwellingId());
5150
this.creatureId = new CreatureId(event.creatureId());
52-
this.costPerTroop = Cost.fromRaw(event.costPerTroop());
51+
this.costPerTroop = Resources.fromRaw(event.costPerTroop());
5352
this.availableCreatures = Amount.zero();
5453
}
5554

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/builddwelling/BuildDwelling.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.dddheroes.heroesofddd.creaturerecruitment.write.DwellingCommand;
44
import com.dddheroes.heroesofddd.creaturerecruitment.write.DwellingId;
5-
import com.dddheroes.heroesofddd.shared.Cost;
5+
import com.dddheroes.heroesofddd.shared.Resources;
66
import com.dddheroes.heroesofddd.shared.CreatureId;
77
import org.axonframework.modelling.command.TargetAggregateIdentifier;
88

@@ -12,10 +12,10 @@ public record BuildDwelling(
1212
@TargetAggregateIdentifier
1313
DwellingId dwellingId,
1414
CreatureId creatureId,
15-
Cost costPerTroop
15+
Resources costPerTroop
1616
) implements DwellingCommand {
1717

1818
public static BuildDwelling command(String dwellingId, String creatureId, Map<String, Integer> costPerTroop) {
19-
return new BuildDwelling(DwellingId.of(dwellingId), CreatureId.of(creatureId), Cost.fromRaw(costPerTroop));
19+
return new BuildDwelling(DwellingId.of(dwellingId), CreatureId.of(creatureId), Resources.fromRaw(costPerTroop));
2020
}
2121
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/builddwelling/DwellingBuilt.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.dddheroes.heroesofddd.creaturerecruitment.write.DwellingEvent;
44
import com.dddheroes.heroesofddd.creaturerecruitment.write.DwellingId;
5-
import com.dddheroes.heroesofddd.shared.Cost;
5+
import com.dddheroes.heroesofddd.shared.Resources;
66
import com.dddheroes.heroesofddd.shared.CreatureId;
77

88
import java.util.Map;
@@ -13,7 +13,7 @@ public record DwellingBuilt(
1313
Map<String, Integer> costPerTroop
1414
) implements DwellingEvent {
1515

16-
public static DwellingBuilt event(DwellingId dwellingId, CreatureId creatureId, Cost costPerTroop) {
16+
public static DwellingBuilt event(DwellingId dwellingId, CreatureId creatureId, Resources costPerTroop) {
1717
return new DwellingBuilt(dwellingId.raw(), creatureId.raw(), costPerTroop.raw());
1818
}
1919
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/write/recruitcreature/CreatureRecruited.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.dddheroes.heroesofddd.creaturerecruitment.write.DwellingId;
55
import com.dddheroes.heroesofddd.shared.Amount;
66
import com.dddheroes.heroesofddd.shared.ArmyId;
7-
import com.dddheroes.heroesofddd.shared.Cost;
7+
import com.dddheroes.heroesofddd.shared.Resources;
88
import com.dddheroes.heroesofddd.shared.CreatureId;
99

1010
import java.util.Map;
@@ -22,7 +22,7 @@ public static CreatureRecruited event(
2222
CreatureId creatureId,
2323
ArmyId toArmy,
2424
Amount quantity,
25-
Cost totalCost
25+
Resources totalCost
2626
) {
2727
return new CreatureRecruited(
2828
dwellingId.raw(),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.dddheroes.heroesofddd.resourcespool.write;
2+
3+
import com.dddheroes.heroesofddd.resourcespool.write.deposit.DepositResources;
4+
import com.dddheroes.heroesofddd.resourcespool.write.deposit.ResourcesDeposited;
5+
import com.dddheroes.heroesofddd.resourcespool.write.withdraw.CannotWithdrawMoreThanDepositedResources;
6+
import com.dddheroes.heroesofddd.resourcespool.write.withdraw.ResourcesWithdrawn;
7+
import com.dddheroes.heroesofddd.resourcespool.write.withdraw.WithdrawResources;
8+
import com.dddheroes.heroesofddd.shared.Amount;
9+
import com.dddheroes.heroesofddd.shared.ResourceType;
10+
import com.dddheroes.heroesofddd.shared.Resources;
11+
import org.axonframework.commandhandling.CommandHandler;
12+
import org.axonframework.eventsourcing.EventSourcingHandler;
13+
import org.axonframework.modelling.command.AggregateCreationPolicy;
14+
import org.axonframework.modelling.command.AggregateIdentifier;
15+
import org.axonframework.modelling.command.CreationPolicy;
16+
import org.axonframework.spring.stereotype.Aggregate;
17+
18+
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
19+
20+
@Aggregate
21+
class ResourcesPool {
22+
23+
@AggregateIdentifier
24+
private ResourcesPoolId resourcesPoolId;
25+
private Resources balance = Resources.empty();
26+
27+
@CommandHandler
28+
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)
29+
void decide(DepositResources command) {
30+
apply(ResourcesDeposited.event(command.resourcesPoolId(), command.type(), command.amount()));
31+
}
32+
33+
@EventSourcingHandler
34+
void evolve(ResourcesDeposited event) {
35+
resourcesPoolId = new ResourcesPoolId(event.resourcesPoolId());
36+
this.balance = balance.plus(ResourceType.from(event.type()), new Amount(event.amount()));
37+
}
38+
39+
@CommandHandler
40+
void decide(WithdrawResources command) {
41+
new CannotWithdrawMoreThanDepositedResources(
42+
balance,
43+
Resources.from(command.type(), command.amount())
44+
).verify();
45+
apply(ResourcesWithdrawn.event(command.resourcesPoolId(), command.type(), command.amount()));
46+
}
47+
48+
@EventSourcingHandler
49+
void evolve(ResourcesWithdrawn event) {
50+
resourcesPoolId = new ResourcesPoolId(event.resourcesPoolId());
51+
this.balance = balance.minus(ResourceType.from(event.type()), new Amount(event.amount()));
52+
}
53+
54+
ResourcesPool() {
55+
// required by Axon
56+
}
57+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.dddheroes.heroesofddd.resourcespool.write;
2+
3+
public interface ResourcesPoolCommand {
4+
5+
ResourcesPoolId resourcesPoolId();
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.dddheroes.heroesofddd.resourcespool.write;
2+
3+
public interface ResourcesPoolEvent {
4+
5+
String resourcesPoolId();
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.dddheroes.heroesofddd.resourcespool.write;
2+
3+
import java.util.UUID;
4+
5+
public record ResourcesPoolId(String raw) {
6+
7+
public ResourcesPoolId {
8+
if (raw == null || raw.isBlank()) {
9+
throw new IllegalArgumentException("Resources Pool ID cannot be null or empty");
10+
}
11+
}
12+
13+
public static ResourcesPoolId of(String raw) {
14+
return new ResourcesPoolId(raw);
15+
}
16+
17+
public static ResourcesPoolId random() {
18+
return new ResourcesPoolId(UUID.randomUUID().toString());
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return raw;
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.dddheroes.heroesofddd.resourcespool.write.deposit;
2+
3+
import com.dddheroes.heroesofddd.resourcespool.write.ResourcesPoolCommand;
4+
import com.dddheroes.heroesofddd.resourcespool.write.ResourcesPoolId;
5+
import com.dddheroes.heroesofddd.shared.Amount;
6+
import com.dddheroes.heroesofddd.shared.ResourceType;
7+
import org.axonframework.modelling.command.TargetAggregateIdentifier;
8+
9+
public record DepositResources(
10+
@TargetAggregateIdentifier
11+
ResourcesPoolId resourcesPoolId,
12+
ResourceType type,
13+
Amount amount
14+
) implements ResourcesPoolCommand {
15+
16+
public static DepositResources command(String resourcesPoolId, String type, Integer amount) {
17+
return new DepositResources(ResourcesPoolId.of(resourcesPoolId), ResourceType.from(type), Amount.of(amount));
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.dddheroes.heroesofddd.resourcespool.write.deposit;
2+
3+
import com.dddheroes.heroesofddd.resourcespool.write.ResourcesPoolEvent;
4+
import com.dddheroes.heroesofddd.resourcespool.write.ResourcesPoolId;
5+
import com.dddheroes.heroesofddd.shared.Amount;
6+
import com.dddheroes.heroesofddd.shared.ResourceType;
7+
8+
public record ResourcesDeposited(
9+
String resourcesPoolId,
10+
String type,
11+
Integer amount
12+
) implements ResourcesPoolEvent {
13+
14+
public static ResourcesDeposited event(ResourcesPoolId resourcesPoolId, ResourceType type, Amount amount) {
15+
return new ResourcesDeposited(resourcesPoolId.raw(), type.name(), amount.raw());
16+
}
17+
}

0 commit comments

Comments
 (0)