Ejemplo n.º 1
0
 public IActionResult Register(RegistrationViewModel model)
 {
     if (ModelState.IsValid)
     {
         // make sure a user with specified username doesn't already exist (this should probably live in Extensions as a custom validation attribute for the registration VM)
         if (_context.Users.SingleOrDefault(u => u.Username == model.RegistrationUsername) == null)
         {
             User user = new User {
                 Username      = model.RegistrationUsername,
                 Password      = BCrypt.Net.BCrypt.HashPassword(model.RegistrationPassword),
                 FirstName     = model.FirstName,
                 LastName      = model.LastName,
                 WalletBalance = 1000.00,
             };
             _context.Users.Add(user);
             _context.SaveChanges();
             HttpContext.Session.SetInt32("UserID", user.ID);
             HttpContext.Session.SetString("FirstName", user.FirstName);
             return(RedirectToAction("Index", "Auction"));
         }
         else
         {
             ModelState.AddModelError("RegistrationUsername", "A user with this username already exists.");
         }
     }
     return(View("Index"));
 }
Ejemplo n.º 2
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.º 3
0
        public void UpdateIndex()
        {
            IList <Auction> updated_auctions = context.auctions
                                               .Where(auction => (auction.state != state.EXPIRED &&
                                                                  auction.state != state.SOLD &&
                                                                  auction.state != state.DELETED))
                                               .Include(auction => auction.bids)
                                               .ToList();

            foreach (Auction auction in updated_auctions)
            {
                if (DateTime.Compare(auction.opens_at, DateTime.Now) < 0 && auction.state == state.DRAFT || DateTime.Compare(auction.closes_at, DateTime.Now) < 0 && auction.price_increase == 0)
                {
                    auction.state = state.EXPIRED;
                }
                else if (DateTime.Compare(auction.opens_at, DateTime.Now) < 0 && auction.state == state.READY)
                {
                    auction.state = state.OPEN;
                }
                else if (auction.state == state.OPEN && DateTime.Compare(auction.closes_at, DateTime.Now) < 0)
                {
                    if (auction.bids != null)
                    {
                        auction.state = state.SOLD;
                    }
                    else
                    {
                        auction.state = state.EXPIRED;
                    }
                }
            }
            context.SaveChanges();
        }
Ejemplo n.º 4
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.º 5
0
        public IActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                User user = new User();

                user.password = Hasher.HashPassword(user, model.password);

                Console.WriteLine("=========== hashed password =======================================");
                Console.WriteLine("==");
                Console.WriteLine(user.password);
                Console.WriteLine("==");
                Console.WriteLine("===================================================================");

                user.username   = model.username;
                user.first_name = model.first_name;
                user.last_name  = model.last_name;
                user.created_at = DateTime.Now;

                int Initial_funding = 1000;
                user.available_funds = Initial_funding;

                _context.users.Add(user);
                _context.SaveChanges();

                int UserId = _context.users.Last().id;

                HttpContext.Session.SetInt32("UserId", UserId);

                return(RedirectToAction("GetAuctions", "RunAuctions"));
            }
            return(View("Index", model));
        }
Ejemplo n.º 6
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.º 7
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"));
            }
        }
Ejemplo n.º 8
0
 public IActionResult Create(CreateAuctionViewModel model)
 {
     if (!isLoggedIn())
     {
         return(RedirectToAction("Index", "Home"));
     }
     if (ModelState.IsValid)
     {
         Auction auction = new Auction {
             ProductName = model.ProductName,
             Description = model.Description,
             StartingBid = model.StartingBid,
             EndDate     = model.EndDate,
             UserID      = (int)HttpContext.Session.GetInt32("UserID"), // safe, since isLoggedIn checks for ID's existence
         };
         _context.Auctions.Add(auction);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View("CreateForm"));
 }
Ejemplo n.º 9
0
        public IActionResult Register(User u)
        {
            if (ModelState.IsValid)
            {
                User checkName = context.Users.SingleOrDefault(us => us.Username == u.Username);

                if (checkName == null)
                {
                    PasswordHasher <User> RegisterHasher = new PasswordHasher <User>();
                    u.Password = RegisterHasher.HashPassword(u, u.Password);

                    u.Wallet    = 1000.00;
                    u.CreatedAt = DateTime.Now;
                    u.UpdatedAt = DateTime.Now;
                    context.Users.Add(u);
                    context.SaveChanges();
                    Console.WriteLine($"User: {u.Username}");

                    // return RedirectToAction("Home");
                }
                HttpContext.Session.SetInt32("UserId", u.UserId);
                return(Redirect("/home"));
            }

            return(View("Index"));
        }
