public ActionResult Add(PhonebookEditViewModel phonebookEditViewModel) { var contact = new PhoneBook() { Name = phonebookEditViewModel.FirstName, Surname = phonebookEditViewModel.LastName, CreatedDate = DateTime.Now }; _PhoneBookService.Create(contact); var Id = contact.Id; if (Id == 0) { log.Error("Contact not saved. Please try again later!"); Danger("Contact not saved. Please try again later!"); return(View("PhoneBookView", phonebookEditViewModel)); } if (phonebookEditViewModel.Numbers.Count() > 0) { foreach (var entry in phonebookEditViewModel.Numbers) { var phonebookEntry = new PhoneBookEntry() { PhoneBookId = Id, PhoneBookTypeId = Convert.ToInt32(entry.PhoneBookTypeId), Number = entry.Number }; _PhoneBookEntryService.Create(phonebookEntry); Id = phonebookEntry.Id; if (Id == 0) { log.Error($"Contact number {entry.Number} not saved {phonebookEditViewModel.FirstName}!"); Danger($"Contact number {entry.Number} not saved {phonebookEditViewModel.FirstName}!"); return(View("PhoneBookView", phonebookEditViewModel)); } } } Success(string.Format("<b>{0}</b> was successfully saved to the Phonebook.", phonebookEditViewModel.FirstName), true); PhonebookHub.BroadcastData(); return(RedirectToAction("Index", "Phonebook")); }
public void Invalid_Contact_Create() { _phoneBookController.ModelState.AddModelError("Error", "Something went wrong when trynig to save contact to Phonebook!"); _phonebookEditViewModel = new PhonebookEditViewModel { FirstName = "", LastName = "", Numbers = null }; var result = (ViewResult)_phoneBookController.Save(_phonebookEditViewModel); _phonebookServiceMock.Verify(m => m.Create(new PhoneBook()), Times.Never); Assert.AreEqual("", result.ViewName); }
public ActionResult Save(PhonebookEditViewModel phonebookEditViewModel) { try { if (ModelState.IsValid) { var isUpdate = phonebookEditViewModel.Id != 0; if (CheckIfContactAddedANumber(phonebookEditViewModel)) { if (isUpdate) { return(Update(phonebookEditViewModel)); } else { if (!_PhoneBookService.CheckIfContactExists(phonebookEditViewModel.FirstName, phonebookEditViewModel.LastName)) { return(Add(phonebookEditViewModel)); } else { Danger("Contact with the same name already exists."); return(View("PhoneBookView", phonebookEditViewModel)); } } } else { Danger("You have to add atleast 1 number to the phonebook entry"); return(View("PhoneBookView", phonebookEditViewModel)); } } else { Danger("Looks like something went wrong. Model is Invalid."); return(View("PhoneBookView", phonebookEditViewModel)); } } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Phonebook", "Save"))); } }
public ActionResult Update(PhonebookEditViewModel phonebookEditViewModel) { if (phonebookEditViewModel.Numbers.Count() > 0) { foreach (var entry in phonebookEditViewModel.Numbers) { var record = _PhoneBookEntryService.GetById(entry.Id); record.Number = entry.Number; record.PhoneBookTypeId = Convert.ToInt32(entry.PhoneBookTypeId); _PhoneBookEntryService.Update(record); } } Success(string.Format("<b>{0}</b> was successfully updated.", phonebookEditViewModel.FirstName), true); PhonebookHub.BroadcastData(); return(RedirectToAction("Index", "Phonebook")); }
public ActionResult Edit(int id) { try { var contact = _PhoneBookService.GetById(id); if (contact == null) { return(HttpNotFound()); } var numberList = (from pe in _PhoneBookEntryService.GetRecordsByPhoneBookId(contact.Id) join pt in _PhoneBookTypeService.GetAll() on pe.PhoneBookTypeId equals pt.Id select new PhoneNumberEditorViewModel() { Id = pe.Id, Description = pt.Description, PhoneBookTypeId = pe.PhoneBookTypeId.ToString(), Number = pe.Number }).ToList(); var contactDetail = new PhonebookEditViewModel { Id = contact.Id, FirstName = contact.Name, LastName = contact.Surname, Numbers = numberList, ddlPhoneTypes = PopulateNumberTypesDropDownList() }; //Store In Session so can populate in Partial Session["ddlPhoneTypes"] = contactDetail.ddlPhoneTypes; return(View("PhoneBookView", contactDetail)); } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Phonebook", "Edit"))); } }
public void Valid_Contact_Create() { _phoneBookController = new PhonebookTestController(); var contact = new PhoneBook() { Name = "Alwyn", Surname = "Underwood", CreatedDate = DateTime.Now }; _phonebookEditViewModel = new PhonebookEditViewModel { FirstName = contact.Name, LastName = contact.Surname, Numbers = listNumbers }; var result = (RedirectToRouteResult)_phoneBookController.Save(_phonebookEditViewModel); _phonebookServiceMock.Verify(m => m.Create(contact), Times.Once); Assert.AreEqual("Index", result.RouteValues["action"]); }
public bool CheckIfContactAddedANumber(PhonebookEditViewModel phonebookEditViewModel) { return(phonebookEditViewModel.Numbers != null && phonebookEditViewModel.Numbers.Count() > 0); }