// Add employee public EmployeeBase EmployeeAdd(EmployeeAdd newItem) { // Attempt to add the object var addedItem = ds.Employees.Add(Mapper.Map <Employee>(newItem)); ds.SaveChanges(); // Return the result, or null if there was an error return((addedItem == null) ? null : Mapper.Map <EmployeeBase>(addedItem)); }
// Attention 17 - Add new // POST: api/Employees /// <summary> /// Add a new employee object /// </summary> /// <param name="newItem">Fully-configured employee</param> /// <returns>New employee object, with store-genereated identifier</returns> public IHttpActionResult Post([FromBody] EmployeeAdd newItem) { // Ensure that the URI is clean (and does not have an id parameter) if (Request.GetRouteData().Values["id"] != null) { return(BadRequest("Invalid request URI")); } // Ensure that a "newItem" is in the entity body if (newItem == null) { return(BadRequest("Must send an entity body with the request")); } // Ensure that we can use the incoming data if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Attempt to add the new object var addedItem = m.EmployeeAdd(newItem); // Continue? if (addedItem == null) { return(BadRequest("Cannot add the object")); } // HTTP 201 with the new object in the entity body // Notice how to create the URI for the Location header var uri = Url.Link("DefaultApi", new { id = addedItem.EmployeeId }); // Create a hypermedia representation // Attention 18 - This "add new" use case must use the second constructor EmployeeLinked result = new EmployeeLinked (Mapper.Map <EmployeeWithLink>(addedItem), addedItem.EmployeeId); return(Created(uri, result)); }