コード例 #1
0
        /// <summary>
        /// Updates the list of stocks in the database by adding new stocks from the list of current stocks and
        /// by deleting stocks that are no longer listed in the list of current stocks.
        /// </summary>
        /// <param name="currentStocks">The current stocks.</param>
        public void MakeDbStockListCurrent(List<Stock> currentStocks)
        {
            foreach (var stock in currentStocks)
            {
                try
                {
                    using (var context = new StockScreenerEntities())
                    {
                        if (context.Stocks.Any(s => s.Ticker.Equals(stock.Ticker)))
                        {
                            var stockToUpdate = context.Stocks.First(s => s.Ticker.Equals(stock.Ticker));
                            if (!stockToUpdate.CompanyName.Equals(stock.CompanyName))
                            {
                                Console.WriteLine("Updating " + stock.Ticker + ":  " + stockToUpdate.CompanyName + " to " + stock.CompanyName);

                                stockToUpdate.CompanyName = stock.CompanyName;
                                context.SaveChanges();
                            }
                        }
                        else
                        {
                            context.Stocks.AddObject(stock);
                            context.SaveChanges();

                            Console.WriteLine("Added " + stock.Exchange + ":" + stock.Ticker);
                        }
                    }
                }
                catch (UpdateException ex)
                {
                    if (string.IsNullOrEmpty(ex.InnerException.Message) || !ex.InnerException.Message.Contains("Cannot insert duplicate key"))
                    {
                        throw;
                    }
                }
            }

            this.PruneStocksInDb(currentStocks);
        }
コード例 #2
0
        /// <summary>
        /// Creates a new watch list.
        /// </summary>
        private void CreateWatchList()
        {
            // First, display a list of existing watch lists.
            this.DisplayWatchLists();

            // Prompt the user to enter a name for the new watch list.
            Console.Write("Enter new watch list name ([Blank] to cancel) -->  ");
            var name = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                // The user opted to cancel without entering a name.
                Console.WriteLine();
                return;
            }

            // Prompt the user to confirm the inputted name.
            Console.Write("Create watch list named '" + name + "'?  (Y/N) -->  ");
            var isConfirmed = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(isConfirmed) || !isConfirmed.ToUpper().Equals("Y"))
            {
                // The user did not confirm the name.
                Console.WriteLine("Watch list not created.");
                Console.WriteLine();
                return;
            }

            // Create a new entry in the database using the inputted name.
            using (var context = new StockScreenerEntities())
            {
                try
                {
                    context.Watchlists.AddObject(new Watchlist { Name = name });
                    context.SaveChanges();
                    Console.WriteLine("Watch list was created.");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Deletes a watch list.
        /// </summary>
        private void DeleteWatchList()
        {
            // First, display a list of available watch lists.
            this.DisplayWatchLists();

            // Prompt the user to enter the watch list that will be deleted.
            Console.Write("Select ID of watch list to delete. ([Blank] to cancel.) -->  ");
            var input = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(input))
            {
                // The user opted to cancel without entering an ID.
                Console.WriteLine();
                return;
            }

            int id;
            if (!int.TryParse(input, out id))
            {
                // Invalid Input -- non-numeric data!!!
                Console.WriteLine("Invalid input...no watch list was deleted.");
                Console.WriteLine();
                return;
            }

            using (var context = new StockScreenerEntities())
            {
                try
                {
                    var watchLists = context.Watchlists.ToList();

                    if (watchLists.Count(list => list.ID == id) <= 0)
                    {
                        // Invalid Input -- Invalid watch list ID!!!
                        Console.WriteLine("Invalid input...no watch list was deleted.");
                        Console.WriteLine();
                        return;
                    }

                    // Delete the selected ID from the database.
                    var listToDelete = watchLists.First(list => list.ID == id);
                    context.Watchlists.DeleteObject(listToDelete);
                    context.SaveChanges();
                    Console.WriteLine("Watch list was deleted.");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// Prunes the stocks in the DB by removing all stocks that are not in the list of current stocks.
 /// </summary>
 /// <param name="currentStocks">The list of current stocks that should remain in the DB.</param>
 private void PruneStocksInDb(IEnumerable<Stock> currentStocks)
 {
     using (var context = new StockScreenerEntities())
     {
         var databaseStocks = context.Stocks.ToList();
         foreach (var stock in databaseStocks.Where(stock => currentStocks.Count(list => list.Ticker.Equals(stock.Ticker)) <= 0))
         {
             try
             {
                 context.Stocks.DeleteObject(stock);
                 context.SaveChanges();
                 Console.WriteLine("Removed " + stock.Exchange + ":" + stock.Ticker);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex);
             }
         }
     }
 }
コード例 #5
0
        /// <summary>
        /// Updates the daily stock data in database.
        /// </summary>
        /// <param name="dailies">The daily data to insert into the database.</param>
        private static void UpdateDailiesInDatabase(IEnumerable<StockDaily> dailies)
        {
            using (var context = new StockScreenerEntities())
            {
                foreach (var daily in dailies)
                {
                    context.StockDailies.AddObject(daily);
                }

                var numRecords = context.SaveChanges();

                Console.WriteLine("Inserted " + numRecords + " records.");
            }
        }