using Netina.Domain.Entities.Brands;

namespace Netina.Repository.Handlers.Brands;

public class DeleteBrandCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<DeleteBrandCommand, bool>
{
    public async Task<bool> Handle(DeleteBrandCommand request, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<Brand>().TableNoTracking
            .FirstOrDefaultAsync(b => b.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("Brand not found");
        repositoryWrapper.SetRepository<Brand>().Delete(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);

        return true;
    }
}