Ejemplo n.º 10
0
        public IActionResult Dashboard()
        {
            int?LogId = HttpContext.Session.GetInt32("UserId");

            if (LogId == null)
            {
                return(Redirect("/"));
            }

            ViewBag.LoggedUserId = LogId;
            User user = _context.users.Where(u => u.UserId == LogId).FirstOrDefault();

            ViewBag.User = user;

            List <Auction> auctions = _context.auctions.Include(a => a.Seller).Include(a => a.Bids).ToList();

            foreach (Auction auction in auctions)
            {
                auction.Bids.OrderBy(b => b.Amount).ToList();///doeesn't actually work
                if (auction.EndDate < DateTime.Now)
                {
                    User   seller = auction.Seller;
                    User   winner = auction.Bids[auction.Bids.Count - 1].Bidder;
                    double amount = auction.Bids[auction.Bids.Count - 1].Amount;
                    winner.Wallet -= amount;
                    seller.Wallet += amount;
                    _context.auctions.Remove(auction);
                    _context.SaveChanges();
                }
            }
            _context.SaveChanges();
            auctions = _context.auctions.Include(a => a.Seller).Include(a => a.Bids).ToList();


            // ViewBag.Auctions = auctions;
            return(View(auctions));
        }
Ejemplo n.º 11
0
        public IActionResult Register(User user)
        {
            // Check initial ModelState
            if (ModelState.IsValid)
            {
                // If a User exists with provided email
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    // Manually add a ModelState error to the Email field, with provided
                    // error message
                    ModelState.AddModelError("Email", "Email already in use!");

                    return(View("LoginReg"));
                }
                PasswordHasher <User> hasher = new PasswordHasher <User>();

                user.Password = hasher.HashPassword(user, user.Password);
                dbContext.Users.Add(user);
                dbContext.SaveChanges();
                HttpContext.Session.SetInt32("logged_in_id", user.UserId);
                return(RedirectToAction("Auctions"));
            }
            return(View("LoginReg"));
        }
Ejemplo n.º 12
0
 public void SaveChanges()
 {
     mObjContext.SaveChanges();
 }
Ejemplo n.º 13
0
        public IActionResult GetAuctions()
        {
            if (Logged_user == null)
            {
                int?CurrUser = HttpContext.Session.GetInt32("UserId");

                ModelState.AddModelError("User", "User not logged in");
                return(RedirectToAction("Login", "User"));
            }

//*************************************************************************
//
//  Update values when an auction ends and is sold
//
//*************************************************************************
            List <Auction> allAuctions = _context.auctions.OrderBy(a => a.ending_at)
                                         .Include(auction => auction.creator)
                                         .Include(auction => auction.auctionBids)
                                         .ToList();

            DateTime currDate = DateTime.Now;

            foreach (Auction a in allAuctions)
            {
                if (currDate < a.ending_at)
                {
                    break;
                }
                else
                {
                    if (a.sold_for == 0)
                    {
                        List <Bid> allBids    = _context.bids.Where(b => b.auctionid == a.id).OrderByDescending(b => b.amount).ToList();
                        Bid        highBid    = new Bid();
                        User       highBidder = new User();
                        if (allBids.Count() > 0)
                        {
                            highBid    = allBids.First();
                            highBidder = _context.users.SingleOrDefault(u => u.id == highBid.userid);

                            highBidder.available_funds = highBidder.available_funds - highBid.amount;
                            _context.users.Update(highBidder);

                            a.creator.available_funds = a.creator.available_funds + highBid.amount;
                            _context.users.Update(a.creator);

                            a.sold_for = highBid.amount;
                            _context.auctions.Update(a);

                            _context.SaveChanges();
                        }
                    }
                }
            }

//*************************************************************************
//
//  display auctions page
//
//*************************************************************************
            ListAuctionsViewModel viewModel = new ListAuctionsViewModel()
            {
                CurrentAuctions = allAuctions,
                Current_user    = Logged_user
            };


            return(View("ListAuctions", viewModel));
        }