using Brizco.Domain.Dtos.LargDtos;

namespace Brizco.Repository.Handlers.Task;

public class GetActivityQueryHandler : IRequestHandler<GetTaskQuery, TaskLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetActivityQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }

    public async Task<TaskLDto> Handle(GetTaskQuery request, CancellationToken cancellationToken)
    {
        var task = await _repositoryWrapper.SetRepository<Domain.Entities.Task.Task>()
            .TableNoTracking
            .Where(s => s.Id == request.Id)
            .Select(TaskMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (task == null)
            throw new AppException("Task not found", ApiResultStatusCode.NotFound);
        return task;
    }
}