public void GetGlobalMarketView()
        {
            HttpRequestClient client = new HttpRequestClient();
            var obj = client.GetGlobalMarketView();

            tblGlobalMarketView gmv = new tblGlobalMarketView();

            Guid guid = Guid.NewGuid();

            gmv = new tblGlobalMarketView()
            {
                ID                            = guid,
                ActiveAssets                  = Utlity.ParseInt(obj.ActiveAssets),
                ActiveCurrencies              = Utlity.ParseInt(obj.ActiveCurrencies),
                ActiveMarkets                 = Utlity.ParseInt(obj.ActiveMarkets),
                BTCPercentageofMarketCap      = Utlity.ParseDouble(obj.BTCPercentageOfMarketCap),
                LastUpdated                   = obj.LastUpdated,
                TimeStamp                     = DateTime.Now,
                Total24HVolumeConvertCurrency = Utlity.ParseDecimal(obj.Total24hVolumeConvertCurrency),
                Total24HVolumeUSD             = Utlity.ParseDecimal(obj.Total24hVolumeUSD),
                TotalMarketCapConvertCurrency = Utlity.ParseDecimal(obj.TotalMarketCapConvertCurrency),
                TotalMarketCapUSD             = Utlity.ParseDecimal(obj.TotalMarketCapUSD),
                TransactionID                 = guid.ToString().GetHashCode()
            };

            CurrencyDataContext.InsertGlobalMarketView(gmv);
        }
        private List <tblCurrencyView> GetCurrencyViewToUpdate(List <tblCurrencyMarketView> curFreshList, List <tblCurrencyView> newlyAdded)
        {
            List <tblCurrencyView> curView = new List <tblCurrencyView>();

            foreach (var item in curFreshList)
            {
                curView.Add(new tblCurrencyView
                {
                    AvailableSupply     = item.AvailableSupply,
                    CoinMarkCapID       = item.CoinMarkCapID,
                    MarketCapUSD        = item.MarketCapUSD,
                    Name                = item.Name,
                    PriceBTC            = item.PriceBTC,
                    PriceUSD            = item.PriceUSD,
                    Rank                = item.Rank,
                    Symbol              = item.Symbol,
                    TimeStamp           = DateTime.Now,
                    TotalSupply         = item.TotalSupply,
                    LastUpdatedUTC      = Utlity.ParseLong(item.LastUpdatedUTC),
                    PercentageChange1H  = item.PercentageChange1H,
                    PercentageChange24H = item.PercentageChange24H,
                    PercentageChange7D  = item.PercentageChange7D,
                    Volume24hUSD        = item.Volume24hUSD,
                });
            }

            if (newlyAdded != null && newlyAdded.Count > 0)
            {
                foreach (var item in newlyAdded)
                {
                    foreach (var newItem in curView)
                    {
                        if (item.CoinMarkCapID.ToLower() == newItem.CoinMarkCapID.ToLower())
                        {
                            curView.Remove(newItem);
                            break;
                        }
                    }
                }
            }

            return(curView);
        }
        public void GetAllCurrenciesAndUpdate()
        {
            //For each JSON call there will be transaction ID associated with it
            var restTransactionID = Guid.NewGuid().GetHashCode();

            try
            {
                HttpRequestClient client = new HttpRequestClient();
                var freshCurrencies      = client.GetCurrencies();

                tblCurrencyMarketView         cur          = null;
                List <tblCurrencyMarketView>  curFreshList = new List <tblCurrencyMarketView>();
                List <tblCurrencyRankChanged> ranksChanged = new List <tblCurrencyRankChanged>();
                List <tblCurrencyView>        newlyAdded   = new List <tblCurrencyView>();

                foreach (var item in freshCurrencies)
                {
                    cur = new tblCurrencyMarketView()
                    {
                        ID                  = Guid.NewGuid(),
                        CoinMarkCapID       = item.Id,
                        AvailableSupply     = Utlity.ParseDecimal(item.AvailableSupply),
                        LastUpdatedUTC      = item.LastUpdated,
                        MarketCapConvert    = Utlity.ParseDecimal(item.MarketCapConvert),
                        MarketCapUSD        = Utlity.ParseDecimal(item.MarketCapUsd),
                        Name                = item.Name,
                        PercentageChange1H  = Utlity.ParseDouble(item.PercentChange1h),
                        PercentageChange24H = Utlity.ParseDouble(item.PercentChange24h),
                        PercentageChange7D  = Utlity.ParseDouble(item.PercentChange7d),
                        PriceBTC            = Utlity.ParseDecimal(item.PriceBtc),
                        PriceConvert        = Utlity.ParseDecimal(item.PriceConvert),
                        PriceUSD            = Utlity.ParseDecimal(item.PriceUsd),
                        Rank                = Utlity.ParseInt(item.Rank),
                        Symbol              = item.Symbol,
                        TotalSupply         = Utlity.ParseDecimal(item.TotalSupply),
                        Volume24hUSD        = Utlity.ParseDecimal(item.Volume24hUsd),
                        Volumne24HConvert   = Utlity.ParseDecimal(item.Volume24Convert),
                        TimeStamp           = DateTime.Now,
                        TransactionID       = restTransactionID
                    };
                    curFreshList.Add(cur);
                }


                //Add Fresh Currencies, which are going to be just 1 or 2 at a time
                var addRemovePairs = FindNewCurrencies(curFreshList, restTransactionID);
                if (addRemovePairs.Added != null && addRemovePairs.Added.Count > 0)
                {
                    newlyAdded = BuildNewCurrencyList(curFreshList, addRemovePairs.Added, restTransactionID);
                    CurrencyDataContext.InsertCurrencyViews(newlyAdded);
                }

                if (addRemovePairs.Removed != null && addRemovePairs.Removed.Count > 0)
                {
                    CurrencyDataContext.UpdateCurrencyViewForDelete(addRemovePairs.Removed, restTransactionID);
                }

                //Get those currency views which need to be updated in DB (New ones)
                var updateCurView = GetCurrencyViewToUpdate(curFreshList, newlyAdded);

                //The update the old currency views and report rank change
                if (updateCurView != null && updateCurView.Count > 0)
                {
                    ranksChanged = CurrencyDataContext.UpdateCurrencyViewAndCurrencyRankChanged(updateCurView, restTransactionID);
                }

                CurrencyDataContext.InsertCurrencyMarketView(curFreshList);

                CurrencyDataContext.InsertTransactionLog(restTransactionID);
            }
            catch (Exception ex)
            {
                CurrencyDataContext.InsertTransactionLog(restTransactionID, false);
                ErrorLogging.LogError(ex);
            }
        }