예제 #1
0
        public IActionResult Create(Truck newTruck)
        {
            if (ModelState.IsValid == false)
            {
                // return view instead of redirect so validation errors will be displayed
                return(View("New"));
            }

            // relate the truck to the user who submitted it
            newTruck.UserId = (int)uid;
            db.Trucks.Add(newTruck);
            db.SaveChanges();
            return(RedirectToAction("All"));
        }
예제 #2
0
        public IActionResult Register(User newUser)
        {
            if (ModelState.IsValid)
            {
                // if Any already user exists that matches the email
                if (db.Users.Any(u => u.Email == newUser.Email))
                {
                    ModelState.AddModelError("Email", "is taken");
                }
            }

            if (ModelState.IsValid == false)
            {
                // To display the custom error message above IF it was added, OR to display the other validation errors
                return(View("Index"));
            }

            // hash pw
            PasswordHasher <User> hasher = new PasswordHasher <User>();

            newUser.Password = hasher.HashPassword(newUser, newUser.Password);

            db.Users.Add(newUser);
            db.SaveChanges();

            HttpContext.Session.SetInt32("UserId", newUser.UserId);
            HttpContext.Session.SetString("UserName", newUser.FirstName);
            return(RedirectToAction("All", "Trucks"));
        }
예제 #3
0
        public IActionResult Register(User newUser)
        {
            if (ModelState.IsValid)
            {
                if (db.Users.Any(user => user.Email == newUser.Email))
                {
                    ModelState.AddModelError("Email", "is taken");
                    // re-render the Index view to display manually added error message
                    return(View("Index"));
                }
            }
            else
            {
                // re-render the Index view to display automatically added error messages
                return(View("Index"));
            }

            // if none of the above returns happened, no errors

            PasswordHasher <User> hasher = new PasswordHasher <User>();

            newUser.Password = hasher.HashPassword(newUser, newUser.Password);

            db.Users.Add(newUser);
            db.SaveChanges();

            // add to session, they are now "logged in"
            HttpContext.Session.SetInt32("UserId", newUser.UserId);
            HttpContext.Session.SetString("FirstName", newUser.FirstName);
            return(RedirectToAction("Dashboard", "FoodTrucks"));
        }
예제 #4
0
        public IActionResult Delete(int truckId)
        {
            if (!isLoggedIn)
            {
                return(RedirectToAction("Index", "Home"));
            }

            Truck truck = db.Trucks.FirstOrDefault(t => t.TruckId == truckId);

            if (truck == null || truck.UserId != uid)
            {
                return(RedirectToAction("All"));
            }

            db.Trucks.Remove(truck);
            db.SaveChanges();
            return(RedirectToAction("All"));
        }
        public IActionResult Create(FoodTruck newTruck)
        {
            if (uid == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (ModelState.IsValid)
            {
                if (db.FoodTrucks.Any(truck => truck.Name == newTruck.Name))
                {
                    ModelState.AddModelError("Name", "is taken");
                }
                else
                {
                    newTruck.UserId = (int)uid;
                    db.Add(newTruck);
                    db.SaveChanges();
                    return(RedirectToAction("Dashboard"));
                }
            }
            // above return did not run, so not valid, re-render page to display error messages
            return(View("New"));
        }