Exemplo n.º 1
0
        public ActionResult BuyStock(TransactionModel model)
        {
            if (ModelState.IsValid && model.Ticker != "None")
            {
                using (StockContext db = new StockContext())
                {
                    Stock s = getOneStock(model.Ticker);
                    StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = model.Ticker, Price = s.CurrentPrice * -1, Shares = model.Amount, TransactionDate = DateTime.Now };
                    db.StockTransactions.Add(st);
                    db.SaveChanges();
                }
             //   getNetworth();
                //return RedirectToLocal("/Stocks/Index");
                return RedirectToAction("Index", "Stocks");
            }

            // If we got this far, something failed, redisplay form
             //   ModelState.AddModelError("Amount", "The amount provided is invalid.");
             //   return View("Index", model);
            return RedirectToAction("Index", "Stocks");
        }
Exemplo n.º 2
0
        public ActionResult StockInfo(String name)
        {
            if (name != null)
            {
                name = name.Replace(" ", "");
            }

            String oneDay = "http://chart.finance.yahoo.com/z?s=" + name + "&t=1d&q=l&l=on&z=m";
            String oneMonth = "http://chart.finance.yahoo.com/z?s=" + name + "&t=1m&q=l&l=on&z=m";
            String threeMonth = "http://chart.finance.yahoo.com/z?s=" + name + "&t=3m&q=l&l=on&z=m";
            String oneYear = "http://chart.finance.yahoo.com/z?s=" + name + "&t=1y&q=l&l=on&z=m";

               // ViewBag.stock = getOneStock(name);
            TransactionModel model = new TransactionModel();
            if (name != null)
            {
                Debug.WriteLine("The name is " + name);
                Stock s = getOneStock(name.ToUpper());;
                ViewBag.stock = s;
                model.Ticker = s.Ticker;
            }
            else
            {
                Stock dumbStock = new Stock();
                dumbStock.Name = "No name";
                dumbStock.Ticker = "None";
                dumbStock.CurrentPrice = 0;
                dumbStock.HighPrice = 1;
                dumbStock.OpeningPrice = 0;
                dumbStock.Comment = "No comment";
                ViewBag.stock = dumbStock;
                model.Ticker = dumbStock.Ticker;
            }
            ViewBag.oneDay = oneDay;
            ViewBag.oneMonth = oneMonth;
            ViewBag.threeMonth = threeMonth;
            ViewBag.oneYear = oneYear;

            return PartialView(model);
        }
Exemplo n.º 3
0
        public ActionResult SellStock(TransactionModel model)
        {
            if (ModelState.IsValid && model.Ticker != "None" && model.Ticker != "NA")
            {
                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);
                        }

                    }

                    int shares = 0;
                    if (transactionDictonary.ContainsKey(model.Ticker))
                    {

                        foreach (StockTransaction trans in transactionDictonary[model.Ticker])
                        {
                            shares += trans.Shares;
                        }

                        if (shares != 0 && shares > model.Amount)
                        {
                            Stock s = getOneStock(model.Ticker);
                            StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = model.Ticker, Price = s.CurrentPrice, Shares = model.Amount * -1, TransactionDate = DateTime.Now };
                            db.StockTransactions.Add(st);
                            db.SaveChanges();
                        }
                        else if (shares != 0 && shares < model.Amount)
                        {
                            Stock s = getOneStock(model.Ticker);
                            StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = model.Ticker, Price = s.CurrentPrice, Shares = shares * -1, TransactionDate = DateTime.Now };
                            db.StockTransactions.Add(st);
                            db.SaveChanges();
                        }
                    }
                }
                //return RedirectToLocal("/Stocks/Index");
                return RedirectToAction("Index", "Stocks");
            }

            // If we got this far, something failed, redisplay form
              //      ModelState.AddModelError("Amount", "The amount provided is invalid.");
              //  return View("Index", model);
            return RedirectToAction("Index", "Stocks");
        }