@@ -10,7 +10,6 @@ namespace TransactionProcessor.BusinessLogic.Services;
1010using Microsoft . EntityFrameworkCore ;
1111using Models ;
1212using NetBarcode ;
13- using Shared . DomainDrivenDesign . EventSourcing ;
1413using Shared . EntityFramework ;
1514using Shared . EventStore . Aggregate ;
1615using Shared . Exceptions ;
@@ -69,9 +68,7 @@ public class VoucherDomainService : IVoucherDomainService
6968 private static readonly String EstateManagementDatabaseName = "TransactionProcessorReadModel" ;
7069
7170 private readonly IAggregateService AggregateService ;
72-
73- private const String ConnectionStringIdentifier = "EstateReportingReadModel" ;
74-
71+
7572 #region Constructors
7673
7774 public VoucherDomainService ( Func < IAggregateService > aggregateService ,
@@ -83,107 +80,86 @@ public VoucherDomainService(Func<IAggregateService> aggregateService,
8380 #endregion
8481
8582 #region Methods
86-
87- private async Task < Result < T > > ApplyUpdates < T > ( Func < VoucherAggregate , Task < Result < T > > > action ,
88- Guid voucherId ,
89- CancellationToken cancellationToken ,
90- Boolean isNotFoundError = true )
91- {
92- try
93- {
94- Result < VoucherAggregate > getVoucherResult = await this . AggregateService . GetLatest < VoucherAggregate > ( voucherId , cancellationToken ) ;
95- Result < VoucherAggregate > voucherAggregateResult =
96- DomainServiceHelper . HandleGetAggregateResult ( getVoucherResult , voucherId , isNotFoundError ) ;
97-
98- if ( voucherAggregateResult . IsFailed )
99- return ResultHelpers . CreateFailure ( voucherAggregateResult ) ;
100-
101- VoucherAggregate voucherAggregate = voucherAggregateResult . Data ;
102- Result < T > result = await action ( voucherAggregate ) ;
103- if ( result . IsFailed )
104- return ResultHelpers . CreateFailure ( result ) ;
105-
106- Result saveResult = await this . AggregateService . Save ( voucherAggregate , cancellationToken ) ;
107- if ( saveResult . IsFailed )
108- return ResultHelpers . CreateFailure ( saveResult ) ;
109- return Result . Success ( result . Data ) ;
110- }
111- catch ( Exception ex )
112- {
113- return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
114- }
115- }
116-
83+
11784 public async Task < Result < IssueVoucherResponse > > IssueVoucher ( Guid voucherId , Guid operatorId , Guid estateId ,
11885 Guid transactionId ,
11986 DateTime issuedDateTime ,
12087 Decimal value ,
12188 String recipientEmail , String recipientMobile , CancellationToken cancellationToken ) {
122- Result < IssueVoucherResponse > result = await ApplyUpdates < IssueVoucherResponse > (
123- async ( VoucherAggregate voucherAggregate ) => {
89+ try {
90+ Result < EstateResponse > validateResult = await this . ValidateVoucherIssue ( estateId , operatorId , cancellationToken ) ;
91+ if ( validateResult . IsFailed )
92+ return ResultHelpers . CreateFailure ( validateResult ) ;
12493
125- Result < EstateResponse > validateResult = await this . ValidateVoucherIssue ( estateId , operatorId , cancellationToken ) ;
126- if ( validateResult . IsFailed )
127- return ResultHelpers . CreateFailure ( validateResult ) ;
94+ Result < VoucherAggregate > voucherResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . GetLatest < VoucherAggregate > ( voucherId , ct ) , voucherId , cancellationToken , false ) ;
95+ if ( voucherResult . IsFailed )
96+ return ResultHelpers . CreateFailure ( voucherResult ) ;
12897
129- voucherAggregate . Generate ( operatorId , estateId , transactionId , issuedDateTime , value ) ;
98+ VoucherAggregate voucherAggregate = voucherResult . Data ;
13099
131- Models . Voucher voucherModel = voucherAggregate . GetVoucher ( ) ;
100+ voucherAggregate . Generate ( operatorId , estateId , transactionId , issuedDateTime , value ) ;
132101
133- // Generate the barcode
134- Barcode barcode = new Barcode ( voucherModel . VoucherCode ) ;
135- voucherAggregate . AddBarcode ( barcode . GetBase64Image ( ) ) ;
136- voucherAggregate . Issue ( recipientEmail , recipientMobile , issuedDateTime ) ;
102+ Models . Voucher voucherModel = voucherAggregate . GetVoucher ( ) ;
137103
138- return Result . Success ( new IssueVoucherResponse
139- {
140- ExpiryDate = voucherModel . ExpiryDate ,
141- Message = voucherModel . Message ,
142- VoucherCode = voucherModel . VoucherCode ,
143- VoucherId = voucherId
144- } ) ;
104+ // Generate the barcode
105+ Barcode barcode = new Barcode ( voucherModel . VoucherCode ) ;
106+ voucherAggregate . AddBarcode ( barcode . GetBase64Image ( ) ) ;
107+ voucherAggregate . Issue ( recipientEmail , recipientMobile , issuedDateTime ) ;
145108
146- } , voucherId , cancellationToken , false ) ;
109+ Result saveResult = await this . AggregateService . Save ( voucherAggregate , cancellationToken ) ;
110+ if ( saveResult . IsFailed )
111+ return ResultHelpers . CreateFailure ( saveResult ) ;
147112
148- return result ;
113+ return Result . Success ( new IssueVoucherResponse
114+ {
115+ ExpiryDate = voucherModel . ExpiryDate ,
116+ Message = voucherModel . Message ,
117+ VoucherCode = voucherModel . VoucherCode ,
118+ VoucherId = voucherId
119+ } ) ;
120+ }
121+ catch ( Exception ex )
122+ {
123+ return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
124+ }
149125 }
150126
151- /// <summary>
152- /// Redeems the voucher.
153- /// </summary>
154- /// <param name="estateId">The estate identifier.</param>
155- /// <param name="voucherCode">The voucher code.</param>
156- /// <param name="redeemedDateTime">The redeemed date time.</param>
157- /// <param name="cancellationToken">The cancellation token.</param>
158- /// <returns></returns>
159- /// <exception cref="NotFoundException">No voucher found with voucher code [{voucherCode}]</exception>
160127 public async Task < Result < RedeemVoucherResponse > > RedeemVoucher ( Guid estateId ,
161128 String voucherCode ,
162129 DateTime redeemedDateTime ,
163130 CancellationToken cancellationToken )
164131 {
165- // Find the voucher based on the voucher code
166- using ResolvedDbContext < EstateManagementContext > ? resolvedContext = this . Resolver . Resolve ( EstateManagementDatabaseName , estateId . ToString ( ) ) ;
167- await using EstateManagementContext context = resolvedContext . Context ;
132+ try
133+ {
134+ // Find the voucher based on the voucher code
135+ using ResolvedDbContext < EstateManagementContext > ? resolvedContext = this . Resolver . Resolve ( EstateManagementDatabaseName , estateId . ToString ( ) ) ;
136+ await using EstateManagementContext context = resolvedContext . Context ;
168137
169- TransactionProcessor . Database . Entities . VoucherProjectionState voucher = await context . VoucherProjectionStates . SingleOrDefaultAsync ( v => v . VoucherCode == voucherCode , cancellationToken ) ;
138+ TransactionProcessor . Database . Entities . VoucherProjectionState voucher = await context . VoucherProjectionStates . SingleOrDefaultAsync ( v => v . VoucherCode == voucherCode , cancellationToken ) ;
170139
171- if ( voucher == null )
172- {
173- return Result . NotFound ( $ "No voucher found with voucher code [{ voucherCode } ]") ;
174- }
140+ if ( voucher == null )
141+ {
142+ return Result . NotFound ( $ "No voucher found with voucher code [{ voucherCode } ]") ;
143+ }
175144
176- Result < RedeemVoucherResponse > result = await ApplyUpdates < RedeemVoucherResponse > (
177- async ( VoucherAggregate voucherAggregate ) => {
178145 Result < EstateResponse > validateResult = await this . ValidateVoucherRedemption ( estateId , cancellationToken ) ;
179146 if ( validateResult . IsFailed )
180147 return ResultHelpers . CreateFailure ( validateResult ) ;
181-
148+
149+
150+
151+ Result < VoucherAggregate > voucherResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . GetLatest < VoucherAggregate > ( voucher . VoucherId , ct ) , voucher . VoucherId , cancellationToken ) ;
152+ if ( voucherResult . IsFailed )
153+ return ResultHelpers . CreateFailure ( voucherResult ) ;
154+
155+ VoucherAggregate voucherAggregate = voucherResult . Data ;
156+
182157 // Redeem the voucher
183158 voucherAggregate . Redeem ( redeemedDateTime ) ;
184159
185- // Save the changes
186- await this . AggregateService . Save ( voucherAggregate , cancellationToken ) ;
160+ Result saveResult = await this . AggregateService . Save ( voucherAggregate , cancellationToken ) ;
161+ if ( saveResult . IsFailed )
162+ return ResultHelpers . CreateFailure ( saveResult ) ;
187163
188164 Models . Voucher voucherModel = voucherAggregate . GetVoucher ( ) ;
189165
@@ -194,21 +170,21 @@ public async Task<Result<RedeemVoucherResponse>> RedeemVoucher(Guid estateId,
194170 VoucherCode = voucherModel . VoucherCode
195171 } ) ;
196172
197- } , voucher . VoucherId , cancellationToken ) ;
198-
199- return result ;
173+ }
174+ catch ( Exception ex )
175+ {
176+ return Result . Failure ( ex . GetExceptionMessages ( ) ) ;
177+ }
200178 }
201179
202180
203181 private async Task < Result > ValidateVoucherIssue ( Guid estateId , Guid operatorId , CancellationToken cancellationToken )
204182 {
205183 // Validate the Estate Record is a valid estate
206- Result < EstateAggregate > getEstateResult = await this . AggregateService . Get < EstateAggregate > ( estateId , cancellationToken ) ;
207- if ( getEstateResult . IsFailed )
208- {
209- return ResultHelpers . CreateFailure ( getEstateResult ) ;
210- }
211- EstateAggregate estateAggregate = getEstateResult . Data ;
184+ Result < EstateAggregate > estateResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . Get < EstateAggregate > ( estateId , ct ) , estateId , cancellationToken ) ;
185+ if ( estateResult . IsFailed )
186+ return ResultHelpers . CreateFailure ( estateResult ) ;
187+ EstateAggregate estateAggregate = estateResult . Data ;
212188
213189 Estate estate = estateAggregate . GetEstate ( ) ;
214190 if ( estate . Operators == null || estate . Operators . Any ( ) == false )
@@ -228,10 +204,9 @@ private async Task<Result> ValidateVoucherIssue(Guid estateId, Guid operatorId,
228204 private async Task < Result > ValidateVoucherRedemption ( Guid estateId , CancellationToken cancellationToken )
229205 {
230206 // Validate the Estate Record is a valid estate
231- Result < EstateAggregate > getEstateResult = await this . AggregateService . Get < EstateAggregate > ( estateId , cancellationToken ) ;
232- if ( getEstateResult . IsFailed ) {
233- return ResultHelpers . CreateFailure ( getEstateResult ) ;
234- }
207+ Result < EstateAggregate > estateResult = await DomainServiceHelper . GetAggregateOrFailure ( ct => this . AggregateService . Get < EstateAggregate > ( estateId , ct ) , estateId , cancellationToken ) ;
208+ if ( estateResult . IsFailed )
209+ return ResultHelpers . CreateFailure ( estateResult ) ;
235210
236211 return Result . Success ( ) ;
237212 }
0 commit comments