コード例 #1
0
        public IActionResult accept(string ProductName, int StartingBid, string Description, DateTime EndDate, int UserId)
        {
            if (HttpContext.Session.GetString("LOGGED_IN_USER") == null)
            {
                return(Redirect("/"));
            }
            string currUser  = HttpContext.Session.GetString("LOGGED_IN_USER");
            var    Logger    = _context.Users.SingleOrDefault(user => user.username == currUser);
            String date      = DateTime.Now.ToString("dd.MM.yyy");
            var    currdate  = DateTime.Now;
            var    TotalDays = (EndDate - currdate).TotalDays;

            Auction newAuction = new Auction
            {
                UserId      = Logger.UserId,
                ProductName = ProductName,
                StartingBid = StartingBid,
                Description = Description,
                EndDate     = EndDate,
                CreatedAt   = DateTime.Now,
                UpdatedAt   = DateTime.Now,
                DaysLeft    = Math.Ceiling(TotalDays)
            };

            _context.Add(newAuction);
            _context.SaveChanges();

            return(Redirect("/dashboard"));
        }
コード例 #2
0
        public IActionResult Register(int UserId, string firstname, string lastname, string username, string password, string confirm, int wallet)
        {
            errors.Clear();  //clear out all errors to begin
            whichErr = null; //reset whichErr
            var checkUser     = _context.Users.SingleOrDefault(user => user.username == username);
            var FindFirstUser = _context.Users.ToList();

            User NewUser = new User
            {
                firstname = firstname,
                lastname  = lastname,
                username  = username,
                password  = password,
                confirm   = confirm
            };


            if (TryValidateModel(NewUser) == false)
            {
                ViewBag.ModelFields = ModelState.Values;
                foreach (var error in ModelState.Values)
                {
                    if (error.Errors.Count > 0)                                  //assuming the Error count is greater than 0
                    {
                        string errorMess = (string)error.Errors[0].ErrorMessage; //create a string to store each error message
                        errors.Add(errorMess);                                   //add that message to the errors list
                        whichErr = "reg";                                        //set whichErr to a login error
                    }
                }

                return(RedirectToAction("Index"));
            }

            if (checkUser != null)                                 //if a user is retrieved based on the entered username..
            {
                errors.Add("User already exists, please log in."); //add this error to the errors list
                whichErr = "reg";                                  //set whichErr to a registration error
                return(RedirectToAction("Index"));
            }
            // otherwise validation passes, redirect to success

            else
            {
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                string hashed = Hasher.HashPassword(NewUser, NewUser.password);

                User NewPerson = new User
                {
                    firstname = firstname,
                    lastname  = lastname,
                    username  = username,
                    password  = hashed,
                    confirm   = hashed,
                    wallet    = 1000
                };
                _context.Add(NewPerson);
                _context.SaveChanges();

                HttpContext.Session.SetString("LOGGED_IN_USER", username);
                string currUser = HttpContext.Session.GetString("LOGGED_IN_USER");

                return(Redirect("dashboard"));
            }
        }