public async Task <(bool IsValid, PostcodeLocation PostcodeLocation)> IsPostcodeValid(string postcode)
    {
        var key = CacheKeys.PostcodeKey(postcode);

        if (_cache.TryGetValue(key, out PostcodeLocation postcodeLocation))
        {
            _logger.LogInformation("Cache hit - found postcode {postcode}", postcodeLocation.Postcode);
        }
        else
        {
            var location = await _locationApiClient.GetGeoLocationDataAsync(postcode);

            if (location is not null)
            {
                postcodeLocation = new PostcodeLocation
                {
                    Postcode  = location.Postcode,
                    Latitude  = location.Latitude,
                    Longitude = location.Longitude
                };

                if (_cacheExpiryInSeconds > 0)
                {
                    _cache.Set(key, postcodeLocation, CacheUtilities.DefaultMemoryCacheEntryOptions(
                                   _dateTimeService,
                                   _logger,
                                   _cacheExpiryInSeconds,
                                   _cacheExpiryInSeconds
                                   ));
                }
            }
        }

        return(postcodeLocation != null, postcodeLocation);
    }
Esempio n. 2
0
    public async Task Step_Validate_Postcode_And_()
    {
        const string postcode  = "mk 4 2 8 y u";
        const string expected  = "MK42 8YU";
        const double latitude  = 50.0123;
        const double longitude = 1.987;
        const bool   isValid   = true;

        var expectedPostcodeLocation = new PostcodeLocation
        {
            Postcode  = expected,
            Latitude  = latitude,
            Longitude = longitude
        };

        var viewModel = new FindViewModel
        {
            Postcode = postcode
        };

        var context = new SearchContext(viewModel);

        _providerSearchService.IsSearchPostcodeValid(Arg.Is <string>(p => p == postcode)).Returns((isValid, expectedPostcodeLocation));

        await _searchStep.Execute(context);

        context.ViewModel.Postcode.Should().Be(expected);
        context.Continue.Should().BeTrue();
        await _providerSearchService.Received(1).IsSearchPostcodeValid(Arg.Is <string>(p => p == postcode));
    }
Esempio n. 3
0
    public async Task Step_Validate_Wrong_Postcode()
    {
        const string postcode  = "dddfd";
        const double latitude  = 50.0123;
        const double longitude = 1.987;
        const bool   isValid   = false;

        var postcodeLocation = new PostcodeLocation
        {
            Postcode  = postcode,
            Latitude  = latitude,
            Longitude = longitude
        };

        var viewModel = new FindViewModel
        {
            Postcode = postcode
        };

        var context = new SearchContext(viewModel);

        _providerSearchService.IsSearchPostcodeValid(Arg.Is <string>(p => p == postcode)).Returns((isValid, postcodeLocation));

        await _searchStep.Execute(context);

        context.ViewModel.ValidationStyle.Should().Be(AppConstants.ValidationStyle);
        context.ViewModel.PostcodeValidationMessage.Should().Be(AppConstants.RealPostcodeValidationMessage);
        context.Continue.Should().BeFalse();
        await _providerSearchService.Received(1).IsSearchPostcodeValid(Arg.Is <string>(p => p == postcode));
    }
    public async Task <IList <ProviderLocation> > CalculateProviderLocationDistanceInMiles(PostcodeLocation origin, IQueryable <ProviderLocation> providerLocations)
    {
        double originLatitude;
        double originLongitude;

        if (!origin.Latitude.HasValue || !origin.Longitude.HasValue)
        {
            var originGeoLocation = await _locationApiClient.GetGeoLocationDataAsync(origin.Postcode);

            originLatitude  = originGeoLocation.Latitude;
            originLongitude = originGeoLocation.Longitude;
        }
        else
        {
            originLatitude  = origin.Latitude.Value;
            originLongitude = origin.Longitude.Value;
        }

        var results = new List <ProviderLocation>();

        foreach (var providerLocation in providerLocations)
        {
            providerLocation.DistanceInMiles = CalculateDistanceInMiles(
                originLatitude, originLongitude,
                providerLocation.Latitude, providerLocation.Longitude);

            results.Add(providerLocation);
        }

        return(results);
    }