Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using cdmdotnet.Logging;
12 : using Cqrs.Authentication;
13 : using Cqrs.Configuration;
14 : using Cqrs.Events;
15 : using Cqrs.Messages;
16 : using Cqrs.Services;
17 :
18 : namespace Cqrs.WebApi
19 : {
20 : /// <summary>
21 : /// A <see cref="CqrsApiController"/> that includes an implementation of the <see cref="IEventService{TAuthenticationToken}.GetEventData"/> method
22 : /// </summary>
23 : public abstract class CqrsEventApiController<TSingleSignOnToken>
24 : : CqrsApiController
25 : , IEventService<TSingleSignOnToken>
26 : where TSingleSignOnToken : ISingleSignOnToken, new()
27 1 : {
28 : /// <summary>
29 : /// Instantiates a new instance of <see cref="CqrsEventApiController{TSingleSignOnToken}"/>.
30 : /// </summary>
31 1 : protected CqrsEventApiController(ILogger logger, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TSingleSignOnToken> authenticationTokenHelper, IEventStore<TSingleSignOnToken> eventStore, IConfigurationManager configurationManager)
32 : : base(logger, correlationIdHelper, configurationManager)
33 : {
34 : AuthenticationTokenHelper = authenticationTokenHelper;
35 : EventStore = eventStore;
36 : }
37 :
38 : /// <summary>
39 : /// Gets or set the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}"/>.
40 : /// </summary>
41 : protected IAuthenticationTokenHelper<TSingleSignOnToken> AuthenticationTokenHelper { get; private set; }
42 :
43 : /// <summary>
44 : /// Gets or set the <see cref="IEventStore{TAuthenticationToken}"/>.
45 : /// </summary>
46 : protected virtual IEventStore<TSingleSignOnToken> EventStore { get; private set; }
47 :
48 :
49 : #region Implementation of IEventService<SingleSignOnToken>
50 :
51 : /// <summary>
52 : /// Get all <see cref="IEvent{TAuthenticationToken}">events</see>
53 : /// raised with the same <see cref="IMessage.CorrelationId"/>.
54 : /// </summary>
55 : /// <param name="serviceRequest">The <see cref="IMessage.CorrelationId"/> of the <see cref="IEvent{TAuthenticationToken}">events</see> to find.</param>
56 : /// <returns>A collection of <see cref="EventData">event data</see></returns>
57 : IServiceResponseWithResultData<IEnumerable<EventData>> IEventService<TSingleSignOnToken>.GetEventData(IServiceRequestWithData<TSingleSignOnToken, Guid> serviceRequest)
58 : {
59 : return GetEventData(serviceRequest);
60 : }
61 :
62 : /// <summary>
63 : /// Get all <see cref="IEvent{TAuthenticationToken}">events</see>
64 : /// raised with the same <see cref="IMessage.CorrelationId"/>.
65 : /// </summary>
66 : /// <param name="serviceRequest">The <see cref="IMessage.CorrelationId"/> of the <see cref="IEvent{TAuthenticationToken}">events</see> to find.</param>
67 : /// <returns>A collection of <see cref="EventData">event data</see></returns>
68 1 : protected virtual IServiceResponseWithResultData<IEnumerable<EventData>> GetEventData(IServiceRequestWithData<TSingleSignOnToken, Guid> serviceRequest)
69 : {
70 : AuthenticationTokenHelper.SetAuthenticationToken(serviceRequest.AuthenticationToken);
71 : CorrelationIdHelper.SetCorrelationId(serviceRequest.CorrelationId);
72 :
73 : OnGetEventData(serviceRequest);
74 : IEnumerable<EventData> results = EventStore.Get(serviceRequest.Data);
75 : results = OnGotEventData(serviceRequest, results);
76 :
77 : return new ServiceResponseWithResultData<IEnumerable<EventData>>
78 : {
79 : State = ServiceResponseStateType.Succeeded,
80 : ResultData = results,
81 : CorrelationId = CorrelationIdHelper.GetCorrelationId()
82 : };
83 : }
84 :
85 : #endregion
86 :
87 : /// <summary>
88 : /// Executed before calling the <see cref="IEventStore{TAuthenticationToken}.Get(System.Type,System.Guid,bool,int)"/> method on <see cref="EventStore"/>
89 : /// in <see cref="GetEventData"/>.
90 : /// </summary>
91 : /// <param name="serviceRequest">The original <see cref="IServiceRequestWithData{TAuthenticationToken,Guid}"/>.</param>
92 1 : protected virtual void OnGetEventData(IServiceRequestWithData<TSingleSignOnToken, Guid> serviceRequest) { }
93 :
94 : /// <summary>
95 : /// Executed after calling the <see cref="IEventStore{TAuthenticationToken}.Get(System.Type,System.Guid,bool,int)"/> method on <see cref="EventStore"/>
96 : /// in <see cref="GetEventData"/>.
97 : /// </summary>
98 : /// <param name="serviceRequest">The original <see cref="IServiceRequestWithData{TAuthenticationToken,Guid}"/>.</param>
99 : /// <param name="results">The collection of <see cref="IEvent{TAuthenticationToken}">events</see> from the <see cref="EventStore"/>.</param>
100 1 : protected virtual IEnumerable<EventData> OnGotEventData(IServiceRequestWithData<TSingleSignOnToken, Guid> serviceRequest, IEnumerable<EventData> results)
101 : {
102 : return results;
103 : }
104 : }
105 : }
|