ddd-team1 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@ export class MakeMoveUseCase {
|
|
|
9
9
|
constructor(private repo: GameRepository) {}
|
|
10
10
|
|
|
11
11
|
async execute(gameId: Game['id'], standardAlgebraicNotation: string): Promise<void> {
|
|
12
|
-
|
|
12
|
+
const game = await this.repo.findById(gameId) ?? new Game(gameId);
|
|
13
13
|
const expectedVersion = game.version;
|
|
14
14
|
const command = {
|
|
15
15
|
name: 'Make move!' as const,
|
package/src/domain/Game.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { UUID } from 'crypto';
|
|
2
2
|
import type { Command } from './Command.ts';
|
|
3
|
-
import type { DomainEvent
|
|
3
|
+
import type { DomainEvent } from './DomainEvent.ts';
|
|
4
4
|
import { AggregateRoot } from './shared';
|
|
5
5
|
import { Board } from './value-objects/Board.ts';
|
|
6
6
|
import { StandardAlgebraicNotationMove } from './value-objects/StandardAlgebraicNotationMove.ts';
|
|
@@ -14,23 +14,22 @@ export class Game extends AggregateRoot<UUID> {
|
|
|
14
14
|
|
|
15
15
|
static fromEvents(id: UUID, events: DomainEvent[]): Game {
|
|
16
16
|
const game = new Game(id);
|
|
17
|
-
game.
|
|
18
|
-
game.setVersion(events.filter(e => e.aggregateId === id).length);
|
|
17
|
+
game.rehydrate(events.filter(e => e.aggregateId === id));
|
|
19
18
|
return game;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
protected apply(event: DomainEvent): void {
|
|
22
|
+
switch (event.name) {
|
|
23
|
+
case 'Game started.':
|
|
24
|
+
break;
|
|
25
|
+
case 'Move made.':
|
|
26
|
+
this.board = this.board.play(StandardAlgebraicNotationMove.fromString(event.payload));
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
move(move: StandardAlgebraicNotationMove):
|
|
33
|
-
if (this.
|
|
31
|
+
move(move: StandardAlgebraicNotationMove): void {
|
|
32
|
+
if (this.version === 0) {
|
|
34
33
|
this.recordThat({
|
|
35
34
|
name: 'Game started.',
|
|
36
35
|
aggregateId: this.id,
|
|
@@ -42,14 +41,13 @@ export class Game extends AggregateRoot<UUID> {
|
|
|
42
41
|
aggregateId: this.id,
|
|
43
42
|
payload: move.toString(),
|
|
44
43
|
});
|
|
45
|
-
this.replay(this.newlyRecordEvents);
|
|
46
|
-
return this.newlyRecordEvents;
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
execute(command: Command):
|
|
46
|
+
execute(command: Command): void {
|
|
50
47
|
switch (command.name) {
|
|
51
48
|
case 'Make move!':
|
|
52
|
-
|
|
49
|
+
this.move(command.payload.move);
|
|
50
|
+
break;
|
|
53
51
|
default:
|
|
54
52
|
throw new Error(`Unknown command: ${command.name}`);
|
|
55
53
|
}
|
|
@@ -3,28 +3,35 @@ import type { DomainEvent } from "../DomainEvent.ts";
|
|
|
3
3
|
|
|
4
4
|
export abstract class AggregateRoot<T> extends Entity<T>
|
|
5
5
|
{
|
|
6
|
-
|
|
6
|
+
private _newlyRecordedEvents: DomainEvent[] = [];
|
|
7
7
|
private _version = 0;
|
|
8
8
|
|
|
9
9
|
get version(): number {
|
|
10
10
|
return this._version;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
flushEvents(): DomainEvent[] {
|
|
14
|
-
const eventsToFlush = [...this.newlyRecordEvents];
|
|
15
|
-
this.newlyRecordEvents = [];
|
|
16
|
-
return eventsToFlush;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
13
|
constructor(id: T){
|
|
20
14
|
super(id);
|
|
21
15
|
}
|
|
22
16
|
|
|
23
|
-
protected
|
|
24
|
-
|
|
17
|
+
protected abstract apply(event: DomainEvent): void;
|
|
18
|
+
|
|
19
|
+
protected recordThat(event: DomainEvent): void {
|
|
20
|
+
this.apply(event);
|
|
21
|
+
this._version++;
|
|
22
|
+
this._newlyRecordedEvents.push(event);
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
rehydrate(events: DomainEvent[]): void {
|
|
26
|
+
for (const event of events) {
|
|
27
|
+
this.apply(event);
|
|
28
|
+
this._version++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
flushEvents(): DomainEvent[] {
|
|
33
|
+
const eventsToFlush = [...this._newlyRecordedEvents];
|
|
34
|
+
this._newlyRecordedEvents = [];
|
|
35
|
+
return eventsToFlush;
|
|
29
36
|
}
|
|
30
37
|
}
|