using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Request.Queries;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Request.Handlers
{
    public class GetRequestHandler : IRequestHandler<GetRequestQuery, Data.Entities.Request>
    {
        private readonly IRequestRepository _requestService;


        public GetRequestHandler(IRequestRepository requestService)
        {
            _requestService = requestService;
        }
        public async Task<Data.Entities.Request> Handle(GetRequestQuery request, CancellationToken cancellationToken)
        {
            if (request.Id == null)
            {
                throw new BadHttpRequestException("Id cannot be null");
            }

            var req = await _requestService.GetByIdAsync(request.Id);

            if (request == null)
            {
                throw new NotFoundException("Request not found");
            }

            return req;
        }
    }
}
