public async Task <IActionResult> Post(Location location, [FromServices] IOptions <ApiBehaviorOptions> apiBehaviorOptions) { try { Guid.Parse(location.Id); } catch (Exception _) { ModelState.AddModelError(nameof(Location.Id), "Location Id is not a valid Guid"); return(apiBehaviorOptions.Value.InvalidModelStateResponseFactory(ControllerContext)); } var postcode = location.Physical_Addresses.First().Postal_Code; var isValid = await _postcodeServiceClient.ValidatePostcode(postcode); if (!isValid.Result) { ModelState.AddModelError(nameof(Organisation.Id), "Postcode does not exist."); return(apiBehaviorOptions.Value.InvalidModelStateResponseFactory(ControllerContext)); } var locationResult = await _postcodeServiceClient.GetPostcodeLocation(postcode); location.Latitude = locationResult.Latitude; location.Longitude = locationResult.Longitude; _locationSearchServiceClient.AddOrUpdateLocation(location); await _locationRepository.InsertOne(location); return(Accepted(location)); }
public async Task <IEnumerable <SearchLocation> > QueryLocations(string postcode, double?distance) { double kilometers = 5; if (distance != null) { kilometers = (double)distance / 0.62437; } var location = await _postcodeServiceClient.GetPostcodeLocation(postcode); var searchParameters = new SearchParameters() { Filter = $"geo.distance(Point, geography'POINT({location.Latitude} {location.Longitude})') le {Math.Round(kilometers, 2)}", SearchMode = SearchMode.Any, }; try { var results = await _searchIndexClient.Documents.SearchAsync <SearchLocation>("*", searchParameters); var locations = results.Results.Select(r => r.Document); return(locations); } catch (Exception _) { throw; } }