Esempio n. 1
0
        public EmployeeBase EmployeeEditManager(EmployeeEditManager 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);

            var s = ds.Employees
                    .Include("Employee1")
                    .SingleOrDefault(e => e.EmployeeId == editedItem.Employee2EmployeeId);

            // Check s's name vs. Authroized person's name
            // If name is different do not make chenge
            var user    = HttpContext.Current.User as ClaimsPrincipal;
            var surname = user.Claims
                          .SingleOrDefault(c => c.Type == ClaimTypes.Surname)
                          .Value;
            var givenname = user.Claims
                            .SingleOrDefault(c => c.Type == ClaimTypes.GivenName)
                            .Value;

            if (s.FirstName != givenname || s.LastName != surname)
            {
                //Name of requestor is not same as the name on the database
                return(null);
            }


            if (storedItem == null || s == 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)
                s.Employee1.Remove(storedItem);
                ds.Entry(storedItem).CurrentValues.SetValues(editedItem);
                storedItem.Employee2 = s;

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

                return(Mapper.Map <EmployeeBase>(storedItem));
            }
        }
Esempio n. 2
0
        public IHttpActionResult PutChangeManager(int?id, [FromBody] EmployeeEditManager 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.EmployeeEditManager(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));
            }
        }