//Save a comment to the database public void SaveComment(String comment, String ticker) { Debug.WriteLine(comment + " " + ticker); String[] test = ticker.Split(' '); Debug.WriteLine(test[test.Length-2]); //this grabs the ticker from the label. String tic = test[test.Length -2]; if (tic.Length < 5 && tic != "None") { using (CommentContext db = new CommentContext()) { StockComment com = new StockComment(); com.FBID = (String)Session["FBID"]; com.Ticker = tic; com.Comment = comment; Debug.WriteLine("WHAT"); if (db.StockComments.Find(com.FBID, com.Ticker) != null) { db.StockComments.Remove(db.StockComments.Find(com.FBID, com.Ticker)); db.StockComments.Add(com); db.SaveChanges(); } else { db.StockComments.Add(com); db.SaveChanges(); } } } }
// Get the list of all stocks that we are currently 'watching' public ActionResult StocksList() { Dictionary<string, List<StockTransaction>> transactionDictonary = new Dictionary<string, List<StockTransaction>>(); List<Stock> viewStockList = new List<Stock>(); using (StockContext db = new StockContext()) { foreach (StockTransaction trans in db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"])) { if(transactionDictonary.ContainsKey(trans.Ticker)){ List<StockTransaction> transactionList = transactionDictonary[trans.Ticker]; transactionList.Add(trans); transactionDictonary[trans.Ticker] = transactionList; } else { List<StockTransaction> transactionList = new List<StockTransaction>(); transactionList.Add(trans); transactionDictonary.Add(trans.Ticker, transactionList); } } foreach(KeyValuePair<string, List<StockTransaction>> pair in transactionDictonary){ Stock stock = getOneStock(pair.Key); stock.Shares = 0; stock.Investment = 0; //add up total shares and investment using stocktransaction history foreach (StockTransaction action in pair.Value) { stock.Shares += action.Shares; stock.Investment += action.Price*Math.Abs(action.Shares); } stock.Investment = Math.Round(stock.Investment, 2); stock.StockValue = Math.Round(stock.Shares * stock.CurrentPrice, 2); stock.NetWorth = Math.Round(stock.StockValue + stock.Investment, 2); viewStockList.Add(stock); } } using(CommentContext db = new CommentContext()) { foreach (StockComment com in db.StockComments.ToList()) { if(com.FBID.Replace(" ","") == ((String)Session["FBID"]).Replace(" ", "")) { if (com.Comment != null && com.Comment != "") { Boolean found = false; foreach (Stock s in viewStockList) { if (s.Ticker.Replace(" ", "") == com.Ticker.Replace(" ", "")) { found = true; } } if (found == false) { Stock stock = getOneStock(com.Ticker); stock.Shares = 0; stock.Investment = 0; viewStockList.Add(stock); } } } } ViewBag.slist = viewStockList; } return PartialView(); }
public Stock getOneStock(String name) { //Currently hard coded from viewing a list of stocks String url = "http://download.finance.yahoo.com/d/quotes.csv?s="; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + name + "&f=nsl1h0o"); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(resp.GetResponseStream()); string result = sr.ReadToEnd(); sr.Close(); Stock stock = new Stock(); using(CommentContext db = new CommentContext()) { StockComment com = db.StockComments.Find((String)Session["FBID"],name); if (com != null && com.Comment != null) { stock.Comment = com.Comment; } else { stock.Comment = ""; } } if (!string.IsNullOrWhiteSpace(result)) { String sn = result.Replace("\"", "").Replace(", ", " "); // Regex.Split(, "\r\n"); string[] stockInfo = sn.Split(','); if (!sn.Contains("N/A") && stockInfo.Length > 4) { stock.Ticker = stockInfo[1]; stock.Name = stockInfo[0]; stock.OpeningPrice = Convert.ToDouble(stockInfo[4]); stock.CurrentPrice = Convert.ToDouble(stockInfo[2]); stock.HighPrice = Convert.ToDouble(stockInfo[3]); } else { stock.Ticker = "NA"; stock.Name = "Not found"; stock.OpeningPrice = 0; stock.CurrentPrice = 0; stock.HighPrice = 0; } } return stock; }