Пример #1
0
        public async Task <bool> DeleteAsync(object id)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    var item = await db.CurrencyExchangeItems.FindAsync((Guid)id);

                    if (item != null)
                    {
                        var tracking = db.CurrencyExchangeItems.Remove(item);
                        await db.SaveChangesAsync();

                        return(tracking.State == EntityState.Deleted);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(false);
            }
        }
        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);
        }
Пример #3
0
        public async Task <IEnumerable <CurrencyExchangeItem> > QueryAsync(Func <CurrencyExchangeItem, bool> predicate)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    return(await db.CurrencyExchangeItems.Where(predicate).AsQueryable().ToListAsync());
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(null);
            }
        }
Пример #4
0
        public async Task <CurrencyExchangeItem> GetAsync(object id)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    return(await db.CurrencyExchangeItems.FindAsync((Guid)id));
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(null);
            }
        }
Пример #5
0
        public async Task <List <CurrencyExchangeItem> > GetAllAsync()
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    return(await db.CurrencyExchangeItems.OrderBy(x => x.SortOrder).ToListAsync());
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(null);
            }
        }
Пример #6
0
        public async Task <bool> UpdateRangeAsync(IEnumerable <CurrencyExchangeItem> entities)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    db.CurrencyExchangeItems.UpdateRange(entities);
                    await db.SaveChangesAsync();

                    return(true);
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(false);
            }
        }
Пример #7
0
        public async Task <bool> UpdateAsync(CurrencyExchangeItem entity)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    var tracking = db.CurrencyExchangeItems.Update(entity);
                    await db.SaveChangesAsync();

                    return(tracking.State == EntityState.Modified);
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(false);
            }
        }
        private static CurrencyAddRemove FindNewCurrencies(List <tblCurrencyMarketView> freshCurrencies, int transactionID)
        {
            List <string> newlyAdded   = new List <string>();
            List <string> newlyDeleted = new List <string>();

            CurrencyAddRemove pairs = new CurrencyAddRemove();

            var uniqueFromDB = CurrencyDataContext.GetUniqueCurrencyIDs();

            var uniqueFromWeb = ((from a in freshCurrencies
                                  select a.CoinMarkCapID).Distinct()).ToList();

            newlyAdded = GetNewlyAdded(uniqueFromDB, uniqueFromWeb);

            newlyDeleted = GetNewlyDeleted(uniqueFromDB, uniqueFromWeb);

            pairs.Added   = newlyAdded;
            pairs.Removed = newlyDeleted;


            return(pairs);
        }
Пример #9
0
        public async Task InitializeDatabaseAsync()
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    db.Database.EnsureCreated();
                    // Add some default currencies for the first time.
                    var count = await db.CurrencyExchangeItems.CountAsync();

                    if (count == 0)
                    {
                        var targetCodes = new List <string> {
                            "USD", "CNY", "NZD", "AUD"
                        };
                        var targets = new List <CurrencyExchangeItem>();
                        targetCodes.ForEach(x =>
                        {
                            var item = new CurrencyExchangeItem(new CurrencyItem {
                                Code = "USD"
                            }, new CurrencyItem {
                                Code = x
                            }, ++count);
                            targets.Add(item);
                        });
                        await db.CurrencyExchangeItems.AddRangeAsync(targets);

                        await db.SaveChangesAsync();
                    }
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
            }
        }
Пример #10
0
        public CurrencyXplorer(IApplicationEnvironment appEnv) : this()
        {
            //Configuration = Utils.CreateConfiguration(appEnv, "config.json");

            // Entry point for dependency injection.

            _iCurrencyImporter = new JsonCurrencyImporter();
            //_iCurrencyImporter = new LocalJsonCurrencyImporter(); ;

            _iCurrencyProvider = new NationalBankCurrencyProvider(_iCurrencyImporter);

            _currencyDataContext = new CurrencyDataContext(Configuration["Data:DefaultConnection:ConnectionString"]);
            _iCurrencyRepository = new MsSqlExplorerRepository(_currencyDataContext);

            _iCachingProcessor = new ApiDatabaseCachingProcessor(_iCurrencyProvider, _iCurrencyRepository);

            _iUserSettingsHolder = new UserSettingsHolder(_iCurrencyRepository);

            _dataPresenter = new DataPresenter();
            //_dataHolder = new DataHolder(_iCachingProcessor, _iUserSettingsHolder, Configuration);
            _dataProcessor = new DataProcessor(_iCachingProcessor);

            _allCurrencyCodes = GetAllCurrencyCodes();
        }
        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);
            }
        }
Пример #12
0
 public MsSqlExplorerRepository(CurrencyDataContext currencyDataContext)
 {
     _currencyDataContext = currencyDataContext;
 }
 public static void LogError(Exception ex)
 {
     // Logging error in seperate thread
     Task err = Task.Run(() => CurrencyDataContext.InsertError(ex));
 }