/// <summary>Occurs when an item is added or removed from the StockEntriesBound collection</summary> private void StockEntriesBound_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { try { LoggingService.Log("Collection changed", "Log.txt"); StockEntry stockChanged = new StockEntry(); SQLStockRepository stockRepo = new SQLStockRepository(); string serverResponse = ""; if (e.OldItems != null) { stockChanged = e.OldItems[0] as StockEntry; serverResponse = stockRepo.DeleteStock(stockChanged.ID); } if (e.NewItems != null) { stockChanged = e.NewItems[0] as StockEntry; serverResponse = stockRepo.AddStockEntry(stockChanged); uint stockID; if (uint.TryParse(serverResponse, out stockID)) { stockChanged.ID = stockID; serverResponse = "Stock entry inserted with ID of " + stockID; } } if (serverResponse != "") { Messages.Items.Insert(0, serverResponse); } } catch (Exception ex) { Messages.Items.Insert(0, ex.Message); LoggingService.Log(ex, "Log.txt"); } }
/// <summary>Occurs when a value in the StockEntriesBound collection is changed</summary> private void StockEntriesBound_ItemPropertyChanged(object sender, PropertyChangedEventArgs e) { try { LoggingService.Log("Collection property changed", "Log.txt"); StockEntry stockChanged = new StockEntry(); SQLStockRepository stockRepo = new SQLStockRepository(); string serverResponse; uint stockID; stockChanged = sender as StockEntry; if (stockChanged.ID == 0) { //Insert stock serverResponse = stockRepo.AddStockEntry(stockChanged); if (uint.TryParse(serverResponse, out stockID)) { stockChanged.ID = stockID; serverResponse = "Stock entry inserted with ID of " + stockID; } } else { //Update stock serverResponse = stockRepo.UpdateStockEntry(stockChanged); } Messages.Items.Insert(0, serverResponse); } catch (Exception ex) { Messages.Items.Insert(0, ex.Message); LoggingService.Log(ex, "Log.txt"); } }
public void AddNewStockEntryTest() { SQLStockRepository stockRepo = new SQLStockRepository(); string expected = "102674"; StockEntry insertStock = new StockEntry() { ID = uint.Parse(0.ToString()), Exchange = "NYSE", Symbol = "AEA", Date = DateTime.Parse("12/10/2017"), Volume = uint.Parse(892800.ToString()) }; insertStock.PriceOpen.Amount = 8.00m; insertStock.PriceClose.Amount = 7.75m; insertStock.PriceHigh.Amount = 8.06m; insertStock.PriceLow.Amount = 7.51m; insertStock.PriceCloseAdjusted.Amount = 6.33m; string actual = stockRepo.AddStockEntry(insertStock); Assert.AreEqual(expected, actual); }