public IActionResult AddEmail(long id) { var customer = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (customer == null) { return(RedirectToAction("List")); } var newEmail = new CustomerEmail { CustomerId = id }; var adding = new AddEditEmailViewModel { AlterEmail = newEmail }; adding = FillEditEmail(customer, adding); if (Request.Headers["X-Requested-With"] == "XMLHttpRequest") { return(PartialView(adding)); } return(View(adding)); }
public IActionResult EditEmail(long id, string em = null) { //make sure customer with id exists var requested = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (ValidCustomerIdAndEmail(requested, id, em) != null) { return(ValidCustomerIdAndEmail(requested, id, em)); } var editModel = new AddEditEmailViewModel(); //make sure there is an attatched email to the customer if (_db.Emails.Any(e => e.CustomerId == id)) { //if there is find the one with the associated email id var requestedEmail = _db.Emails.FirstOrDefault(e => e.Email == em); if (requestedEmail == null) { return(RedirectToAction("Profile", "Customer", new { id = id })); } else { editModel = FillEditEmail(requested, editModel); editModel.AlterEmail = requestedEmail; } } if (Request.Headers["X-Requested-With"] == "XMLHttpRequest") { return(PartialView(editModel)); } return(View(editModel)); }
public IActionResult EditEmail(long id, CustomerEmail editEmail, string em = null) { //make sure customer with id exists var requested = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (ValidCustomerIdAndEmail(requested, id, em) != null) { return(ValidCustomerIdAndEmail(requested, id, em)); } var edited = new AddEditEmailViewModel { AlterEmail = editEmail }; //make sure there is an attatched email to the customer if (_db.Emails.Any(e => e.CustomerId == id)) { //if there is find the one with the associated email id var requestedEmail = _db.Emails.FirstOrDefault(e => e.Email == em); if (requestedEmail == null) { return(RedirectToAction("Profile", "Customer", new { id = id })); } else { var sendBack = FillEditEmail(requested, edited); if (!ModelState.IsValid) { return(View(sendBack)); } if (!string.IsNullOrEmpty(edited.AlterEmail.Email)) { var exists = _db.Emails.FirstOrDefault(e => e.Email == edited.AlterEmail.Email); if (exists == null) { requestedEmail.Email = edited.AlterEmail.Email; _db.SaveChanges(); } else if (exists.Email == em) { requestedEmail.Email = edited.AlterEmail.Email; _db.SaveChanges(); } else { ModelState.AddModelError("", "Email already exists"); return(View(sendBack)); } } } } return(RedirectToAction("Profile", "Customer", new { id = id })); }
//Email View Model filler to fill out customer info private AddEditEmailViewModel FillEditEmail(Customer givenCustomer, AddEditEmailViewModel toFill) { toFill.CusId = givenCustomer.CustomerId; toFill.Given = givenCustomer.FirstName; toFill.Surname = givenCustomer.LastName; toFill.Creation = givenCustomer.DateCreated; toFill.Emails = _db.Emails.Where(e => e.CustomerId == givenCustomer.CustomerId).ToList(); toFill.Phones = _db.PhoneNumbers.Where(p => p.CustomerId == givenCustomer.CustomerId).ToList(); toFill.Addresses = _db.MailingAddresses.Where(m => m.CustomerId == givenCustomer.CustomerId).ToList(); return(toFill); }
public IActionResult AddEmail(long id, CustomerEmail addEmail) { var customer = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (customer == null) { return(RedirectToAction("List")); } var additional = new AddEditEmailViewModel { AlterEmail = addEmail }; // assign in case it doesnt work additional = FillEditEmail(customer, additional); if (!ModelState.IsValid) { return(View(additional)); } if (!string.IsNullOrWhiteSpace(additional.AlterEmail.Email)) { var exists = _db.Emails.FirstOrDefault(e => e.Email == addEmail.Email); if (exists == null) { _db.Add(addEmail); _db.SaveChanges(); } else { ModelState.AddModelError("", "Email already exists"); return(View(additional)); } } return(RedirectToAction("profile", "customer", new { id = id })); }