Example #1
0
        public CustomerBase CustomerEditContactInfo(CustomerEditContactInfo editedItem)
        {
            // Ensure that we can continue
            if (editedItem == null)
            {
                return(null);
            }

            // Attempt to fetch the object
            var storedItem = ds.Customers
                             .Include("Employee")
                             .SingleOrDefault(e => e.CustomerId == editedItem.CustomerId);

            if (storedItem == null)
            {
                return(null);
            }
            else
            {
                // Fetch the object from the data store - ds.Entry(storedItem)
                // Get its current values collection - .CurrentValues
                // Set those to the edited values - .SetValues(editedItem)
                ds.Entry(storedItem).CurrentValues.SetValues(editedItem);
                // The SetValues() method ignores missing properties and navigation properties
                ds.SaveChanges();

                return(Mapper.Map <CustomerBase>(storedItem));
            }
        }
        /// <summary>
        /// Update specific customer's contact information
        /// </summary>
        /// <param name="id">Customer Id</param>
        /// <param name="editedItem">Json form of new contact info</param>
        /// <returns>Status 200 if success</returns>
        // PUT: api/Customers/5
        public IHttpActionResult Put(int?id, [FromBody] CustomerEditContactInfo editedItem)
        {
            // Ensure that an "editedItem" is in the entity body
            if (editedItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            // Ensure that the id value in the URI matches the id value in the entity body
            if (id.GetValueOrDefault() != editedItem.CustomerId)
            {
                return(BadRequest("Invalid data in the entity body"));
            }

            // Ensure that we can use the incoming data
            if (ModelState.IsValid)
            {
                // Attempt to update the item
                var changedItem = m.CustomerEditContactInfo(editedItem);

                // Notice the ApiController convenience methods
                if (changedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot edit the object"));
                }
                else
                {
                    // Create a hypermedia representation
                    CustomerLinked result = new CustomerLinked
                                                (Mapper.Map <CustomerWithLink>(changedItem));

                    // HTTP 200 with the changed item in the entity body
                    return(Ok(result));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }