namespace Netina.Repository.Handlers.Products;

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

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