public IHttpActionResult GetCustomer(int id)
        {
            Customer dbCustomer = db.Customers.Find(id);

            if (dbCustomer == null)
            {
                return NotFound();
            }

            CustomerModel modelCustomer = new CustomerModel
            {
                CustomerId = dbCustomer.CustomerId,
                Address1 = dbCustomer.Address1,
                Address2 = dbCustomer.Address2,
                City = dbCustomer.City,
                CreatedDate = dbCustomer.CreatedDate,
                FirstName = dbCustomer.FirstName,
                LastName = dbCustomer.LastName,
                States = dbCustomer.States,
                Zip = dbCustomer.Zip
            };
           
        
            return Ok(modelCustomer);
        }
        public void PostCustomerCreatesCustomer()
        {
            //Arrange
            var customerController = new CustomersController();

            //Act
            var newCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "123 Main Street",
                Address2 = "Suite 2",
                City = "San Diego",
                States = "CA",
                Zip = "92101"
            };

            //The result of the Post Request
            IHttpActionResult result = customerController.PostCustomer(newCustomer);

            //Assert

            //If not 'true' Assert False
            Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<CustomerModel>));

            //Cast
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;

            //Check if Customer is posted to the database
            //Check to see if Customer ID is NOT equal to zero.  If Customer Id us equal to zero,
            //then customer was NOT added to Database
            Assert.IsTrue(contentResult.Content.CustomerId != 0);
        }
        public void DeleteCustomerDeletesCustomer()
        {
            //Arrange:
            // Instantiate CustomersController so its methods can be called
            // Create a new customer to be deleted, and get its customer ID
            var customerController = new CustomersController();

            var customer = new CustomerModel
            {
                FirstName = "Jim",
                LastName = "McDonald",
                Address1 = "Farm",
                Address2 = "Yard",
                City = "Denver",
                State = "CO",
                Zip = "56432"
            };
            IHttpActionResult result = customerController.PostCustomer(customer);
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;

            int customerIdToDelete = contentResult.Content.CustomerId;

            //Act: Call DeleteCustomer
            result = customerController.DeleteCustomer(customerIdToDelete);

            //Assert:
            // Verify that HTTP result is OK
            // Verify that reading deleted customer returns result not found
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<Customer>));

            result = customerController.GetCustomer(customerIdToDelete);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
        public void PutCustomerUpdatesCustomer()
        {
            //Arrange
            var cusomersContoller = new CustomersController();

            var newCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "123 Main Street",
                Address2 = "Suite 2",
                City = "San Diego",
                States = "CA",
                Zip = "92101"
            };

            //The result of the Post Request
            IHttpActionResult result = cusomersContoller.PostCustomer(newCustomer);

            //Cast result as Content Result so that I can gather information from ContentResult
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;


            //Result contains the customer I had JUST createad
            result = cusomersContoller.GetCustomer(contentResult.Content.CustomerId);

            //Get CustomerModel from 'result'
            OkNegotiatedContentResult<CustomerModel> customerResult = (OkNegotiatedContentResult<CustomerModel>)result;


            //Act
            //The result of the Put Request
            result = cusomersContoller.PutCustomer(customerResult.Content.CustomerId, newCustomer);

            //Assert
            Assert.IsInstanceOfType(result, typeof(StatusCodeResult));

        }
        public IHttpActionResult PutCustomer(int id, CustomerModel customer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != customer.CustomerID)
            {
                return BadRequest();
            }
            var dbPutCustomer = db.Customers.Find(customer.CustomerID);

            dbPutCustomer.Update(customer);

            db.Entry(dbPutCustomer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostCustomer(CustomerModel customer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var dbPostCustomer = new Customer();

            dbPostCustomer.Update(customer);

            db.Customers.Add(dbPostCustomer);

            db.SaveChanges();

            customer.CustomerID = dbPostCustomer.CustomerID;

            return CreatedAtRoute("DefaultApi", new { id = dbPostCustomer.CustomerID }, customer);
        }
        public void DeleteCustomerRecord()
        {
            //Arrange
            //Create Controller
            var customersController = new CustomersController();

            //Create a customer to be deleted
            var dbCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "123 Main Street",
                Address2 = "Suite 2",
                City = "San Diego",
                States = "CA",
                Zip = "92101",
           
            };

            //Add 'new customer' to the DB using a POST
            //Save returned value as RESULT
            IHttpActionResult result = customersController.PostCustomer(dbCustomer);

            //Cast result as Content Result so that I can gather information from ContentResult
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;


            //Result contains the customer I had JUST createad
            result = customersController.GetCustomer(contentResult.Content.CustomerId);

            //Get CustomerModel from 'result'
            OkNegotiatedContentResult<CustomerModel> customerResult = (OkNegotiatedContentResult<CustomerModel>)result;


            //Act
            //The result of the Delete Request
             result = customersController.DeleteCustomer(customerResult.Content.CustomerId);

            //Assert

            //If action returns: NotFound()
            Assert.IsNotInstanceOfType(result, typeof(NotFoundResult));

            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<CustomerModel>));         
        }
        public IHttpActionResult PutCustomer(int id, CustomerModel customer)
        {

            //this checks is everything is good with the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != customer.CustomerId)
            {
                return BadRequest();
            }

            //Mark the object as Modified
            //Update customer in the database
            var dbCustomer = db.Customers.Find(id);

            //update the Database
            dbCustomer.Update(customer);

            //Updates Entries STATE in the Database
            db.Entry(dbCustomer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult DeleteCustomer(int id)
        {
            //Locate Customer from Database
            Customer customer = db.Customers.Find(id);

            //If Customer is not found in Database
            if (customer == null)
            {
                return NotFound();
            }

            var transactions = db.Transactions.Where(t => t.Account.CustomerId == customer.CustomerId);

            db.Transactions.RemoveRange(transactions);

            db.SaveChanges();

            var accounts = db.Accounts.Where(a => a.CustomerId == customer.CustomerId);

            db.Accounts.RemoveRange(accounts);


            db.SaveChanges();

            db.Customers.Remove(customer);

      
            db.SaveChanges();

            //Return model to user
            var customerModel = new CustomerModel
            {
                FirstName = customer.FirstName,
                LastName = customer.LastName,
                Address1 = customer.Address1,
                Address2 = customer.Address2,
                City = customer.City,
                States = customer.States,
                Zip = customer.Zip,
            };

            return Ok(customerModel);
        }
        public IHttpActionResult PostCustomer(CustomerModel customer)
        {
            //If everything is good with the communication
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //Build new Customer
            var dbCustomer = new Customer();

            //Update Customer with new Model
            dbCustomer.Update(customer);
            
            //add Customer model to DB
            db.Customers.Add(dbCustomer);         

            db.SaveChanges();

            customer.CustomerId = dbCustomer.CustomerId;

            return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer);
        }
        public IHttpActionResult GetCustomer(int id)
        {
            // Find Customer object corresponding to customer ID
            Customer dbCustomer = db.Customers.Find(id);

            if (dbCustomer == null)
            {
                return NotFound();
            }

            // Populate new CustomerModel object from Customer object
            CustomerModel modelCustomer = new CustomerModel
            {
                Address1 = dbCustomer.Address1,
                Address2 = dbCustomer.Address2,
                City = dbCustomer.City,
                CreatedDate = dbCustomer.CreatedDate,
                CustomerId = dbCustomer.CustomerId,
                FirstName = dbCustomer.FirstName,
                LastName = dbCustomer.LastName,
                State = dbCustomer.State,
                Zip = dbCustomer.Zip
            };

            return Ok(modelCustomer);
        }
        public IHttpActionResult PutCustomer(int id, CustomerModel customer)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != customer.CustomerId)
            {
                return BadRequest();
            }

            if (!CustomerExists(id))
            {
                return BadRequest();
            }

            //  Get the customer record corresponding to the customer ID,
            //   update its properties to the values in the input CustomerModel object,
            //    and then set an indicator that the record has been modified
            var dbCustomer = db.Customers.Find(id);
            dbCustomer.Update(customer);
            db.Entry(dbCustomer).State = EntityState.Modified;

            // Perform update by saving changes to DB
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw new Exception("Unable to update the customer in the database.");
                }
            }
            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostCustomer(CustomerModel customer)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // Set up new Customer object,
            //  and populate it with the values from
            //  the input CustomerModel object
            Customer dbCustomer = new Customer();
            dbCustomer.Update(customer);

            // Add the new Customer object to the list of Customer objects
            db.Customers.Add(dbCustomer);

            // Save the changes to the DB
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

                throw new Exception("Unable to add the customer to the database.");
            }

            // Update the CustomerModel object with the new customer ID
            //  that was placed in the Customer object after the changes
            //  were saved to the DB
            customer.CustomerId = dbCustomer.CustomerId;
            return CreatedAtRoute("DefaultApi", new { id = dbCustomer.CustomerId }, customer);
        }
        public void PostCustomerCreatesCustomer()
        {
            //Arrange: Instantiate CustomersController so its methods can be called
            var customerController = new CustomersController();

            //Act:
            // Create a CustomerModel object populated with test data,
            //  and call PostCustomer
            var newCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "Land of QA",
                Address2 = "34 Broome St",
                City = "San Francisco",
                State = "CA",
                Zip = "92456"
            };
            IHttpActionResult result = customerController.PostCustomer(newCustomer);

            //Assert:
            // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult
            // Verify that the HTTP result body contains a nonzero customer ID
            Assert.IsInstanceOfType
                (result, typeof(CreatedAtRouteNegotiatedContentResult<CustomerModel>));
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;
            Assert.IsTrue(contentResult.Content.CustomerId != 0);

            // Delete the test customer
            result = customerController.DeleteCustomer(contentResult.Content.CustomerId);
        }