using NetinaCMS.Repository.Repositories.Base.Contracts;

namespace NetinaCMS.Repository.Handlers.BlogCategories;

public class DeleteBlogCategoryCommandHandler : IRequestHandler<DeleteBlogCategoryCommand,bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public DeleteBlogCategoryCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<bool> Handle(DeleteBlogCategoryCommand request, CancellationToken cancellationToken)
    {
        var ent = await _repositoryWrapper.SetRepository<BlogCategory>()
            .TableNoTracking
            .FirstOrDefaultAsync(bc => bc.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("Blog Category not found", ApiResultStatusCode.NotFound);

        _repositoryWrapper.SetRepository<BlogCategory>()
            .Delete(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}