Пример #1
0
        public IActionResult CreateCatagory(CatagoryViewModel newCatagory)
        {
            if (ModelState.IsValid)
            {
                Catagory addCatagory = new Catagory {
                    CatagoryName = newCatagory.CatagoryName
                };

                _context.Add(addCatagory);
                _context.SaveChanges();
            }
            return(RedirectToAction("CatalogAdmin", "Admin"));
        }
        public IActionResult RegisterUser(RegistrationViewModel newUser)
        {
            HttpContext.Session.Clear();

            if (ModelState.IsValid)
            {
                //Check email is not in use.
                User emailChecker = _context.Users.SingleOrDefault(u => u.Email == newUser.Email);

                if (emailChecker != null)
                {
                    //Error Here
                    ModelState.AddModelError("Email", "Email already in use");
                    return(View("Index"));
                }

                User addUser = new User {
                    Email     = newUser.Email,
                    FirstName = newUser.FirstName,
                    LastName  = newUser.LastName,
                    Password  = newUser.Password
                };

                //Hash that Password
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                addUser.Password = Hasher.HashPassword(addUser, newUser.Password);

                // checked to give SuperAdmin
                List <User> AllUsers = _context.Users.ToList();
                if (AllUsers.Count() == 0)
                {
                    addUser.UserLevel = 9;
                }
                else
                {
                    addUser.UserLevel = 1;
                }

                // add User to Database
                _context.Add(addUser);
                _context.SaveChanges();

                //Save the new User to Session and move them to Userlist
                List <User> thisUser = _context.Users.Where(u => u.Email == addUser.Email).ToList();
                HttpContext.Session.SetInt32("UserId", (int)thisUser[0].UserId);

                //Direct new User to ListUsers

                return(RedirectToAction("Shop", "Shop"));
            }

            return(RedirectToAction("Index", "Login"));
        }