@@ -9,6 +9,7 @@ namespace TransactionProcessor.Controllers
99 using System . Threading ;
1010 using Microsoft . AspNetCore . Mvc ;
1111 using Newtonsoft . Json ;
12+ using Newtonsoft . Json . Linq ;
1213 using Shared . DomainDrivenDesign . EventSourcing ;
1314 using Shared . EventStore . Aggregate ;
1415 using Shared . EventStore . EventHandling ;
@@ -94,11 +95,6 @@ public async Task<IActionResult> PostEventAsync([FromBody] Object request,
9495 }
9596 }
9697
97- /// <summary>
98- /// Callbacks the specified cancellation token.
99- /// </summary>
100- /// <param name="cancellationToken">The cancellation token.</param>
101- /// <param name="eventId">The event identifier.</param>
10298 private void Callback ( CancellationToken cancellationToken ,
10399 Guid eventId )
104100 {
@@ -109,22 +105,17 @@ private void Callback(CancellationToken cancellationToken,
109105 }
110106 }
111107
112- /// <summary>
113- /// Gets the domain event.
114- /// </summary>
115- /// <param name="domainEvent">The domain event.</param>
116- /// <returns></returns>
117108 private async Task < IDomainEvent > GetDomainEvent ( Object domainEvent )
118109 {
119- String eventType = this . Request . Query [ "eventType" ] . ToString ( ) ;
110+ String eventType = this . Request . Headers [ "eventType" ] . ToString ( ) ;
120111
121- var type = TypeMap . GetType ( eventType ) ;
112+ Type type = TypeMap . GetType ( eventType ) ;
122113
123114 if ( type == null )
124115 throw new Exception ( $ "Failed to find a domain event with type { eventType } ") ;
125116
126117 JsonIgnoreAttributeIgnorerContractResolver jsonIgnoreAttributeIgnorerContractResolver = new JsonIgnoreAttributeIgnorerContractResolver ( ) ;
127- var jsonSerialiserSettings = new JsonSerializerSettings
118+ JsonSerializerSettings jsonSerialiserSettings = new JsonSerializerSettings
128119 {
129120 ReferenceLoopHandling = ReferenceLoopHandling . Ignore ,
130121 TypeNameHandling = TypeNameHandling . All ,
@@ -135,15 +126,28 @@ private async Task<IDomainEvent> GetDomainEvent(Object domainEvent)
135126
136127 if ( type . IsSubclassOf ( typeof ( DomainEvent ) ) )
137128 {
138- var json = JsonConvert . SerializeObject ( domainEvent , jsonSerialiserSettings ) ;
139- DomainEventFactory domainEventFactory = new ( ) ;
129+ String json = JsonConvert . SerializeObject ( domainEvent , jsonSerialiserSettings ) ;
140130
141- return domainEventFactory . CreateDomainEvent ( json , type ) ;
131+ DomainEventFactory domainEventFactory = new ( ) ;
132+ String validatedJson = this . ValidateEvent ( json ) ;
133+ return domainEventFactory . CreateDomainEvent ( validatedJson , type ) ;
142134 }
143135
144136 return null ;
145137 }
146138
139+ private String ValidateEvent ( String domainEventJson )
140+ {
141+ JObject domainEvent = JObject . Parse ( domainEventJson ) ;
142+
143+ if ( domainEvent . ContainsKey ( "eventId" ) == false || domainEvent [ "eventId" ] . ToObject < Guid > ( ) == Guid . Empty )
144+ {
145+ throw new ArgumentException ( "Domain Event must contain an Event Id" ) ;
146+ }
147+
148+ return domainEventJson ;
149+ }
150+
147151 #endregion
148152
149153 #region Others
0 commit comments