public async Task <LocationItem> GetLocationInformation(string location, double lat, double lon, bool includeDistrictNameInPostcodeDisplayName = false)
        {
            if (string.IsNullOrEmpty(location))
            {
                return(null);
            }

            if (lat != 0 && lon != 0)
            {
                return(new LocationItem(location, new [] { lat, lon }, string.Empty));
            }

            GetLocationsListItem getLocationsListItem = null;

            if (Regex.IsMatch(location, PostcodeRegex))
            {
                getLocationsListItem = await _locationApiClient.Get <GetLocationsListItem>(new GetLocationByFullPostcodeRequest(location));

                getLocationsListItem.IncludeDistrictNameInPostcodeDisplayName = includeDistrictNameInPostcodeDisplayName;
                location = getLocationsListItem.DisplayName;
            }
            else if (Regex.IsMatch(location, OutcodeDistrictRegex))
            {
                getLocationsListItem = await _locationApiClient.Get <GetLocationsListItem>(new GetLocationByOutcodeAndDistrictRequest(location.Split(' ').FirstOrDefault()));

                if (getLocationsListItem.Location != null)
                {
                    location = getLocationsListItem.DisplayName;
                }
            }
            else if (Regex.IsMatch(location, OutcodeRegex))
            {
                getLocationsListItem = await _locationApiClient.Get <GetLocationsListItem>(new GetLocationByOutcodeRequest(location));
            }
            else if (location.Split(",").Length >= 2)
            {
                var locationInformation = location.Split(",");
                var locationName        = string.Join(",", locationInformation.Take(locationInformation.Length - 1)).Trim();
                var authorityName       = locationInformation.Last().Trim();
                getLocationsListItem = await _locationApiClient.Get <GetLocationsListItem>(new GetLocationByLocationAndAuthorityName(locationName, authorityName));
            }

            if (location.Length >= 2 && getLocationsListItem?.Location == null)
            {
                var locations = await _locationApiClient.Get <GetLocationsListResponse>(new GetLocationsQueryRequest(location));

                var locationsListItem = locations?.Locations?.FirstOrDefault();
                if (locationsListItem != null)
                {
                    getLocationsListItem = locationsListItem;
                    location             = getLocationsListItem.DisplayName;
                }
            }

            return(getLocationsListItem?.Location != null
                ? new LocationItem(location, getLocationsListItem.Location.GeoPoint, getLocationsListItem.Country)
                : null);
        }
        public async Task <GetLocationsResult> Handle(GetLocationsQuery request, CancellationToken cancellationToken)
        {
            var result = await _apiClient.Get <GetLocationsListResponse>(new GetLocationsQueryRequest(request.SearchTerm));

            return(new GetLocationsResult
            {
                Locations = result.Locations
            });
        }
Example #3
0
        public async Task <GetAddressesQueryResult> Handle(GetAddressesQuery request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(request.Query))
            {
                throw new ArgumentException($"Query is required", nameof(GetAddressesQuery.Query));
            }

            var addressesResponse = await _locationApiClient.Get <GetAddressesListResponse>(new GetAddressesQueryRequest(request.Query, _config.LocationsApiMinMatch));

            return(new GetAddressesQueryResult(addressesResponse));
        }