Exemplo n.º 1
0
        public static List <ProductDetails> GetProductsInRadius(int i_RadiusInKM, string i_Barcode,
                                                                double i_Lat, double i_Lng)
        {
            APIfunctions API = new APIfunctions();
            List <GetPriceByProductBarCode> ProductsFromAPI = new List <GetPriceByProductBarCode>();

            //list of ID's of stores that are in requested radius.
            //(from the DB)
            Dictionary <string, double> storesInRadius = GetStoresIDList(i_Lng, i_Lat, i_RadiusInKM);
            List <ProductDetails>       Products       = GetProductsFromDB(storesInRadius, i_Barcode);

            //if we didnt find the entire data in DB then we need to get the rest of it from the API
            if (storesInRadius.Count > 0)
            {
                try // If API is not working well - we will throw Exception
                {
                    ProductsFromAPI = API.GetListOfProducts(storesInRadius, i_Barcode);
                    List <ProductDetails> resProduct = CreateProductDetailsFromProductsAPI(ProductsFromAPI);
                    Products.AddRange(resProduct);
                    UpdateDBWithProducts(ProductsFromAPI, i_Barcode);
                }
                catch (ArgumentException ArgEx)
                {
                    throw ArgEx; // problems with NameValueCollection
                }
                catch (Exception Ex)
                {
                    throw Ex; //API problems
                }
            }

            return(Products);
        }
Exemplo n.º 2
0
        public static void StartDB()
        {
            MySqlConnection conn = ConnectToDB();
            APIfunctions    API  = new APIfunctions();
            // list of all chains
            List <Chain> Chains = API.GetChains();

            foreach (Chain chain in Chains)
            {
                //for each cahin - get list of stores
                List <GetStoresByChain> Stores = API.GetStores(chain.chain_id);
                foreach (GetStoresByChain store in Stores)
                {
                    if (!(store.store_id.Equals(null)))
                    {
                        //for each store in chain- insert data into DB
                        try
                        {
                            CheckForNullFields(store);
                            string insertQuery = string.Format(@"insert into
Store (ChainName, StoreName, StoreID, GPSLat, GPSLng, StoreCity, StoreAddress) 
VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}');",
                                                               mySQLEscape(store.chain_name), mySQLEscape(store.store_name),
                                                               store.store_id, store.store_gps_lat,
                                                               store.store_gps_lng, mySQLEscape(store.city_name), mySQLEscape(store.store_address));
                            MySqlCommand insertCommand = new MySqlCommand(insertQuery, conn);

                            if (insertCommand.ExecuteNonQuery() == 1)
                            {
                                Console.WriteLine("In ZolpoDB :: StartDB. Insert succeeded");
                            }
                            else
                            {
                                Console.WriteLine("In ZolpoDB :: StartDB. Insert failed for Store ID:{0}", store.store_id);
                            }
                        }
                        catch (MySqlException ex)
                        {
                            Console.WriteLine("In ZolpoDB::StartDB. Exception: " + ex.Message);
                        }
                    }
                }
            }
            CloseConnectionToDB(conn);
        }