// GET api/values/5 public CustomerViewModel Get(int id) { CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork(); Customer.DataLayer.Customer customers = unitOfWork.GetRepoInstance <DataLayer.Customer>().GetById(id); //IEnumerable<CustomerViewModel> customerViewModel = new List<CustomerViewModel>(); var result = Mapper.Map <Customer.DataLayer.Customer, CustomerViewModel>(customers); return(result); }
public void Delete() { int customerID = 16; // Creating an unit of word cause to use one dbcontext. CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork(); GenericRepository <Customer.DataLayer.Customer> repCustomer = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>(); int expctedCount = repCustomer.Count(); Customer.DataLayer.Customer customer = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>().GetAll().Where(c => c.CustomerID == customerID).Single(); unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>().Delete(customer); unitOfWork.SaveChanges(); Assert.AreEqual(--expctedCount, repCustomer.Count()); }
public void Update() { int id = 14; CustomerViewModel customer = new CustomerViewModel(); customer.FirstName = "nameTestUpdate"; customer.LastName = "familyTestUpdate"; customer.CityName = "Tehran"; customer.ProvinceName = "Tehran"; // Creating an unit of word cause to use one dbcontext. CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork(); // Get customer Customer.DataLayer.Customer customerSingle = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>().GetAll().Where(c => c.CustomerID == id).Single(); // Find city and province entity City city = unitOfWork.GetRepoInstance <City>().GetAll().Where(s => s.CityName == customer.CityName).Single(); Province province = unitOfWork.GetRepoInstance <Province>().GetAll().Where(s => s.ProvinceName == customer.ProvinceName).Single(); city.Province = province; customerSingle.FirstName = customer.FirstName; customerSingle.LastName = customer.LastName; customerSingle.City = city; // Get a repository of customer to do CRUD. GenericRepository <Customer.DataLayer.Customer> repCustomer = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>(); // Inserting new customer data repCustomer.Update(customerSingle); repCustomer.Save(); // Get customer customerSingle = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>().GetAll().Where(c => c.CustomerID == id) .Single(); Assert.AreEqual(customer.FirstName, customerSingle.FirstName); }
// DELETE api/values/5 public HttpResponseMessage Delete(int id) { if (id < 1) { return(Request.CreateResponse(HttpStatusCode.NotAcceptable)); } // Creating an unit of word cause to use one dbcontext. CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork(); Customer.DataLayer.Customer customer = unitOfWork.GetRepoInstance <DataLayer.Customer>().GetAll().Where(c => c.CustomerID == id).Single(); unitOfWork.GetRepoInstance <DataLayer.Customer>().Delete(customer); unitOfWork.SaveChanges(); // Response to caller return(Request.CreateResponse(System.Net.HttpStatusCode.OK)); }
public void Insert() { // Arrange: // Web API posts CustomerViewModel via json format and will be deserialized into CustomerViewModel CustomerViewModel customer = new CustomerViewModel(); customer.FirstName = "nameTest"; customer.LastName = "familyTest"; customer.CityName = "Hamedan"; customer.ProvinceName = "Hamedan"; // Creating an unit of work. CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork(); // Mapping ViewModel to Model to provide database format model Customer.DataLayer.Customer entityCustomer = Mapper.Map <CustomerViewModel, Customer.DataLayer.Customer>(customer); // Find city and province entity City city = unitOfWork.GetRepoInstance <City>().GetAll().Where(s => s.CityName == customer.CityName).Single(); Province province = unitOfWork.GetRepoInstance <Province>().GetAll().Where(s => s.ProvinceName == customer.ProvinceName).Single(); city.Province = province; // change city becuase city is a new city in mapping and we don't want to create new city. entityCustomer.City = city; // Creating a customer item. //Customer.DataLayer.Customer c = new Customer.DataLayer.Customer() //{ // FirstName = "test", // LastName = "test", // City = city, //}; GenericRepository <Customer.DataLayer.Customer> repCustomer = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>(); int expctedCount = repCustomer.Count(); repCustomer.Insert(entityCustomer); repCustomer.Save(); Assert.AreEqual(++expctedCount, repCustomer.Count()); }
// PUT api/values/5 public HttpResponseMessage Put(int id, [FromBody] CustomerViewModel customer) { if (id < 1 || customer == null) { return(Request.CreateResponse(HttpStatusCode.NotAcceptable)); } // Creating an unit of word cause to use one dbcontext. CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork(); // Get customer Customer.DataLayer.Customer customerSingle = unitOfWork.GetRepoInstance <DataLayer.Customer>().GetAll().Where(c => c.CustomerID == id).Single(); // Find city and province entity City city = unitOfWork.GetRepoInstance <City>().GetAll().Where(s => s.CityName == customer.CityName).Single(); Province province = unitOfWork.GetRepoInstance <Province>().GetAll().Where(s => s.ProvinceName == customer.ProvinceName).Single(); city.Province = province; customerSingle.FirstName = customer.FirstName; customerSingle.LastName = customer.LastName; customerSingle.City = city; // Get a repository of customer to do CRUD. GenericRepository <DataLayer.Customer> repCustomer = unitOfWork.GetRepoInstance <Customer.DataLayer.Customer>(); // Inserting new customer data repCustomer.Update(customerSingle); repCustomer.Save(); // Response to caller return(Request.CreateResponse(System.Net.HttpStatusCode.OK)); }