public override async Task <IDisplayResult> DisplayAsync(PostcodeSearch part, BuildPartDisplayContext context) { var contents = new List <PostcodeSearchViewModel>(); var results = Enumerable.Empty <ContentItem>(); ContentItem[] list = null; var postcode = GetQuerystringValue(QueryStringPostcode); var postcodesApi = new PostcodesApi(); var response = await postcodesApi.GetGameData(postcode); if (response != null && !string.IsNullOrEmpty(postcode)) { var coord = new GeoCoordinate(Convert.ToDouble(response.FirstResult.Latitude), Convert.ToDouble(response.FirstResult.Longitude)); results = await _session.Query <ContentItem>() .With <ContentItemIndex>() .Where(x => x.Published) .With <PostcodeIndex>() .ListAsync(); foreach (var result in results) { contents.Add(new PostcodeSearchViewModel { ContentItem = result, Latitude = Convert.ToDouble(result?.ContentItem?.As <PostcodePart>()?.Latitude ?? null), Longitude = Convert.ToDouble(result?.ContentItem?.As <PostcodePart>()?.Longitude ?? null), Postcode = result?.ContentItem?.As <PostcodePart>()?.Postcode }); } list = contents.OrderBy(x => x.GeoCoordinate.GetDistanceTo(coord)).ToList().Select(x => x.ContentItem).ToArray(); } return(Initialize <PostcodeSearchListViewModel>("PostcodeSearch_List", model => { model.EmptyResultsContent = part.EmptyResultsContent; model.Postcode = postcode; model.PostcodeInputPlaceholder = part.PostcodeInputPlaceholder; model.Results = list; model.SubmitButtonLabel = part.SubmitButtonLabel; }) .Location("Detail", "Content:5")); }
public async override Task <IDisplayResult> UpdateAsync(PostcodePart part, IUpdateModel updater, UpdatePartEditorContext context) { var model = new PostcodeViewModel(); if (await updater.TryUpdateModelAsync(model, Prefix)) { var postcodesApi = new PostcodesApi(); var response = await postcodesApi.GetGameData(model.Postcode); part.Postcode = model.Postcode; part.Longitude = response != null && !string.IsNullOrEmpty(response.FirstResult.Postcode) ? response.FirstResult.Longitude : model.Longitude; part.Latitude = response != null && !string.IsNullOrEmpty(response.FirstResult.Postcode) ? response.FirstResult.Latitude : model.Latitude; } if (!string.IsNullOrEmpty(model.Postcode) && string.IsNullOrEmpty(part.Longitude) && string.IsNullOrEmpty(part.Latitude)) { updater.ModelState.AddModelError(Prefix, nameof(part.Postcode), T["Please enter a valid postcode or longitude, latitude."]); } return(Edit(part, context)); }