namespace Netina.Repository.Handlers.ProductCategories;

public class DeleteProductCategoryCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<DeleteProductCategoryCommand, bool>
{
    public async Task<bool> Handle(DeleteProductCategoryCommand request, CancellationToken cancellationToken)
    {

        var ent = await repositoryWrapper.SetRepository<ProductCategory>().TableNoTracking
            .FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("ProductCategory not found", ApiResultStatusCode.NotFound);
        repositoryWrapper.SetRepository<ProductCategory>().Delete(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}