public CustomerServiceFixture() { // Set up in memory db options scoped to current test fixture DbContextOptions <CustomerDataContext> options; var builder = new DbContextOptionsBuilder <CustomerDataContext>(); builder.UseInMemoryDatabase(nameof(CustomerServiceFixture)); options = builder.Options; // Create customer db _testDataContext = new CustomerDataContext(options); _testDataContext.Database.EnsureDeleted(); _testDataContext.Database.EnsureCreated(); //Seed with one test record _testRecordGuid = Guid.NewGuid(); _testDataContext.Add(new Customer { Id = _testRecordGuid, FirstName = "Daniel", LastName = "Simpson", DateOfBirth = new DateTime(1988, 1, 5) }); _testDataContext.SaveChanges(); //Create test CustomerService _testCustomerService = new CustomerService(_testDataContext); }
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 })); }