36 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C#
		
	
	
| namespace Netina.Core.EntityServices.DiscountHandlers;
 | |
| 
 | |
| public class CheckUserDiscountFirstUseCommandHandler : IRequestHandler<CheckUserDiscountFirstUseCommand, bool>
 | |
| {
 | |
|     private readonly IRepositoryWrapper _repositoryWrapper;
 | |
|     private readonly ICurrentUserService _currentUserService;
 | |
| 
 | |
|     public CheckUserDiscountFirstUseCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
 | |
|     {
 | |
|         _repositoryWrapper = repositoryWrapper;
 | |
|         _currentUserService = currentUserService;
 | |
|     }
 | |
| 
 | |
|     public async Task<bool> Handle(CheckUserDiscountFirstUseCommand request, CancellationToken cancellationToken)
 | |
|     {
 | |
| 
 | |
|         if (_currentUserService.UserId != null && Guid.TryParse(_currentUserService.UserId, out Guid userId))
 | |
|         {
 | |
|             var customer = await _repositoryWrapper.SetRepository<Customer>()
 | |
|                 .TableNoTracking
 | |
|                 .FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken);
 | |
|             if (customer == null)
 | |
|                 throw new BaseApiException(ApiResultStatusCode.NotFound, "Customer not found");
 | |
| 
 | |
|             var discountedUserOrder = await _repositoryWrapper.SetRepository<Order>()
 | |
|                 .TableNoTracking
 | |
|                 .FirstOrDefaultAsync(f => f.CustomerId == customer.Id && f.DiscountCode == request.DiscountCode,
 | |
|                     cancellationToken);
 | |
|             if (discountedUserOrder != null)
 | |
|                 return false;
 | |
|             return true;
 | |
|         }
 | |
|         else
 | |
|             throw new BaseApiException(ApiResultStatusCode.BadRequest,"UserId is wrong");
 | |
|     }
 | |
| } |