namespace Netina.Repository.Handlers.Discounts;

public class DeleteDiscountCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<DeleteDiscountCommand, bool>
{
    public async Task<bool> Handle(DeleteDiscountCommand request, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<Discount>().TableNoTracking
            .FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("Discount NotFound", ApiResultStatusCode.NotFound);

        repositoryWrapper.SetRepository<Discount>().Delete(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}