Beispiel #1
0
        //
        // POST api/values
        public HttpResponseMessage Post([FromBody] CustomerViewModel customer)
        {
            // There is no parameter or it is not passed corrected.
            if (customer == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable));
            }

            // Creating an unit of word cause to use one dbcontext.
            CustomerUnitOfWork unitOfWork = new CustomerUnitOfWork();

            // Mapping ViewModel into Model
            // ** In mapping ViewModel into Model, a new city will be created and we should aware of it.
            DataLayer.Customer entityCustomer = Mapper.Map <CustomerViewModel, DataLayer.Customer>(customer);
            // Get a repository of customer to do CRUD.
            GenericRepository <DataLayer.Customer> repCustomer =
                unitOfWork.GetRepoInstance <Customer.DataLayer.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;

            // assign a city that is existed in database, because city is a new city in mapping and we don't want to create new a city.
            entityCustomer.City = city;

            // Inserting new customer data
            repCustomer.Insert(entityCustomer);
            repCustomer.Save();


            // Response to caller
            //var response = Request.CreateResponse<DataLayer.Customer>(System.Net.HttpStatusCode.Created, entityCustomer);
            var response = Request.CreateResponse(HttpStatusCode.Created);

            return(response);
        }
Beispiel #2
0
        public ActionResult Account(CustomerEdit customerEdit)
        {
            using (var db = new NorthwndEntities())
            {
                // Customer from data base - DataLayer.Customer
                DataLayer.Customer customer = db.Customers.Find(UserAccount.GetUserId());
                //customer.CompanyName = UpdatedCustomer.CompanyName;
                customer.Address      = customerEdit.Address;
                customer.City         = customerEdit.City;
                customer.ContactName  = customerEdit.ContactName;
                customer.ContactTitle = customerEdit.ContactTitle;
                customer.Country      = customerEdit.Country;
                customer.Email        = customerEdit.Email;
                customer.Fax          = customerEdit.Fax;
                customer.Phone        = customerEdit.Phone;
                customer.PostalCode   = customerEdit.PostalCode;
                customer.Region       = customerEdit.Region;


                db.SaveChanges();
            }
            return(RedirectToAction("Index", "Home"));
        }