Ejemplo n.º 1
0
        public IActionResult RegisterUser(RegisterViewModel model)
        {
            PasswordHasher <RegisterViewModel> hasher = new PasswordHasher <RegisterViewModel>();

            if (_dbContext.users.Where(u => u.username == model.username).SingleOrDefault() != null)
            {
                ModelState.AddModelError("username", "Username is taken.");
            }

            if (ModelState.IsValid)
            {
                User newUser = new User()
                {
                    username       = model.username,
                    first_name     = model.first_name,
                    last_name      = model.last_name,
                    password       = hasher.HashPassword(model, model.password),
                    wallet_balance = 1000
                };
                User theUser = _dbContext.Add(newUser).Entity;
                _dbContext.SaveChanges();
                HttpContext.Session.SetInt32("UserId", theUser.user_id);
                return(RedirectToAction("Index", "Dashboard"));
            }
            return(View("Index"));
        }
Ejemplo n.º 2
0
        public IActionResult CreateUser(RegisterUser u)
        {
            if (ModelState.IsValid)
            {
                List <User> users = _context.users.Where(nu => nu.Username == u.Username).ToList();
                if (users.Count > 0)
                {
                    ViewBag.InvalidUsername = true;
                    return(View("Index"));
                }
                PasswordHasher <User> Hasher = new PasswordHasher <User>();

                User nUser = new User();
                nUser.Password  = Hasher.HashPassword(nUser, u.Password);
                nUser.FirstName = u.FirstName;
                nUser.LastName  = u.LastName;
                nUser.Username  = u.Username;
                nUser.Wallet    = 1000.00;

                _context.Add(nUser);
                _context.SaveChanges();

                HttpContext.Session.SetInt32("UserId", nUser.UserId);
                return(Redirect("/dashboard"));
            }
            return(View("Index"));
        }
Ejemplo n.º 3
0
        public IActionResult Register(User user, string PasswordConfirm)
        {
            if (user.Password != PasswordConfirm)
            {
                ViewBag.PasswordError = "Passwords don't match";
                return(View("Index"));
            }
            if (ModelState.IsValid)
            {
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                user.Password = Hasher.HashPassword(user, user.Password);
                User ExistingUsername = _context.users.SingleOrDefault(u => u.Username == user.Username);
                if (ExistingUsername != null)
                {
                    ViewBag.RegError = "Username already registered";
                    return(View("Index"));
                }
                else
                {
                    _context.Add(user);
                    _context.SaveChanges();

                    int?UserId = user.UserId;
                    ViewBag.CurrentUser = user;
                    HttpContext.Session.SetInt32("UserId", (int)UserId);
                    HttpContext.Session.SetString("UserUsername", (string)user.Username);
                    HttpContext.Session.SetString("UserFirstName", (string)user.FirstName);
                    return(RedirectToAction("Dashboard"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }
Ejemplo n.º 4
0
        public IActionResult ProcessBid(float amt, int itemid, int userid)
        {
            if (ActiveUser == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            User         user        = ActiveUser;
            AuctionEvent auctionInfo = _dbContext.auctions
                                       .Where(a => a.auction_id == HttpContext.Session
                                              .GetInt32("ItemId")).SingleOrDefault();

            if (amt == 0)
            {
                TempData["Error"] = "Please specify the amount.";
                return(RedirectToAction("ShowAuction"));
            }
            else if (amt <= auctionInfo.highest_bid)
            {
                TempData["Error"] = "Your bid MUST be greater than the highest bid.";
                return(RedirectToAction("ShowAuction"));
            }
            else if (amt > user.wallet_balance)
            {
                TempData["Error"] = "You don't have enough balance for the bid.";
                return(RedirectToAction("ShowAuction"));
            }
            else
            {
                auctionInfo.highest_bid = amt;
                Bid newBid = new Bid
                {
                    bidder   = user,
                    auctions = auctionInfo,
                };
                if (_dbContext.bids.Where(b => b.auction_id == auctionInfo.auction_id) == null)
                {
                    Bid theBid = _dbContext.Add(newBid).Entity;
                }
                else
                {
                    Bid theBid = _dbContext.Update(newBid).Entity;
                }
                user.wallet_balance -= amt;
                _dbContext.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }