1
1
using MediatR ;
2
- using PlayOfferService . Application . Exceptions ;
3
2
using PlayOfferService . Domain . Events ;
4
3
using PlayOfferService . Domain . Events . PlayOffer ;
5
4
using PlayOfferService . Domain . Models ;
@@ -13,15 +12,15 @@ public class ClubEventHandler : IRequestHandler<TechnicalClubEvent>
13
12
private readonly ReadEventRepository _readEventRepository ;
14
13
private readonly WriteEventRepository _writeEventRepository ;
15
14
private readonly PlayOfferRepository _playOfferRepository ;
16
-
15
+
17
16
public ClubEventHandler ( ClubRepository clubRepository , ReadEventRepository readEventRepository , WriteEventRepository writeEventRepository , PlayOfferRepository playOfferRepository )
18
17
{
19
18
_clubRepository = clubRepository ;
20
19
_readEventRepository = readEventRepository ;
21
20
_writeEventRepository = writeEventRepository ;
22
21
_playOfferRepository = playOfferRepository ;
23
22
}
24
-
23
+
25
24
public async Task Handle ( TechnicalClubEvent clubEvent , CancellationToken cancellationToken )
26
25
{
27
26
Console . WriteLine ( "ClubEventHandler received event: " + clubEvent . EventType ) ;
@@ -31,7 +30,7 @@ public async Task Handle(TechnicalClubEvent clubEvent, CancellationToken cancell
31
30
Console . WriteLine ( "Event already applied, skipping" ) ;
32
31
return ;
33
32
}
34
-
33
+
35
34
switch ( clubEvent . EventType )
36
35
{
37
36
case EventType . TENNIS_CLUB_REGISTERED :
@@ -46,8 +45,11 @@ public async Task Handle(TechnicalClubEvent clubEvent, CancellationToken cancell
46
45
case EventType . TENNIS_CLUB_DELETED :
47
46
await HandleTennisClubDeletedEvent ( clubEvent ) ;
48
47
break ;
48
+ case EventType . TENNIS_CLUB_NAME_CHANGED :
49
+ await HandleTennisClubNameChangedEvent ( clubEvent ) ;
50
+ break ;
49
51
}
50
-
52
+
51
53
await _clubRepository . Update ( ) ;
52
54
await _readEventRepository . AppendEvent ( clubEvent ) ;
53
55
await _readEventRepository . Update ( ) ;
@@ -56,7 +58,7 @@ public async Task Handle(TechnicalClubEvent clubEvent, CancellationToken cancell
56
58
private async Task HandleTennisClubDeletedEvent ( TechnicalClubEvent clubEvent )
57
59
{
58
60
await CreatePlayOfferCancelledEventsByClubId ( clubEvent ) ;
59
-
61
+
60
62
var existingClub = await _clubRepository . GetClubById ( clubEvent . EntityId ) ;
61
63
existingClub ! . Apply ( [ clubEvent ] ) ;
62
64
}
@@ -70,7 +72,7 @@ private async Task HandleTennisClubUnlockedEvent(TechnicalClubEvent clubEvent)
70
72
private async Task HandleTennisClubLockedEvent ( TechnicalClubEvent clubEvent )
71
73
{
72
74
await CreatePlayOfferCancelledEventsByClubId ( clubEvent ) ;
73
-
75
+
74
76
var existingClub = await _clubRepository . GetClubById ( clubEvent . EntityId ) ;
75
77
existingClub ! . Apply ( [ clubEvent ] ) ;
76
78
}
@@ -81,12 +83,12 @@ private async Task HandleTennisClubRegisteredEvent(TechnicalClubEvent clubEvent)
81
83
newClub . Apply ( [ clubEvent ] ) ;
82
84
_clubRepository . CreateClub ( newClub ) ;
83
85
}
84
-
86
+
85
87
private async Task CreatePlayOfferCancelledEventsByClubId ( TechnicalClubEvent clubEvent )
86
88
{
87
89
// Get all play offers by club id
88
90
var existingPlayOffer = await _playOfferRepository . GetPlayOffersByIds ( null , null , clubEvent . EntityId ) ;
89
-
91
+
90
92
// Create PlayOfferCancelled events for each play offer
91
93
foreach ( var playOffer in existingPlayOffer )
92
94
{
@@ -100,9 +102,15 @@ private async Task CreatePlayOfferCancelledEventsByClubId(TechnicalClubEvent clu
100
102
EventData = new PlayOfferCancelledEvent ( ) ,
101
103
CorrelationId = clubEvent . EventId
102
104
} ;
103
-
105
+
104
106
await _writeEventRepository . AppendEvent ( cancelledEvent ) ;
105
107
}
106
108
await _writeEventRepository . Update ( ) ;
107
109
}
110
+
111
+ private async Task HandleTennisClubNameChangedEvent ( TechnicalClubEvent clubEvent )
112
+ {
113
+ var existingClub = await _clubRepository . GetClubById ( clubEvent . EntityId ) ;
114
+ existingClub ! . Apply ( [ clubEvent ] ) ;
115
+ }
108
116
}
0 commit comments