private OperationStatus setupOperationStatus(Person p) { var opStatus = new OperationStatus(); opStatus.Data = p; return opStatus; }
public OperationStatus DeletePerson(Person person) { try { using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { if (person.Phones != null) { foreach (Phone phone in person.Phones) unitOfWork.Phones.Delete(phone); person.Phones = null; } if (person.Addresses != null) { foreach (PersonAddressAssn addressAssn in person.Addresses) unitOfWork.PersonAddressAssns.Delete(addressAssn); person.Addresses = null; } unitOfWork.Persons.Delete(person); unitOfWork.Commit(); } return new OperationStatus { Success = true }; } catch (Exception e) { return OperationStatus.CreateFromException("Error deleting person.", e); } }
public ActionResult New() { var person = new Person(); person.PersonID = 0; person.FirstName = string.Empty; person.LastName = string.Empty; person.EmailAddress = string.Empty; person.Phones = new List<Phone>(); person.Phones.Add(new Phone { PhoneTypeID = 1, Number = string.Empty }); person.Phones.Add(new Phone { PhoneTypeID = 2, Number = string.Empty }); person.Phones.Add(new Phone { PhoneTypeID = 3, Number = string.Empty }); var emptyAddress = new Address { StreetAddress = string.Empty, City = string.Empty, State = string.Empty, Zip = string.Empty }; person.Addresses = new List<PersonAddressAssn>(); person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 1, Address = emptyAddress }); person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 2, Address = emptyAddress }); person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 3, Address = emptyAddress }); ViewBag.States = stateSelectList(); return PartialView("Edit", person); }
public void TestPersonSave() { PersonController personController = new PersonController(mockPersonService.Object); var person = new Person(); person.PersonID = 1; person.FirstName = "Pam"; person.LastName = "Scicchitano"; person.EmailAddress = "*****@*****.**"; ActionResult result = personController.Save(person); // Verify that the result is of type ContentResult Assert.IsInstanceOf<ContentResult>(result); }
public OperationStatus DeletePerson(Person person) { try { using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { unitOfWork.Persons.Delete(person); unitOfWork.Commit(); } return new OperationStatus { Success = true }; } catch (Exception e) { return OperationStatus.CreateFromException("Error deleting person.", e); } }
public ContentResult Save(Person person) { if (person.PersonID == 0) { var opStatus = personService.InsertPerson(person); return Content("The person has been created in the system."); } else { var opStatus = personService.UpdatePerson(person); return Content("The person has been updated"); } }
public OperationStatus UpdatePerson(Person updatedPerson) { try { using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { var person = unitOfWork.Persons.Update(updatedPerson); unitOfWork.Commit(); var json = serializePersonToPersonDocumentJson(person); notificationService.SendPersonUpdatedNotification(person.PersonID, json); return new OperationStatus { Success = true, Data = person }; } } catch (Exception e) { return OperationStatus.CreateFromException("Error updating person.", e); } }
public void TestInsertPerson() { var testUoW = new FootlooseFSTestUnitOfWork(); var mockUoWFactory = new Mock<IFootlooseFSUnitOfWorkFactory>(); var mockNotificationService = new Mock<IFootlooseFSNotificationService>(); // Instead of instantiating a new UoW object in the factory we want to use the one we created so we can monitor // it's internal data and verify that the service did what is was supposed to do mockUoWFactory.Setup(m => m.CreateUnitOfWork()).Returns(testUoW); mockNotificationService.Setup(m => m.SendPersonUpdatedNotification(It.IsAny<int>(), It.IsAny<string>())).Returns(true); var uowFactory = mockUoWFactory.Object; var service = new FootlooseFSService(uowFactory, mockNotificationService.Object); var person = new Person { PersonID = 5, FirstName = "John", LastName = "Dorman", EmailAddress = "*****@*****.**", // Updated email address Phones = new List<Phone> { new Phone { PersonID = 5, Number = "813-657-2222", // Update home phone PhoneType = new PhoneType { Name = "Home", PhoneTypeID = 1 }, PhoneTypeID = 1 } }, Addresses = new List<PersonAddressAssn> { new PersonAddressAssn { Address = new Address { StreetAddress = "823 Newton Drive", // Updated address State = "FL", Zip = "33782", AddressID = 5, City = "Pinellas Park" }, AddressID = 5, AddressType = new AddressType { Name = "Home", AddressTypeID = 1 }, AddressTypeID = 1, PersonID = 5 } }, }; var opStatus = service.InsertPerson(person); var insertedPerson = (Person)opStatus.Data; // Verify that the InsertPerson method returns data of type Person Assert.IsInstanceOf<Person>(insertedPerson); // Get the inserted person object from the UoW var insertedPersonFromUoW = testUoW.Persons.GetQueryable().Where(p => p.PersonID == 5).First(); // Verify that email address was applied Assert.AreEqual(insertedPersonFromUoW.EmailAddress, "*****@*****.**"); // Verify that the home phone number was updated var homePhone = insertedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 1).FirstOrDefault(); Assert.AreEqual(homePhone.Number, "813-657-2222"); // Verify that there is no work number var workPhone = insertedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 2); Assert.AreEqual(workPhone.Count(), 0); // Verify that there is no cell number var cellPhone = insertedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 3); Assert.AreEqual(cellPhone.Count(), 0); // Verify that the address was updated var address = insertedPersonFromUoW.Addresses.Where(a => a.AddressTypeID == 1).FirstOrDefault().Address; Assert.AreEqual(address.StreetAddress, "823 Newton Drive"); Assert.AreEqual(address.City, "Pinellas Park"); Assert.AreEqual(address.Zip, "33782"); Assert.AreEqual(address.State, "FL"); }
public JsonResult Save(FormCollection formCollection) { var person = new Person(); int personID = 0; Int32.TryParse(formCollection["personID"], out personID); person.PersonID = personID; person.FirstName = formCollection["firstName"]; person.LastName = formCollection["lastName"]; person.EmailAddress = formCollection["emailAddress"]; person.Phones = new List<Phone>(); person.Phones.Add(new Phone { PersonID = personID, PhoneTypeID = (int)PhoneTypes.Home, Number = formCollection["homePhone"] }); person.Phones.Add(new Phone { PersonID = personID, PhoneTypeID = (int)PhoneTypes.Work, Number = formCollection["workPhone"] }); person.Phones.Add(new Phone { PersonID = personID, PhoneTypeID = (int)PhoneTypes.Cell, Number = formCollection["cellPhone"] }); person.Addresses = new List<PersonAddressAssn>(); if (!string.IsNullOrEmpty(formCollection["homeStreetAddress"])) { var address = new Address(); int homeAddressID = 0; Int32.TryParse(formCollection["homeAddressID"], out homeAddressID); address.AddressID = homeAddressID; address.StreetAddress = formCollection["homeStreetAddress"]; address.City = formCollection["homeCity"]; address.State = formCollection["homeState"]; address.Zip = formCollection["homeZip"]; person.Addresses.Add(new PersonAddressAssn { PersonID = personID, AddressID = homeAddressID, Address = address, AddressTypeID = (int)AddressTypes.Home }); } if (!string.IsNullOrEmpty(formCollection["workStreetAddress"])) { var address = new Address(); int workAddressID = 0; Int32.TryParse(formCollection["workAddressID"], out workAddressID); address.AddressID = workAddressID; address.StreetAddress = formCollection["workStreetAddress"]; address.City = formCollection["workCity"]; address.State = formCollection["workState"]; address.Zip = formCollection["workZip"]; person.Addresses.Add(new PersonAddressAssn { PersonID = personID, AddressID = workAddressID, Address = address, AddressTypeID = (int)AddressTypes.Work }); } if (!string.IsNullOrEmpty(formCollection["altStreetAddress"])) { var address = new Address(); int altAddressID = 0; Int32.TryParse(formCollection["altAddressID"], out altAddressID); address.AddressID = altAddressID; address.StreetAddress = formCollection["altStreetAddress"]; address.City = formCollection["altCity"]; address.State = formCollection["altState"]; address.Zip = formCollection["altZip"]; person.Addresses.Add(new PersonAddressAssn { PersonID = personID, AddressID = altAddressID, Address = address, AddressTypeID = (int)AddressTypes.Alternate }); } if (person.PersonID == 0) { var opStatus = _footlooseFSService.InsertPerson(person); var newPerson = (Person)opStatus.Data; var savePersonResult = new SavePersonResult { Message = "The person has been created in the system.", Person = newPerson, PersonID = newPerson.PersonID, HomeAddressID = newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).Any() ? newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).First().AddressID : 0, WorkAddressID = newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).Any() ? newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).First().AddressID : 0, AlternateAddressID = newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).Any() ? newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).First().AddressID : 0, }; return Json(savePersonResult); } else { var opStatus = _footlooseFSService.UpdatePerson(person); var updatedPerson = (Person)opStatus.Data; var savePersonResult = new SavePersonResult { Message = "The person has been updated", Person = updatedPerson, PersonID = updatedPerson.PersonID, HomeAddressID = updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).Any() ? updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).First().AddressID : 0, WorkAddressID = updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).Any() ? updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).First().AddressID : 0, AlternateAddressID = updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).Any() ? updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).First().AddressID : 0, }; return Json(savePersonResult); } }
public OperationStatus DeletePerson(Person person) { return footlooseService.DeletePerson(person); }
public void TestUpdatePerson() { var testUoW = new FootlooseFSTestUnitOfWork(); var mockUoWFactory = new Mock<IFootlooseFSUnitOfWorkFactory>(); var mockNotificationService = new Mock<IFootlooseFSNotificationService>(); // Instead of instantiating a new UoW object in the factory we want to use the one we created so we can monitor // it's internal data and verify that the service did what is was supposed to do mockUoWFactory.Setup(m => m.CreateUnitOfWork()).Returns(testUoW); mockNotificationService.Setup(m => m.SendPersonUpdatedNotification(It.IsAny<int>(), It.IsAny<string>())).Returns(true); var uowFactory = mockUoWFactory.Object; var service = new FootlooseFSService(uowFactory, mockNotificationService.Object); var person = new Person { PersonID = 1, FirstName = "Pam", LastName = "Scicchitano", EmailAddress = "*****@*****.**", // Updated email address Phones = new List<Phone> { new Phone { PersonID = 1, Number = "336-418-5555", // Update home phone PhoneType = new PhoneType { Name = "Home", PhoneTypeID = 1 }, PhoneTypeID = 1 }, new Phone { PersonID = 1, Number = "336-418-7777", // Updated work phone PhoneType = new PhoneType { Name = "Work", PhoneTypeID = 2 }, PhoneTypeID = 2 }, new Phone { PersonID = 1, Number = "336-418-4444", // Updated cell phone PhoneType = new PhoneType { Name = "Cell", PhoneTypeID = 3 }, PhoneTypeID = 3 } }, Addresses = new List<PersonAddressAssn> { new PersonAddressAssn { Address = new Address { StreetAddress = "631 Glebe Road", // Updated address State = "VA", Zip = "20178", AddressID = 1, City = "Arlington" }, AddressID = 1, AddressType = new AddressType { Name = "Home", AddressTypeID = 1 }, AddressTypeID = 1, PersonID = 1 } }, }; var opStatus = service.UpdatePerson(person); var updatedPerson = (Person)opStatus.Data; // Verify that the UpdatePerson method returns data of type Person Assert.IsInstanceOf<Person>(updatedPerson); // Get the updated person object from the UoW var updatedPersonFromUoW = testUoW.Persons.GetQueryable().Where(p => p.PersonID == 1).First(); // Verify that email address was updated Assert.AreEqual(updatedPersonFromUoW.EmailAddress, "*****@*****.**"); // Verify that the home phone number was updated var homePhone = updatedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 1).FirstOrDefault(); Assert.AreEqual(homePhone.Number, "336-418-5555"); // Verify that the work number was updated var workPhone = updatedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 2).FirstOrDefault(); Assert.AreEqual(workPhone.Number, "336-418-7777"); // Verify that the cell number was updated var cellPhone = updatedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 3).FirstOrDefault(); Assert.AreEqual(cellPhone.Number, "336-418-4444"); // Verify that the address was updated var address = updatedPersonFromUoW.Addresses.Where(a => a.AddressTypeID == 1).FirstOrDefault().Address; Assert.AreEqual(address.StreetAddress, "631 Glebe Road"); Assert.AreEqual(address.City, "Arlington"); Assert.AreEqual(address.Zip, "20178"); Assert.AreEqual(address.State, "VA"); }
public OperationStatus UpdatePerson(Person person) { return footlooseService.UpdatePerson(person); }
private string serializePersonToPersonDocumentJson(Person person) { var personDocument = new PersonDocument(); personDocument.PersonID = person.PersonID; personDocument.EmailAddress = person.EmailAddress; personDocument.FirstName = person.FirstName; personDocument.LastName = person.LastName; personDocument.PhoneNumber = person.Phones.Any(p => p.PhoneTypeID == 1) ? person.Phones.First(p => p.PhoneTypeID == 1).Number : string.Empty; var address = person.Addresses.Any(a => a.AddressTypeID == 1) ? person.Addresses.First(a => a.AddressTypeID == 1).Address : null; if (address != null) { personDocument.StreetAddress = address.StreetAddress; personDocument.City = address.City; personDocument.State = address.State; personDocument.Zip = address.Zip; } return JsonConvert.SerializeObject(personDocument); }
static public List<Person> GetPersonTestData() { List<Person> persons = new List<Person>(); var person = new Person { PersonID = 1, FirstName = "Pam", LastName = "Scicchitano", EmailAddress = "*****@*****.**", Phones = new List<Phone> { new Phone { PersonID = 1, Number = "336-418-5159", PhoneType = new PhoneType { Name = "Home", PhoneTypeID = 1 }, PhoneTypeID = 1 }, new Phone { PersonID = 1, Number = "336-418-4000", PhoneType = new PhoneType { Name = "Work", PhoneTypeID = 2 }, PhoneTypeID = 2 }, new Phone { PersonID = 1, Number = "336-418-6000", PhoneType = new PhoneType { Name = "Cell", PhoneTypeID = 3 }, PhoneTypeID = 3 } }, Addresses = new List<PersonAddressAssn> { new PersonAddressAssn { Address = new Address { StreetAddress = "38 S Dunworth St #4185", State = "NC", Zip = "27215", AddressID = 1, City = "Raleigh" }, AddressID = 1, AddressType = new AddressType { Name = "Home", AddressTypeID = 1 }, AddressTypeID = 1, PersonID = 1 } }, }; persons.Add(person); person = new Person { PersonID = 2, FirstName = "Dominique", LastName = "Marantz", EmailAddress = "*****@*****.**", Phones = new List<Phone> { new Phone { PersonID = 2, Number = "223-564-5159", PhoneType = new PhoneType { Name = "Home", PhoneTypeID = 1 }, PhoneTypeID = 1 }, new Phone { PersonID = 2, Number = "223-564-4000", PhoneType = new PhoneType { Name = "Work", PhoneTypeID = 2 }, PhoneTypeID = 1 }, new Phone { PersonID = 2, Number = "223-564-6000", PhoneType = new PhoneType { Name = "Cell", PhoneTypeID = 3 }, PhoneTypeID = 1 } }, Addresses = new List<PersonAddressAssn> { new PersonAddressAssn { Address = new Address { StreetAddress = "94 Sunland Court", State = "FL", Zip = "33997", AddressID = 2, City = "Ft. Lauderdale" }, AddressID = 2, AddressType = new AddressType { Name = "Home", AddressTypeID = 1 }, AddressTypeID = 1, PersonID = 2 } }, }; persons.Add(person); return persons; }
private void updatePhone(Person person, Person updatedPerson, int phoneType, IFootlooseFSUnitOfWork unitOfWork) { var updatePhone = updatedPerson.Phones.Where(p => p.PhoneTypeID == phoneType).FirstOrDefault(); if (updatePhone == null) { var phone = person.Phones.Where(p => p.PhoneTypeID == phoneType).FirstOrDefault(); if (phone != null) unitOfWork.Phones.Delete(phone); } else { var phone = person.Phones.Where(p => p.PhoneTypeID == phoneType).FirstOrDefault(); if (phone == null) { phone = new Phone(); phone.PersonID = updatePhone.PersonID; phone.PhoneTypeID = updatePhone.PhoneTypeID; person.Phones.Add(phone); } phone.Number = updatePhone.Number; } }
private void updateAddress(Person person, Person updatedPerson, int addressType, IFootlooseFSUnitOfWork unitOfWork) { var updatedAddressAssn = updatedPerson.Addresses.Where(a => a.AddressTypeID == addressType).FirstOrDefault(); if (updatedAddressAssn == null) { var addressAssn = person.Addresses.Where(a => a.AddressTypeID == addressType).FirstOrDefault(); if (addressAssn != null) { unitOfWork.Addresses.Delete(addressAssn.Address); unitOfWork.PersonAddressAssns.Delete(addressAssn); } } else { var addressAssn = person.Addresses.Where(a => a.AddressTypeID == addressType).FirstOrDefault(); if (addressAssn == null) { addressAssn = new PersonAddressAssn(); addressAssn.PersonID = updatedAddressAssn.PersonID; addressAssn.AddressTypeID = updatedAddressAssn.AddressTypeID; addressAssn.Address = new Address(); person.Addresses.Add(addressAssn); } addressAssn.Address.StreetAddress = updatedAddressAssn.Address.StreetAddress; addressAssn.Address.City = updatedAddressAssn.Address.City; addressAssn.Address.State = updatedAddressAssn.Address.State; addressAssn.Address.Zip = updatedAddressAssn.Address.Zip; } }
public OperationStatus InsertPerson(Person person) { try { // Use the ValidationContext to validate the Product model against the product data annotations // before saving it to the database var validationContext = new ValidationContext(person, serviceProvider: null, items: null); var validationResults = new List<ValidationResult>(); var isValid = Validator.TryValidateObject(person, validationContext, validationResults, true); // If there any exception return them in the return result if (!isValid) { OperationStatus opStatus = new OperationStatus(); opStatus.Success = false; foreach (ValidationResult message in validationResults) { opStatus.Messages.Add(message.ErrorMessage); } return opStatus; } else { // Otherwise connect to the data source using the db context and save the // person to the database using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { unitOfWork.Persons.Add(person); unitOfWork.Commit(); } return new OperationStatus { Success = true, Data = person }; } } catch (Exception e) { return OperationStatus.CreateFromException("Error inserting person.", e); } }
public OperationStatus UpdatePerson(Person updatedPerson) { try { // Use the ValidationContext to validate the Product model against the product data annotations // before saving it to the database var validationContext = new ValidationContext(updatedPerson, serviceProvider: null, items: null); var validationResults = new List<ValidationResult>(); var isValid = Validator.TryValidateObject(updatedPerson, validationContext, validationResults, true); // If there any exception return them in the return result if (!isValid) { OperationStatus opStatus = new OperationStatus(); opStatus.Success = false; foreach (ValidationResult message in validationResults) { opStatus.Messages.Add(message.ErrorMessage); } return opStatus; } else { // Otherwise connect to the data source using the db context and save the // person to the database Person person; using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork()) { person = unitOfWork.Persons.GetQueryable().Where(p => p.PersonID == updatedPerson.PersonID) .Include("Addresses.Address") .Include("Phones") .First(); person.FirstName = updatedPerson.FirstName; person.LastName = updatedPerson.LastName; person.EmailAddress = updatedPerson.EmailAddress; updatePhone(person, updatedPerson, (int)PhoneTypes.Home, unitOfWork); updatePhone(person, updatedPerson, (int)PhoneTypes.Work, unitOfWork); updatePhone(person, updatedPerson, (int)PhoneTypes.Cell, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Home, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Work, unitOfWork); updateAddress(person, updatedPerson, (int)AddressTypes.Alternate, unitOfWork); unitOfWork.Persons.Update(person); unitOfWork.Commit(); } return new OperationStatus { Success = true, Data = person }; } } catch (Exception e) { return OperationStatus.CreateFromException("Error updating person.", e); } }
public void TestInsertAndDeletePerson() { var uowFactory = new FootlooseFSSqlUnitOfWorkFactory(); var notificationService = new FootlooseFSNotificationService(); var service = new FootlooseFSService(uowFactory, notificationService); var person = new Person { FirstName = "John", LastName = "Dorman", EmailAddress = "*****@*****.**", // Updated email address Phones = new List<Phone> { new Phone { Number = "813-657-2222", // Update home phone PhoneTypeID = 1 } }, Addresses = new List<PersonAddressAssn> { new PersonAddressAssn { Address = new Address { StreetAddress = "823 Newton Drive", // Updated address State = "FL", Zip = "33782", AddressID = 5, City = "Pinellas Park" }, AddressTypeID = 1, } }, }; var opStatus = service.InsertPerson(person); var insertedPerson = (Person)opStatus.Data; // Verify that the InsertPerson method returns data of type Person Assert.IsInstanceOf<Person>(insertedPerson); var insertedPersonFromUoW = service.GetPersonById(insertedPerson.PersonID, new PersonIncludes { Phones = true, Addressses = true }); // Verify that email address was applied Assert.AreEqual(insertedPersonFromUoW.EmailAddress, "*****@*****.**"); // Verify that the home phone number was updated var homePhone = insertedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 1).FirstOrDefault(); Assert.AreEqual(homePhone.Number, "813-657-2222"); // Verify that there is no work number var workPhone = insertedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 2 && ! string.IsNullOrEmpty(p.Number)); Assert.AreEqual(workPhone.Count(), 0); // Verify that there is no cell number var cellPhone = insertedPersonFromUoW.Phones.Where(p => p.PhoneTypeID == 3 && !string.IsNullOrEmpty(p.Number)); Assert.AreEqual(cellPhone.Count(), 0); // Verify that the address was updated var address = insertedPersonFromUoW.Addresses.Where(a => a.AddressTypeID == 1).FirstOrDefault().Address; Assert.AreEqual(address.StreetAddress, "823 Newton Drive"); Assert.AreEqual(address.City, "Pinellas Park"); Assert.AreEqual(address.Zip, "33782"); Assert.AreEqual(address.State, "FL"); // Now delete the person service.DeletePerson(insertedPersonFromUoW); // Make sure the person is deleted var deletedPerson = service.GetPersonById(insertedPersonFromUoW.PersonID, new PersonIncludes()); Assert.IsNull(deletedPerson); }
public OperationStatus InsertPerson(Person person) { return footlooseService.InsertPerson(person); }