27 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			27 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
| namespace NetinaShop.Core.EntityServices.OrderBagHandlers;
 | |
| 
 | |
| public class SubmitDiscountCommandHandler : IRequestHandler<SubmitDiscountCommand,bool>
 | |
| {
 | |
|     private readonly IRepositoryWrapper _repositoryWrapper;
 | |
|     private readonly IMediator _mediator;
 | |
| 
 | |
|     public SubmitDiscountCommandHandler(IRepositoryWrapper repositoryWrapper,IMediator mediator)
 | |
|     {
 | |
|         _repositoryWrapper = repositoryWrapper;
 | |
|         _mediator = mediator;
 | |
|     }
 | |
|     public async Task<bool> Handle(SubmitDiscountCommand request, CancellationToken cancellationToken)
 | |
|     {
 | |
|         var order = await _repositoryWrapper.SetRepository<Order>()
 | |
|             .TableNoTracking
 | |
|             .FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
 | |
|         if (order == null)
 | |
|             throw new AppException("Order not found", ApiResultStatusCode.NotFound);
 | |
|         order.SetDiscount(request.DiscountCode);
 | |
|         _repositoryWrapper.SetRepository<Order>().Update(order);
 | |
|         await _repositoryWrapper.SaveChangesAsync(cancellationToken);
 | |
|         await _mediator.Send(new CalculateOrderCommand(order.Id), cancellationToken);
 | |
| 
 | |
|         return true;
 | |
|     }
 | |
| } |