namespace Brizco.Repository.Handlers.Shifts;

public class DeletePositionCommandHandler : IRequestHandler<DeleteShiftCommand, bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public DeletePositionCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<bool> Handle(DeleteShiftCommand request, CancellationToken cancellationToken)
    {
        var shift = await _repositoryWrapper.SetRepository<Shift>()
            .TableNoTracking
            .FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
        if (shift == null)
            throw new AppException("Routine not found", ApiResultStatusCode.NotFound);
        _repositoryWrapper.SetRepository<Shift>()
            .Delete(shift);

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