public IActionResult CreateChef(Chef newChef) { if (ModelState.IsValid) { int age = CalculateAge(newChef.Birthday); if (newChef.Birthday > now) { ModelState.AddModelError("Birthday", "You can't be born in the future!"); return(View("NewChef")); } if (age < 18) { ModelState.AddModelError("Birthday", "Must be at least 18!"); return(View("NewChef")); } newChef.Age = age; dbContext.Add(newChef); dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { return(View("NewChef")); } }
public IActionResult AddUsertoDb(RegisterViewModel Models) { if (ModelState.IsValid) { User email = _context.users.SingleOrDefault(u => u.Email == Models.Email); if (email != null) { ViewBag.ExsitEmail = "Email exsit!"; return(View("Index")); } User NewUser = new User() { Name = Models.Name, Alias = Models.Alias, Email = Models.Email, Password = Models.Password, CreatedAt = Models.CreatedAt, UpdatedAt = Models.UpdatedAt }; _context.Add(NewUser); _context.SaveChanges(); HttpContext.Session.SetInt32("loggedUserId", (int)NewUser.UserId); HttpContext.Session.SetString("loggedUserName", (string)NewUser.Alias); return(RedirectToAction("UserDashboard")); } else { return(View("Index")); } }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (dbContext.Users.Any(user => user.Email == newUser.Email)) { ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); } else { PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); dbContext.Add(newUser); dbContext.SaveChanges(); // Get UserID from newly added user and save to Session User regUser = dbContext.Users.FirstOrDefault(user => user.Email == newUser.Email); HttpContext.Session.SetInt32("UserId", regUser.UserId); return(RedirectToAction("Dashboard")); } } else { return(View("Index")); } }
public IActionResult AddCategory(Brand newBrand) { if (ModelState.IsValid) { Commodity brandCommodity = dbContext.Commodities .FirstOrDefault(p => p.CommodityId == newBrand.CommodityId); Category brandCategory = dbContext.Categories .FirstOrDefault(c => c.CategoryId == newBrand.CategoryId); Brand brand = new Brand(); brand.CommodityId = newBrand.CommodityId; brand.CategoryId = newBrand.CategoryId; brand.commodity = brandCommodity; brand.category = brandCategory; dbContext.Add(brand); dbContext.SaveChanges(); return(Redirect($"/showCommodity/{newBrand.CommodityId}")); } else { Commodity commodity = dbContext.Commodities .FirstOrDefault(p => p.CommodityId == newBrand.CommodityId); ModelState.AddModelError("AssociationProduct", "Could not add product"); return(View("ShowCommodity", commodity)); } }
public IActionResult CreateChef(Chef NewChef) { if (ModelState.IsValid) { dbContext.Add(NewChef); dbContext.SaveChanges(); return(RedirectToAction("NewChef")); } else { return(View("NewChef")); } }
public IActionResult AddChef(Chef newChef) { if (ModelState.IsValid) { dbContext.Add(newChef); dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { return(View("AddChef")); } }
public IActionResult CreateProduct(Product newProduct) { if (ModelState.IsValid) { dbContext.Add(newProduct); dbContext.SaveChanges(); return(RedirectToAction("Products")); } var Products = dbContext.Products .ToList(); ViewBag.Products = Products; return(View("Products")); }
public IActionResult CreateProduct(Product NewProduct) { Console.WriteLine(NewProduct.Name); if (ModelState.IsValid) { dbContext.Add(NewProduct); dbContext.SaveChanges(); return(RedirectToAction("Index")); } List <Product> AllProducts = dbContext.Products .ToList(); ViewBag.Products = AllProducts; return(View("Index")); }
public IActionResult Register(UserViewModel model) { bool hasUser = _context.Users.Any((user) => user.Email == model.Email); if (hasUser) { ViewBag.Exists = "User with that email already exists!"; // TempData["hasUserMessage"] = "User with that email already exists!"; // return RedirectToAction("Index"); } if (ModelState.IsValid && hasUser == false) { User NewUser = new User { FName = model.FName, LName = model.LName, Email = model.Email, Password = model.Password }; // hash password and create a new user PasswordHasher <User> Hasher = new PasswordHasher <User>(); NewUser.Password = Hasher.HashPassword(NewUser, model.Password); _context.Add(NewUser); _context.SaveChanges(); int UserId = _context.Users.Last().UserId; HttpContext.Session.SetInt32("UserId", UserId); return(RedirectToAction("Display", "Main")); } // QUESTION: SHOULD I DO REDRECT OR VIEW? return(View("Index", model)); // return RedirectToAction("Index"); }
public IActionResult CreateActivity(Actvty AddActivity) { if (ModelState.IsValid) { AddActivity.PlannerId = (int)HttpContext.Session.GetInt32("UserId"); dbContext.Add(AddActivity); dbContext.SaveChanges(); Actvty thisActivity = dbContext.Activities.OrderByDescending(w => w.CreatedAt).FirstOrDefault(); return(Redirect("/activity/" + thisActivity.ActvtyId)); } return(View("AddActivity", AddActivity)); }
public IActionResult AddDish(Dish newDish) { if (ModelState.IsValid) { dbContext.Add(newDish); dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { return(View("New")); } }
public IActionResult Create(User newUser) { if (ModelState.IsValid) { if (dbContext.Users.Any(user => user.Email == newUser.Email)) { ModelState.AddModelError("NewUser.Email", "Email already in use!"); return(View("Index")); } else { PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); dbContext.Add(newUser); HttpContext.Session.SetInt32("Id", newUser.UserId); dbContext.SaveChanges(); Console.WriteLine(newUser.UserId); return(Redirect($"account/{newUser.UserId}")); } } return(View("Index")); }
public IActionResult AddWedding(WeddingView weddingModel, int plannerId) { TryValidateModel(weddingModel); if (ModelState.IsValid) { Wedding returnedWedding = _context.weddings.SingleOrDefault(wedding => wedding.userId == plannerId); if (returnedWedding != null) { ModelState.AddModelError("wedderOne", "You already have a wedding registered."); } else { Wedding newWedding = new Wedding { wedder_one = weddingModel.wedderOne, wedder_two = weddingModel.wedderTwo, wedding_date = (DateTime)weddingModel.weddingDate, address = weddingModel.address, created_at = DateTime.Now, updated_at = DateTime.Now, userId = plannerId }; _context.Add(newWedding); _context.SaveChanges(); Wedding weddingInfo = _context.weddings.SingleOrDefault(wedding => wedding.weddingId == newWedding.weddingId); return(RedirectToAction("WeddingInfo", new { weddingId = weddingInfo.weddingId })); } } ViewBag.LoggedUser = ReturnLoggedUser(); ViewBag.DateTimeNow = ReturnCurrentDate(); return(View("WeddingForm", weddingModel)); }
public Box CreateBox(Box box) { DateTime currentDateTime = DateTime.Now; box.DateCreated = currentDateTime; box.DateModified = currentDateTime; _context.Add(box); _context.SaveChanges(); int boxId = box.Id; List <DHT> dhts = _context.DHTs.Where(d => d.DeviceId == box.DHTId).ToList(); List <MotionSensor> motionSensors = _context.MotionSensors.Where(d => d.DeviceId == box.MotionSensorId).ToList(); foreach (DHT dht in dhts) { dht.BoxId = boxId; dht.DateCreated = currentDateTime; dht.DateModified = currentDateTime; _context.Update(dht); _context.SaveChanges(); } foreach (MotionSensor motionSensor in motionSensors) { motionSensor.BoxId = boxId; motionSensor.DateCreated = currentDateTime; motionSensor.DateModified = currentDateTime; _context.Update(motionSensor); _context.SaveChanges(); } _context.SaveChanges(); return(box); }
public IActionResult Create(Truber newTruber) { if (ModelState.IsValid) { newTruber.UserId = (int)HttpContext.Session.GetInt32("UserId"); newTruber.DriverId = null; newTruber.Completed = null; dbContext.Add(newTruber); dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { return(View("TruberNew")); } }
public IActionResult Create(User user) { // Validates form against model for field sanitization if (ModelState.IsValid) { // Checks to see if there is a duplicate email address if (dbContext.Users.Any(u => u.Email == user.Email)) { // Manually adds a ModelState error to the Email field ModelState.AddModelError("Email", "Email is already in use!"); return(View("New")); } // Else creates the user else { // Sets DateTime variables for the new user creation user.CreatedAt = DateTime.Now; user.UpdatedAt = DateTime.Now; // Hashes the password PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, user.Password); // Adds user to the DB dbContext.Add(user); dbContext.SaveChanges(); // Creates 'Logged In' status, with security validation. // Each route can now check to see if the User is logged in using // session data to validate a query to the DB. If the email does // not match the email for the user id, then session will be cleared. HttpContext.Session.SetString("LoggedIn", "Yes"); HttpContext.Session.SetInt32("UserId", user.UserId); HttpContext.Session.SetString("Email", user.Email); return(RedirectToAction("Index", "Weddings")); } } // Else display field errors else { return(View("New")); } }
public IActionResult Register(RegisterView regModel) { Console.WriteLine("registering..."); TryValidateModel(regModel); if (ModelState.IsValid) { Console.WriteLine("form is valid..."); // Get any email that matches user's inputted email from database. User returnedEmails = _context.Users.SingleOrDefault(user => user.Email == regModel.Email); // If match, throw error to login. if (returnedEmails != null) { ModelState.AddModelError("Email", "This email is already registered. Please log in."); } else { Console.WriteLine("hashing password and adding user to db..."); // Hash the user's password. PasswordHasher <RegisterView> hashedPassword = new PasswordHasher <RegisterView>(); regModel.Password = hashedPassword.HashPassword(regModel, regModel.Password); // Save user's input in database. User newUser = new User { Name = regModel.Name, Email = regModel.Email, Password = regModel.Password, Created_at = DateTime.Now, Updated_at = DateTime.Now }; _context.Add(newUser); _context.SaveChanges(); HttpContext.Session.SetInt32("loggedUserID", Convert.ToInt32(newUser.UserId)); return(RedirectToAction("Success")); } } Console.WriteLine("form is not valid..."); return(View("Index")); }
public IActionResult Register(RegisterView regModel) { TryValidateModel(regModel); if (ModelState.IsValid) { // Get any email that matches user's inputted email from database. User returnedEmails = _context.users.SingleOrDefault(user => user.email == regModel.email); // If match, throw error to login. if (returnedEmails != null) { ModelState.AddModelError("email", "This email is already registered. Please log in."); } else { // Hash the user's password. PasswordHasher <RegisterView> hashedPassword = new PasswordHasher <RegisterView>(); regModel.password = hashedPassword.HashPassword(regModel, regModel.password); // Save user's input in database. User newUser = new User { first_name = regModel.firstName, last_name = regModel.lastName, email = regModel.email, password = regModel.password, created_at = DateTime.Now, updated_at = DateTime.Now }; _context.Add(newUser); _context.SaveChanges(); HttpContext.Session.SetInt32("loggedUserID", Convert.ToInt32(newUser.userId)); return(RedirectToAction("WeddingIndex", "Wedding")); } } return(View("Index")); }
public IActionResult Index(ValidateViewModel model) { if (ModelState.IsValid) { Person newUser = new Person { firstName = model.firstName, lastName = model.lastName, email = model.email, created_at = DateTime.Now, updated_at = DateTime.Now }; PasswordHasher <Person> hasher = new PasswordHasher <Person>(); newUser.password = hasher.HashPassword(newUser, model.password); _context.Add(newUser); _context.SaveChanges(); HttpContext.Session.SetInt32("currentUserId", newUser.id); return(RedirectToAction("Users")); } else { return(View(model)); } }