public IServiceResponse CreateLocationItems(GeocodeResponse geocodeResponse) { LocationServiceResponse response = new LocationServiceResponse(); using (new BulkUpdateContext()) { using (new SecurityDisabler()) { Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master"); Item folderItem = masterDB.GetItem("/sitecore/content/Data Repository/Locations"); TemplateItem locationItemTemplate = masterDB.GetTemplate("{4D641639-9633-4E9A-AB4A-549B269EC3AC}"); if (folderItem != null && locationItemTemplate != null) { var watch = System.Diagnostics.Stopwatch.StartNew(); Random rng = new Random(); for (int i = 1; i <= AmountOfItems; i++) { double latitudeCoefficient = rng.NextDouble(0, 1); double longitudeCoefficient = rng.NextDouble(0, 1); Item newItem = folderItem.Add(String.Format("Location Item {0} {1}", geocodeResponse.City, i), locationItemTemplate); newItem.Editing.BeginEdit(); newItem.Fields["Code"].Value = rng.Next(1000, 9999).ToString(); newItem.Fields["Address"].Value = String.Format("Address Test {0}", i); newItem.Fields["Postal Code"].Value = geocodeResponse.ZipCode; newItem.Fields["Phone Number"].Value = "1234567890"; newItem.Fields["City"].Value = geocodeResponse.City; newItem.Fields["State Province"].Value = geocodeResponse.RegionName; newItem.Fields["Country"].Value = geocodeResponse.CountryName; newItem.Fields["Latitude"].Value = (geocodeResponse.Latitude + latitudeCoefficient).ToString(); newItem.Fields["Longitude"].Value = (geocodeResponse.Longitude + longitudeCoefficient).ToString(); newItem.Editing.AcceptChanges(); } watch.Stop(); response.Success = true; response.Message = String.Format("{0} Locations were created successfully in {1} seconds", AmountOfItems, watch.Elapsed.TotalSeconds); } else { response.Success = false; response.Message = String.Format("Folder Item or Template cannot be found in Sitecore"); } } } return(response); }
public JsonResult CreateLocations(int?quantity) { IServiceResponse response; var geocodeResponse = GetFromCache <GeocodeResponse>("geocodeResponse"); if (geocodeResponse != null) { response = new LocationService(quantity).CreateLocationItems(geocodeResponse); } else { response = new LocationServiceResponse(); response.Message = "Could not fetch geocode response, please try again"; response.Success = false; } return(Json(response)); }
public async Task <LocationServiceResponse> SearchLocationByQueryAsync(string country, string currency, string locale, string query, CancellationToken cancellationToken) { List <object> cacheKeyList = new List <object>(); cacheKeyList.Add("SearchLocationByQueryAsync"); cacheKeyList.Add(country); cacheKeyList.Add(currency); cacheKeyList.Add(locale); cacheKeyList.Add(query); string cacheKey = string.Join(":", cacheKeyList); LocationServiceResponse data = (LocationServiceResponse)cache.Get(cacheKey); if (data == null) { var variables = new Dictionary <string, string>(); variables.Add("{apiKey}", ""); variables.Add("{market}", country); variables.Add("{currency}", currency); variables.Add("{locale}", locale); variables.Add("{query}", query); var URL = Settings.LocationAutoSuggestByQueryURL.ReplaceFromDictionary(variables); var response = await WebRequestHelper.GetAsync(URL, WebRequestHelper.ResponseAcceptType.JSON, cancellationToken); var responseString = response.Item1.BytesToString(); data = JsonConvert.DeserializeObject <LocationServiceResponse>(responseString); CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(Settings.LocationAutoSuggestByQueryCacheExpiration)); cache.Add(cacheKey, data, policy); } return(data); }