public Dictionary<string, List<Stock>> GetStockHistoryForDownload(string userId) { Dictionary<string, List<Stock>> dict = new Dictionary<string, List<Stock>>(); if (userId != null) { MySqlConnection conn = new MySqlConnection(); conn.CreateConn(); SqlCommand cmd = new SqlCommand("GetStockHistory", conn.Connection); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@UserId", userId)); conn.DataReader = cmd.ExecuteReader(); while (conn.DataReader.Read()) { Stock userStock = new Stock(); string sname = conn.DataReader["StockName"].ToString(); int quantity = Convert.ToInt16(conn.DataReader["Quantity"]); decimal price = Convert.ToDecimal(conn.DataReader["TransactionPrice"]); int hasSold = Convert.ToInt16(conn.DataReader["HasSold"]); if (hasSold == 1) { userStock.SoldPrice = price; userStock.BoughtPrice = 0; } else { userStock.BoughtPrice = price; userStock.SoldPrice = 0; } userStock.Symbol = sname; userStock.NumShares = quantity; if (dict.ContainsKey(sname)) { dict[sname].Add(userStock); } else { List<Stock> userTransactions = new List<Stock>(); userTransactions.Add(userStock); dict.Add(sname, userTransactions); } } } return dict; }
public Stock calculateEarned(List<Stock> transactions) { Stock finalStock = new Stock(); finalStock.NumShares = 0; foreach (Stock s in transactions) { if (s.SoldPrice == 0) { finalStock.SoldPrice -= s.BoughtPrice * s.NumShares; finalStock.NumShares += s.NumShares; } if (s.BoughtPrice == 0) { finalStock.SoldPrice += (s.SoldPrice * s.NumShares); finalStock.NumShares -= s.NumShares; } finalStock.Symbol = s.Symbol; } return finalStock; }
public ActionResult BuyStock(string stock, string buy) { string userId = User.Identity.GetUserId(); Stock StockNote = new Stock(); var transactionType = 0; decimal transPrice = 0; int numTrans = 0; if (stock != null | stock != "") { transPrice = QueryStockPrice(stock); } if (buy != "") { numTrans = Convert.ToInt16(buy); } if (userId != null & numTrans > 0) { MySqlConnection conn = new MySqlConnection(); conn.CreateConn(); SqlCommand cmd = new SqlCommand("CreateStockTransaction", conn.Connection); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@UserId", userId)); cmd.Parameters.Add(new SqlParameter("@StockName", stock.ToUpper())); cmd.Parameters.Add(new SqlParameter("@Quantity", numTrans)); cmd.Parameters.Add(new SqlParameter("@TransactionPrice", transPrice)); cmd.Parameters.Add(new SqlParameter("@HasSold", transactionType)); conn.Command = cmd; conn.Command.Prepare(); conn.Command.ExecuteNonQuery(); } return RedirectToAction("index"); }
public ActionResult GetNote(string stock) { string userId = User.Identity.GetUserId(); string noteString = ""; Stock StockNote = new Stock(); if (userId != null) { MySqlConnection conn = new MySqlConnection(); conn.CreateConn(); SqlCommand cmd = new SqlCommand("GetStockNote", conn.Connection); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@UserId", userId)); cmd.Parameters.Add(new SqlParameter("@StockName", stock)); conn.DataReader = cmd.ExecuteReader(); while (conn.DataReader.Read()) { noteString = conn.DataReader["StockNote"].ToString(); } StockNote.Note = noteString.Trim(); StockNote.Name = stock; } return PartialView(StockNote); }