Beispiel #1
0
        public EmployeeBase EmployeeEditNames(EmployeeEditNames editedItem)
        {
            // Ensure that we can continue
            if (editedItem == null)
            {
                return(null);
            }

            // Attempt to fetch the underlying object
            var storedItem = ds.Employees.Find(editedItem.Id);

            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 <EmployeeBase>(storedItem));
            }
        }
        public EmployeeBase EditEmployeeNames(EmployeeEditNames editedItem)
        {
            // Ensure that we can continue
            if (editedItem == null)
            {
                return null;
            }

            // Attempt to fetch the underlying object
            var storedItem = ds.Employees.Find(editedItem.Id);

            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<EmployeeBase>(storedItem);
            }
        }
Beispiel #3
0
        // PUT: api/Employees/5
        public IHttpActionResult Put(int id, [FromBody] EmployeeEditNames 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 != editedItem.Id)
            {
                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.EmployeeEditNames(editedItem);

                // Notice the ApiController convenience methods
                if (changedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot edit the object"));
                }
                else
                {
                    // HTTP 200 with the changed item in the entity body
                    return(Ok <EmployeeBase>(changedItem));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }