/// <summary> /// Inserts stock into database and maps with watchlist /// </summary> /// <param name="stock">the stock data</param> public int insertStockToDepot(StockListitem stock) { int insertID = 0; if (ConnectionState == true) { // Stock exists? Stocks existingItem = null; // Non existing, then insert if (db.Stocks.Any()) { existingItem = db.Stocks.Where(st => st.Symbol == stock.symbol).SingleOrDefault(); db.SubmitChanges(); } if (existingItem == null) { db.Stocks.InsertOnSubmit(new Stocks { Symbol = stock.symbol, Name = stock.name, StockExchange = stock.exch, Typ = stock.typeDisp }); db.SubmitChanges(); existingItem = db.Stocks.FirstOrDefault(st => st.Symbol == stock.symbol); } int stockID = existingItem.StockID; var stocksMap = new StocksMap { Category = 'd', CategoryID = stock.CategoryID, StockID = stockID, Quantity = stock.Quantity, PurchaseDate = DateTime.Now, PurchasePrice = Convert.ToDecimal(stock.PricePurchase), }; // Map stock with watchlist db.StocksMap.InsertOnSubmit(stocksMap); db.SubmitChanges(); insertID = stocksMap.StocksMapID; } return(insertID); }
/// <summary> /// Updates watchlist , e.g. new quantity /// </summary> public void updateStocksMap(StocksMap stocksMap, char category) { if (ConnectionState == true) { var item = (from sm in db.StocksMap where sm.StocksMapID == stocksMap.StocksMapID where sm.Category == category select sm).FirstOrDefault(); if (item != null) { item.Quantity = stocksMap.Quantity; item.PurchasePrice = stocksMap.PurchasePrice; db.SubmitChanges(); } } }
/// <summary> /// Inserts stock into database and maps with watchlist /// </summary> /// <param name="stock">the stock data</param> public int insertStockToWatchlist(StockListitem stock) { int insertID = 0; if (ConnectionState == true) { // Stock exists? Stocks existingItem; // Non existing, then insert existingItem = checkExistingStocksItem(stock); if (existingItem == null) { db.Stocks.InsertOnSubmit(new Stocks { Symbol = stock.symbol, Name = stock.name, StockExchange = stock.exch, Typ = stock.typeDisp }); db.SubmitChanges(); existingItem = checkExistingStocksItem(stock); } int stockID = existingItem.StockID; var stocksMap = new StocksMap { Category = 'w', CategoryID = stock.CategoryID, StockID = stockID, Quantity = stock.Quantity, PurchaseDate = DateTime.Now, PurchasePrice = Convert.ToDecimal(stock.PricePurchase), }; // Map stock with watchlist db.StocksMap.InsertOnSubmit(stocksMap); db.SubmitChanges(); insertID = stocksMap.StocksMapID; } return(insertID); }
/// <summary> /// Saves changes of stock in watchlist /// </summary> private async void btnUpdateStock_Click(object sender, EventArgs e) { if (selectedStock != null) { mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Wertpapier wird gespeichert..."); StocksMap wlm = selectedStock.toMap('w'); wlm.PurchasePrice = Convert.ToInt32(nupPricePurchase.Value); wlm.Quantity = Convert.ToInt32(nupQuantity.Value); Task task = Task.Run(() => model.updateStocksMap(wlm, 'w')); await task; mainForm.notifyUser(FrmMain.NotifyType.PrepareMessage, "Wertpapierdaten werden aktualisiert..."); refreshWatchlistDataAsync(); mainForm.notifyUser(FrmMain.NotifyType.StatusMessage); } }