// POST: api/JobDuties public IHttpActionResult Post([FromBody] JobDutyAdd 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.JobDuties.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 JobDutyLinked dut = new JobDutyLinked(Mapper.Map <JobDutyWithLink>(addedItem), uri.AbsolutePath); // Return the object return(Created(uri, dut)); } } else { return(BadRequest(ModelState)); } }
// GET: api/JobDuties/5 public IHttpActionResult Get(int id) { // Get by identifier var fetchedObject = m.JobDuties.GetById(id); if (fetchedObject == null) { return(NotFound()); } else { // Create an object to be returned JobDutyLinked dut = new JobDutyLinked(Mapper.Map <JobDutyWithLink>(fetchedObject), Request.RequestUri.AbsolutePath); // Tell the user what can be done with this item and collection dut.Links[0].Method = "GET,DELETE"; dut.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 = dut.Links[0].Href, * Method = "PUT", * ContentType = "application/json" * }; * dut.Links.Add(supervisor); */ // Return the results return(Ok(dut)); } }