Exemplo n.º 1
0
        public void PutSetSupervisor(int id, [FromBody] EmployeeSupervisor item)
        {
            // Attention - Employee set supervisor - command pattern

            // Ensure that an "item" is in the entity body
            if (item == null)
            {
                return;
            }

            // Ensure that the id value in the URI matches the id value in the entity body
            if (id != item.EmployeeId)
            {
                return;
            }

            // Ensure that we can use the incoming data
            if (ModelState.IsValid)
            {
                // Attempt to update the item
                m.EmployeeSetSupervisor(item);
            }
            else
            {
                return;
            }
        }
Exemplo n.º 2
0
        public EmployeeWithDetails EmployeeSetSupervisor(EmployeeSupervisor newItem)
        {
            // Attention - Manager - Employee - command to update the self-referencing to-one association

            // Attempt to fetch the object
            // Include associated data (look at the return type from this method)
            var o = ds.Employees.Include("DirectReports").Include("ReportsTo")
                    .SingleOrDefault(e => e.EmployeeId == newItem.EmployeeId);

            // Attempt to fetch the associated object
            // Include associated data (look at the return type from this method)
            Employee a = null;

            if (newItem.ReportsToId > 0)
            {
                a = ds.Employees.Include("DirectReports").Include("ReportsTo")
                    .SingleOrDefault(e => e.EmployeeId == newItem.ReportsToId);
            }

            // Must do two tests here before continuing
            if (o == null | a == null)
            {
                // Problem - one of the items was not found, so return
                return(null);
            }
            else
            {
                // Configure the new supervisor
                // MUST set both properties - the int and the Employee
                o.ReportsTo   = a;
                o.ReportsToId = a.EmployeeId;

                ds.SaveChanges();

                // Prepare and return the object
                return(mapper.Map <EmployeeWithDetails>(o));
            }
        }