コード例 #1
0
        public IActionResult Watchlist()
        {
            // Nav Bar will be checking to see if ViewBag.Id is valid
            var SessionId = HttpContext.Session.GetInt32("LoggedUserId");

            ViewBag.Id = SessionId;
            // Retreive id from Session for User query
            int?id = HttpContext.Session.GetInt32("LoggedUserId");

            if (id == null)
            {
                return(RedirectToAction("Index", "User"));
            }

            // Retreive current User and Portfolio from the database
            User      User      = _context.Users.SingleOrDefault(u => u.Id == (int)id);
            Watchlist Watchlist = _context.Watchlists
                                  .Include(p => p.Stocks)
                                  .SingleOrDefault(p => p.User == User);

            // For each Stock in Portfolio, call API based on values in database
            // Also, populate Stocks list for later use in ViewBag
            ViewBag.Total = 0;
            foreach (Stock Stock in Watchlist.Stocks)
            {
                // Create a Dictionary object to store JSON values from API call
                Dictionary <string, object> Data = new Dictionary <string, object>();

                // Make API call
                WebRequest.GetQuote(Stock.Symbol, JsonResponse =>
                {
                    Data = JsonResponse;
                }
                                    ).Wait();

                // We can save each Dictionary, containing each stock, to a List, then pass that List to the View using ViewBag.
                //This will allow more flexibility when making changes and will reduce the number of collumns in our DB table.


                // Define values for each stock to be stored in ViewBag
                double CurrentPrice = Convert.ToDouble(Data["latestPrice"]);

                Stock.Name         = (string)Data["companyName"];
                Stock.CurrentPrice = CurrentPrice;
                Stock.Week52Low    = Convert.ToDouble(Data["week52Low"]);
                Stock.Week52High   = Convert.ToDouble(Data["week52High"]);
                Stock.UpdatedAt    = DateTime.Now;

                _context.SaveChanges();

                ViewBag.Total += Stock.CurrentValue;
            }

            // Store values in ViewBag for Portfolio page rendering
            ViewBag.Watchlist = Watchlist;
            ViewBag.User      = User;
            return(View("watchlist"));
        }
コード例 #2
0
        public IActionResult NewUser(AllUserViewModels model)
        {
            // Check if models received any validation errors.
            if (ModelState.IsValid)
            {
                try
                {
                    // Check if email already exists in DB.
                    var EmailExists = _context.Users.Where(e => e.Email == model.Reg.Email).SingleOrDefault();
                    // If email is unique, perform registration.
                    if (EmailExists == null)
                    {
                        // Hash and store password in DB.
                        PasswordHasher <RegisterViewModel> Hasher = new PasswordHasher <RegisterViewModel>();
                        string HashedPassword = Hasher.HashPassword(model.Reg, model.Reg.Password);

                        User NewUser = new User
                        {
                            FirstName = model.Reg.FirstName,
                            LastName  = model.Reg.LastName,
                            Email     = model.Reg.Email,
                            Password  = HashedPassword,
                            CreatedAt = DateTime.Now,
                            UpdatedAt = DateTime.Now,
                        };
                        Portfolio Portfolio = new Portfolio
                        {
                            User      = NewUser,
                            CreatedAt = DateTime.Now,
                            UpdatedAt = DateTime.Now,
                        };
                        Watchlist Watchlist = new Watchlist
                        {
                            User      = NewUser,
                            CreatedAt = DateTime.Now,
                            UpdatedAt = DateTime.Now,
                        };
                        _context.Add(NewUser);
                        _context.Add(Portfolio);
                        _context.Add(Watchlist);
                        _context.SaveChanges();

                        // Set user id in session for use in identification, future db calls, and for greeting the user.
                        HttpContext.Session.SetInt32("LoggedUserId", NewUser.Id);

                        // Redirect to Profile method.
                        return(RedirectToAction("Profile"));
                    }
                    // Redirect w/ error if email already exists in db.
                    else
                    {
                        ViewBag.email = "That email is already in use. Please try again using a different one.";
                        MostActiveStocksAPICall();
                        return(View("landing"));
                    }
                }
                // Catch should only run if there was an error with the password hashing or storing on the new user in the DB.
                catch
                {
                    MostActiveStocksAPICall();
                    return(View("landing"));
                }
            }
            // Else statement will run if the ModelState is invalid.
            else
            {
                MostActiveStocksAPICall();
                return(View("landing"));
            }
        }