Example #1
0
 public MainContext()
 {
     Storage              = new StorageContext();
     Model                = new ModelContext(this);
     UI                   = new UIContext(this);
     Security             = new SecurityContext(this);
     Services             = new ServicesContext(this);
     GeoUtility           = new GeoUtility();
     AccelerometerUtility = new AccelerometerUtility();
     Settings             = new SettingsContext();
 }
        public IEnumerable <InventoryStore> SaveNearestStores()
        {
            UserLocation userLocation = GeoUtility.GetUserLocation();

            Assert.IsNotNull(userLocation, nameof(userLocation));

            List <InventoryStore> storeList = new List <InventoryStore>();
            var ceConfig = (CommerceEngineConfiguration)Factory.CreateObject("commerceEngineConfiguration", true);
            var uri      = new System.Uri(EngineConnectUtility.EngineConfiguration.ShopsServiceUrl);
            var client   = this.GetClient(ceConfig);
            var result   = client.GetAsync("NearestStoreLocator('" + userLocation.Latitude + "|" + userLocation.Longitude + "')").Result;

            if (result.IsSuccessStatusCode)
            {
                var     resultContent = result.Content.ReadAsStringAsync().Result;
                JObject resultList    = JObject.Parse(resultContent);

                foreach (var store in resultList["value"])
                {
                    dynamic newStore = new System.Dynamic.ExpandoObject();
                    newStore.Id = store["Id"];
                    newStore.InventoryStoreId = store["InventoryStoreId"];
                    newStore.DisplayName      = store["DisplayName"];
                    newStore.Address          = store["Address"];
                    newStore.City             = store["City"];
                    newStore.State            = store["StateCode"];
                    newStore.Country          = store["CountryCode"];
                    newStore.Distance         = store["Distance"];
                    newStore.Zip = store["Zip"];
                    storeList.Add(new InventoryStore(newStore));
                }

                string     storeJson    = new JavaScriptSerializer().Serialize(storeList.Take(2));
                HttpCookie storesCookie = new HttpCookie(StoreLocatorConstants.STORE_COOKIES_KEY, storeJson)
                {
                    Expires = DateTime.Now.AddHours(1)
                };
                HttpContext.Current.Response.Cookies.Add(storesCookie);
            }
            else
            {
                return(Enumerable.Empty <InventoryStore>());
            }

            return(storeList.Take(2));
        }