예제 #1
0
        public async Task <GetClosestLocationsResponse> Handle(GetClosestLocationsQuery request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Getting closest locations for {request.Ukprn}, {request.ApprenticeshipId}, {request.PostCode}");

            _requestValidator.ValidateAndThrow(request);

            var coordinateResponse = await _postCodeLookup.GetLatLongFromPostCode(request.PostCode);

            //LocationLookupResponse.
            var coordinates = coordinateResponse.Coordinate;

            var result = await _searchProvider.GetClosestLocations(request.ApprenticeshipId, request.Ukprn, coordinates);

            return(new GetClosestLocationsResponse
            {
                ProviderName = result.ProviderName,
                Results = result
            });
        }
        public async Task <ProviderSearchResults> SearchProviders(string apprenticeshipId, string postCode, Pagination pagination, IEnumerable <string> deliveryModes, bool hasNonLevyContract, bool showNationalOnly, int orderBy = 0)
        {
            if (string.IsNullOrEmpty(postCode))
            {
                return(new ProviderSearchResults {
                    ApprenticeshipId = apprenticeshipId, PostCodeMissing = true
                });
            }

            int apprenticeshipIdInt;

            IApprenticeshipProduct apprenticeship;

            if (int.TryParse(apprenticeshipId, out apprenticeshipIdInt))
            {
                apprenticeship = _getStandards.GetStandardById(apprenticeshipId);
            }
            else
            {
                apprenticeship = _getFrameworks.GetFrameworkById(apprenticeshipId);
            }

            if (apprenticeship == null)
            {
                return(GetProviderSearchResultErrorResponse(apprenticeshipId, null, postCode, LocationLookupResponse.ApprenticeshipNotFound));
            }

            try
            {
                var coordinateResponse = await _postCodeLookup.GetLatLongFromPostCode(postCode);

                var coordinates = coordinateResponse.Coordinate;


                if (coordinateResponse.ResponseCode != LocationLookupResponse.Ok)
                {
                    return(GetProviderSearchResultErrorResponse(apprenticeshipId, apprenticeship?.Title, postCode, coordinateResponse.ResponseCode));
                }

                var takeElements = pagination.Take == 0 ? _paginationSettings.DefaultResultsAmount : pagination.Take;

                LogSearchRequest(postCode, coordinates);

                var filter = new ProviderSearchFilter
                {
                    DeliveryModes      = deliveryModes,
                    HasNonLevyContract = hasNonLevyContract,
                    ShowNationalOnly   = showNationalOnly
                };

                var searchResults = await _providerSearchProvider.SearchProvidersByLocation(apprenticeshipId, coordinates, pagination.Page, takeElements, filter, orderBy);

                var result = new ProviderSearchResults
                {
                    TotalResults               = searchResults.Total,
                    ResultsToTake              = takeElements,
                    ApprenticeshipId           = apprenticeshipId,
                    Title                      = apprenticeship?.Title,
                    Level                      = apprenticeship.Level,
                    PostCode                   = postCode,
                    TrainingOptionsAggregation = searchResults.TrainingOptionsAggregation,
                    NationalProviders          = searchResults.NationalProvidersAggregation,
                    SelectedTrainingOptions    = deliveryModes,
                    ResponseCode               = LocationLookupResponse.Ok,
                    ShowNationalProvidersOnly  = false,
                    Hits     = searchResults.Hits,
                    LastPage = takeElements > 0 ? (int)System.Math.Ceiling((double)searchResults.Total / takeElements) : 1
                };

                return(result);
            }
            catch (SearchException ex)
            {
                _logger.Error(ex, "Search for provider failed.");

                return(GetProviderSearchResultErrorResponse(apprenticeshipId, apprenticeship?.Title, postCode, ServerLookupResponse.InternalServerError));
            }
        }