public static void Main(string[] args) { WorldTradingDataApi newCallToApi = new WorldTradingDataApi(); var request = newCallToApi.MakeApiRequest(); var response = newCallToApi.ReceieveApiResponse(request); List <CallForStockInfo> allStocks = new List <CallForStockInfo>(); CallForStockInfo newStock; var callForStockInfo = newCallToApi.DeserializeResponse(response); for (int stockIndex = 0; stockIndex < WorldTradingDataApi.stocks.Count; stockIndex++) { string symbol = callForStockInfo.data[stockIndex].symbol.ToString(); string price = callForStockInfo.data[stockIndex].price.ToString(); string price_open = callForStockInfo.data[stockIndex].price_open.ToString(); string day_high = callForStockInfo.data[stockIndex].day_high.ToString(); string day_low = callForStockInfo.data[stockIndex].day_low.ToString(); string change_pct = callForStockInfo.data[stockIndex].change_pct.ToString(); string volume_avg = callForStockInfo.data[stockIndex].volume_avg.ToString(); newStock = new CallForStockInfo(symbol, price, price_open, day_high, day_low, change_pct, volume_avg); allStocks.Add(newStock); newStock.DisplayStockInfoToConsole(newStock); Database.MoveCurrentStockInfoToHistoryOfStocksTable(newStock); Database.AddCurrentStockInfoIntoDatabase(newStock); } Console.ReadLine(); }
public void DisplayStockInfoToConsole(CallForStockInfo newStock) { Console.WriteLine($"Symbol: {newStock.Symbol}"); Console.WriteLine($"Price: ${newStock.Price}"); Console.WriteLine($"Open Price: ${newStock.OpenPrice}"); Console.WriteLine($"High Price: ${newStock.HighPrice}"); Console.WriteLine($"Low Price: ${newStock.LowPrice}"); Console.WriteLine($"Percent Change: {newStock.PercentChange}%"); Console.WriteLine($"Average Volume: {newStock.AvgVolume}"); Console.WriteLine(); }
public static void AddCurrentStockInfoIntoDatabase(CallForStockInfo stock) { string connectionString = GetConnectionString(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); //Console.WriteLine("State: {0}", connection.State); //Console.WriteLine("ConnectionString: {0}", // connection.ConnectionString); try { using (SqlCommand command = new SqlCommand( /* * "INSERT INTO WorldTradingData VALUES(@Symbol, @Price, @OpenPrice, @HighPrice, @LowPrice, " + * "@PercentChange, @AvgVolume, @Date)", connection)) */ "IF NOT EXISTS(SELECT * FROM WorldTradingData WHERE Symbol = @Symbol) INSERT INTO WorldTradingData VALUES(@Symbol, @Price, @OpenPrice, @HighPrice, @LowPrice," + "@PercentChange, @AvgVolume, @Date)" + "ELSE UPDATE WorldTradingData SET PercentChange = @PercentChange, AvgVolume = @AvgVolume, " + "Price = @Price, OpenPrice = @OpenPrice, HighPrice = @HighPrice, LowPrice = @LowPrice, Date = @Date WHERE Symbol = @Symbol", connection)) { command.Parameters.Add(new SqlParameter("Symbol", stock.Symbol)); command.Parameters.Add(new SqlParameter("Price", stock.Price)); command.Parameters.Add(new SqlParameter("OpenPrice", stock.OpenPrice)); command.Parameters.Add(new SqlParameter("HighPrice", stock.HighPrice)); command.Parameters.Add(new SqlParameter("LowPrice", stock.LowPrice)); command.Parameters.Add(new SqlParameter("PercentChange", stock.PercentChange)); command.Parameters.Add(new SqlParameter("AvgVolume", stock.AvgVolume)); command.Parameters.Add(new SqlParameter("Date", DateTime.Now)); command.ExecuteNonQuery(); Console.WriteLine($"{stock.Symbol} stock successfully added to WorldTradingData table"); } Console.WriteLine("The database has been successfully updated."); connection.Close(); } catch (Exception e) { Console.WriteLine("Values could not be inserted into database"); throw e; } } }
public static void MoveCurrentStockInfoToHistoryOfStocksTable(CallForStockInfo stock) { string connectionString = GetConnectionString(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); try { using (SqlCommand command = new SqlCommand( "INSERT INTO WorldTradingDataHistory VALUES(@Symbol, @Price, @OpenPrice, @HighPrice, @LowPrice, " + "@PercentChange, @AvgVolume, @Date) " + "SELECT * FROM WorldTradingData", connection)) { command.Parameters.Add(new SqlParameter("Symbol", stock.Symbol)); command.Parameters.Add(new SqlParameter("Price", stock.Price)); command.Parameters.Add(new SqlParameter("OpenPrice", stock.OpenPrice)); command.Parameters.Add(new SqlParameter("HighPrice", stock.HighPrice)); command.Parameters.Add(new SqlParameter("LowPrice", stock.LowPrice)); command.Parameters.Add(new SqlParameter("PercentChange", stock.PercentChange)); command.Parameters.Add(new SqlParameter("AvgVolume", stock.AvgVolume)); command.Parameters.Add(new SqlParameter("Date", DateTime.Now)); //command.Parameters.Add(new SqlParameter("ScrapeId", stock.ScrapeId)); command.ExecuteNonQuery(); Console.WriteLine($"All stocks successfully moved to History Table"); } Console.WriteLine("The history table been successfully updated."); connection.Close(); } catch (Exception e) { Console.WriteLine("Values could not be inserted into history table"); throw e; } } }