public IHttpActionResult PutChangeDirectReport(int?id, [FromBody] EmployeeEditDirectReport 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.EmployeeId)
            {
                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.EmployeeEditDirectReport(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));
            }
        }
Ejemplo n.º 2
0
        public EmployeeBase EmployeeEditDirectReport(EmployeeEditDirectReport editedItem)
        {
            // Ensure that we can continue
            if (editedItem == null)
            {
                return(null);
            }

            // Attempt to fetch the object
            var storedItem = ds.Employees
                             .Include("Employee1")
                             .Include("Employee2")
                             .SingleOrDefault(e => e.EmployeeId == editedItem.EmployeeId);

            if (storedItem == null)
            {
                return(null);
            }
            else
            {
                var newEmployee1 = new HashSet <Employee>();

                foreach (int Employee1EmployeeId in editedItem.Employee1)
                {
                    if (Employee1EmployeeId == editedItem.EmployeeId)
                    {
                        return(null);
                    }

                    var d = ds.Employees
                            .Include("Employee1")
                            .Include("Employee2")
                            .SingleOrDefault(e => e.EmployeeId == Employee1EmployeeId);

                    if (d == null)
                    {
                        return(null);
                    }

                    newEmployee1.Add(d);
                }

                foreach (var employee1 in storedItem.Employee1)
                {
                    employee1.Employee2 = null;
                }

                foreach (var employee1 in newEmployee1)
                {
                    employee1.Employee2 = storedItem;
                }

                //Assignm new direct report list
                storedItem.Employee1 = newEmployee1;

                // The SetValues() method ignores missing properties and navigation properties
                ds.SaveChanges();

                return(Mapper.Map <EmployeeBase>(storedItem));
            }
        }