public ActionResult Save(CustomerPhoneNumber customerPhoneNumber) { if (!ModelState.IsValid) { var viewModel = new CustomerPhoneNumberFormViewModel { CustomerPhoneNumber = customerPhoneNumber, PhoneNumberTypes = _context.PhoneNumberTypes.ToList() }; return(View("CustomerPhoneNumberForm", viewModel)); } if (customerPhoneNumber.Id == 0) { _context.CustomerPhoneNumbers.Add(customerPhoneNumber); } else { var customerPhoneNumberInDb = _context.CustomerPhoneNumbers.Single(m => m.Id == customerPhoneNumber.Id); customerPhoneNumberInDb.PhoneNumberTypeId = customerPhoneNumber.PhoneNumberTypeId; customerPhoneNumberInDb.PhoneNumber = customerPhoneNumber.PhoneNumber; customerPhoneNumberInDb.PhoneExtension = customerPhoneNumber.PhoneExtension; } _context.SaveChanges(); return(RedirectToAction("Details", "Customers", new { id = customerPhoneNumber.CustomerId })); }
public IActionResult AddPhone(long id, CustomerPhoneNumber newPhone) { var customer = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (customer == null) { return(RedirectToAction("List")); } var addedPhone = new AddEditPhoneViewModel { AlterPhone = newPhone }; addedPhone = FillEditPhone(customer, addedPhone); if (!ModelState.IsValid) { return(View(addedPhone)); } if (!string.IsNullOrWhiteSpace(addedPhone.AlterPhone.PhoneNumber)) { _db.Add(newPhone); _db.SaveChanges(); } return(RedirectToAction("profile", "customer", new { id = id })); }
//Override ToString method public override string ToString() { return ("Customer: " + CustomerName + "\nPhone:" + CustomerPhoneNumber.ToString() + "\nTotal Purchases:" + CalcTotalCost().ToString("c")); }
private void UpdatePhoneNumbers(BizeeBirdDbContext db, Customer customer) { for (int idx = customer.PhoneNumbers.Count() - 1; idx >= 0; idx--) { var dbPhoneNumber = customer.PhoneNumbers[idx]; //TODO I'm not sure this is a very reliable check for new records. shouldn't we add a foreign key to CustomerPhoneNumbers and iterate on that //instead of using customer.PhoneNumbers? if (dbPhoneNumber.PhoneNumberId != 0) { bool inPhoneNumberRows = PhoneNumberRows.Where(pn => pn.PhoneNumberId == dbPhoneNumber.PhoneNumberId).Count() > 0; if (!inPhoneNumberRows) { db.CustomerPhoneNumbers.Remove(dbPhoneNumber); } } } foreach (CustomerDialogPhoneNumberRow row in PhoneNumberRows) { if (row.PhoneNumberId.HasValue) { CustomerPhoneNumber customerPhoneNumber = db.CustomerPhoneNumbers.Find(row.PhoneNumberId.Value); customerPhoneNumber.PhoneNumber = row.getPhoneNumber(); } else { customer.PhoneNumbers.Add(new CustomerPhoneNumber() { PhoneNumber = row.getPhoneNumber() }); } } }
public IActionResult AddPhone(long id) { var customer = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (customer == null) { return(RedirectToAction("List")); } var newPhone = new CustomerPhoneNumber { CustomerId = id }; var adding = new AddEditPhoneViewModel { AlterPhone = newPhone }; adding = FillEditPhone(customer, adding); if (Request.Headers["X-Requested-With"] == "XMLHttpRequest") { return(PartialView(adding)); } return(View(adding)); }
public ActionResult DeleteConfirmed(int id) { CustomerPhoneNumber customerPhoneNumber = db.CustomerPhoneNumber.Find(id); db.CustomerPhoneNumber.Remove(customerPhoneNumber); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "Id,AreaCode,PhoneNumber")] CustomerPhoneNumber customerPhoneNumber) { if (ModelState.IsValid) { db.Entry(customerPhoneNumber).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(customerPhoneNumber)); }
public ActionResult Create([Bind(Include = "Id,AreaCode,PhoneNumber")] CustomerPhoneNumber customerPhoneNumber) { if (ModelState.IsValid) { db.CustomerPhoneNumber.Add(customerPhoneNumber); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(customerPhoneNumber)); }
// GET: CustomerPhoneNumbers/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CustomerPhoneNumber customerPhoneNumber = db.CustomerPhoneNumber.Find(id); if (customerPhoneNumber == null) { return(HttpNotFound()); } return(View(customerPhoneNumber)); }
public IActionResult EditPhone(long id, long ph, CustomerPhoneNumber editPhone) { var requested = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (ValidCustomerIdAndLong(requested, id, ph) != null) { return(ValidCustomerIdAndLong(requested, id, ph)); } var edited = new AddEditPhoneViewModel { AlterPhone = editPhone }; //make sure there is an attatched phone to the customer if (_db.PhoneNumbers.Any(p => p.CustomerId == id)) { //if there is find the one with the associated phone id var requestedPhone = _db.PhoneNumbers.FirstOrDefault(p => p.PhoneId == ph); if (requestedPhone == null) { return(RedirectToAction("Profile", "Customer", new { id = id })); } else { var sendBack = FillEditPhone(requested, edited); if (!ModelState.IsValid) { return(View(sendBack)); } if (!string.IsNullOrWhiteSpace(edited.AlterPhone.PhoneNumber)) { requestedPhone.PhoneNumber = edited.AlterPhone.PhoneNumber; _db.SaveChanges(); } } } return(RedirectToAction("Profile", "Customer", new { id = id })); }
private void AddPhoneNumberRow(bool removeButtonDisabled = false, CustomerPhoneNumber phoneNumber = null) { CustomerDialogPhoneNumberRow row; if (phoneNumber != null) { row = new CustomerDialogPhoneNumberRow(removeButtonDisabled, phoneNumber.PhoneNumberId, phoneNumber.PhoneNumber); } else { row = new CustomerDialogPhoneNumberRow(removeButtonDisabled); } row.addOnAddButtonClicked(delegate { AddPhoneNumberRow(); }); row.addOnRemoveButtonClicked(delegate { RemovePhoneNumberRow(row); }); PhoneNumberRows.Add(row); phoneNumberContainerVbox.Add(row); }
public IActionResult Create(CreateCustomerViewModel added) { if (!ModelState.IsValid) { return(View()); } // add a new customer to database with current time as creation time var addedCustomer = new Customer { FirstName = added.FirstName, LastName = added.LastName, DateCreated = DateTime.Now }; _db.Customers.Add(addedCustomer); _db.SaveChanges(); //save customer to db and find id created so we can work with this data for //other tables related to customer var newestCustomer = _db.Customers.Last(); //make sure there is information in email field and if so save to the database if (!string.IsNullOrWhiteSpace(added.Email)) { var emailToAdd = new CustomerEmail { Email = added.Email, CustomerId = newestCustomer.CustomerId }; _db.Add(emailToAdd); _db.SaveChanges(); } //make sure there is information in phone field and if so save to the database if (!string.IsNullOrWhiteSpace(added.PhoneNumber)) { var phoneToAdd = new CustomerPhoneNumber { PhoneNumber = added.PhoneNumber, CustomerId = newestCustomer.CustomerId }; _db.Add(phoneToAdd); _db.SaveChanges(); } //create a Customer Address model and fill it var addressToAdd = new CustomerAddress { StreetAddress = added.StreetAddress, City = added.City, ProvState = added.ProvState, Country = added.Country, Postal = added.Postal, CustomerId = newestCustomer.CustomerId }; var checkedAddress = AnyAddressFieldsFilled(addressToAdd); //make sure that one field was filled (if not returned null) if (checkedAddress != null) { _db.Add(checkedAddress); _db.SaveChanges(); } return(RedirectToAction("Profile", "customer", new { id = newestCustomer.CustomerId })); }