namespace Brizco.Repository.Handlers.Task;

public class DeleteActivityCommandHandler : IRequestHandler<DeleteTaskCommand, bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public DeleteActivityCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<bool> Handle(DeleteTaskCommand request, CancellationToken cancellationToken)
    {
        var task = await _repositoryWrapper.SetRepository<Domain.Entities.Task.Task>()
            .TableNoTracking
            .FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
        if (task == null)
            throw new AppException("Task not found", ApiResultStatusCode.NotFound);
        _repositoryWrapper.SetRepository<Domain.Entities.Task.Task>()
            .Delete(task);

        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}