|
2 | 2 | using dotnet_qrshop.Abstractions.Authentication; |
3 | 3 | using dotnet_qrshop.Abstractions.Messaging; |
4 | 4 | using dotnet_qrshop.Common.Enums; |
| 5 | +using dotnet_qrshop.Common.Models; |
5 | 6 | using dotnet_qrshop.Common.Results; |
6 | 7 | using dotnet_qrshop.Infrastructure.Database.DbContext; |
7 | 8 | using Microsoft.EntityFrameworkCore; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | +using RedLockNet; |
8 | 11 |
|
9 | 12 | namespace dotnet_qrshop.Features.Payments.Commands.CreatePaymentIntent; |
10 | 13 |
|
11 | 14 | public class CreatePaymentIntentCommandHandler( |
12 | 15 | ApplicationDbContext _dbContext, |
13 | 16 | IUserContext _userContext, |
14 | | - IPaymentService _paymentService) : ICommandHandler<CreatePaymentIntentCommand, string> |
| 17 | + IPaymentService _paymentService, |
| 18 | + IDistributedLockFactory _lockFactory, |
| 19 | + IOptions<RedlockSettings> redlockOptions) : ICommandHandler<CreatePaymentIntentCommand, string> |
15 | 20 | { |
| 21 | + private readonly RedlockSettings _lockSettings = redlockOptions.Value; |
16 | 22 | public async Task<Result<string>> Handle(CreatePaymentIntentCommand command, CancellationToken cancellationToken) |
17 | 23 | { |
| 24 | + using var redlock = await _lockFactory.CreateLockAsync("test", _lockSettings.Expiry, _lockSettings.Wait, _lockSettings.Retry); |
| 25 | + if (!redlock.IsAcquired) |
| 26 | + { |
| 27 | + // TODO DYLAN: LOG here |
| 28 | + return Result.Failure<string>(Error.Conflict("Couldn't create payment intent", "Error processing payment, please try again or contact the support")); |
| 29 | + } |
| 30 | + |
18 | 31 | var orderInfo = await _dbContext.Orders |
19 | 32 | .AsNoTracking() |
20 | 33 | .Where(o => o.UserId == _userContext.UserId && o.Id == command.OrderId) |
|
0 commit comments