Api/NetinaShop.Repository/Handlers/OrderBags/SubmitDiscountCommandHandle...

29 lines
1.2 KiB
C#

using NetinaShop.Domain.Entities.Orders;
namespace NetinaShop.Repository.Handlers.OrderBags;
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;
}
}