// GET: api/Employees/5 public IHttpActionResult Get(int id) { // Get by identifier var fetchedObject = m.Employees.GetById(id); if (fetchedObject == null) { return(NotFound()); } else { // Create an object to be returned EmployeeLinked emp = new EmployeeLinked(Mapper.Map <EmployeeWithLink>(fetchedObject), Request.RequestUri.AbsolutePath); // Tell the user what can be done with this item and collection emp.Links[0].Method = "GET,DELETE"; emp.Links[1].Method = "GET,POST"; // TODO maybe refactor this, use the API explorer to discover // (the same API explorer that's used in the HTTP OPTIONS handler // Add another link to tell the user that they can set the supervisor value Link supervisor = new Link() { Rel = "self", Title = "Set the supervisor identifier", Href = emp.Links[0].Href, Method = "PUT", ContentType = "application/json" }; emp.Links.Add(supervisor); // Return the results return(Ok(emp)); } }
// POST: api/Employees 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) { // Attempt to add the new item var addedItem = m.Employees.AddNew(newItem); if (addedItem == null) { return(BadRequest("Cannot add the object")); } else { // HTTP 201 with the new object in the entity body // Build the URI to the new object Uri uri = new Uri(Url.Link("DefaultApi", new { id = addedItem.Id })); // Create an object to be returned EmployeeLinked emp = new EmployeeLinked(Mapper.Map <EmployeeWithLink>(addedItem), uri.AbsolutePath); // Return the object return(Created(uri, emp)); } } else { return(BadRequest(ModelState)); } }