/// <summary> /// Specific customers details /// </summary> /// <param name="id">Customer Id</param> /// <returns>Details about customers information</returns> // GET: api/Customers/5 public IHttpActionResult Get(int?id) { // Attempt to fetch the object var o = m.CustomerGetById(id.GetValueOrDefault()); // Continue? if (o == null) { return(NotFound()); } else { // Create a hypermedia representation CustomerLinked result = new CustomerLinked (Mapper.Map <CustomerWithLink>(o)); return(Ok(result)); } }
/// <summary> /// Update specific customer's contact information /// </summary> /// <param name="id">Customer Id</param> /// <param name="editedItem">Json form of new contact info</param> /// <returns>Status 200 if success</returns> // PUT: api/Customers/5 public IHttpActionResult Put(int?id, [FromBody] CustomerEditContactInfo 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.CustomerId) { 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.CustomerEditContactInfo(editedItem); // Notice the ApiController convenience methods if (changedItem == null) { // HTTP 400 return(BadRequest("Cannot edit the object")); } else { // Create a hypermedia representation CustomerLinked result = new CustomerLinked (Mapper.Map <CustomerWithLink>(changedItem)); // HTTP 200 with the changed item in the entity body return(Ok(result)); } } else { return(BadRequest(ModelState)); } }
// POST: api/Customers /// <summary> /// Add new customer /// </summary> /// <param name="newItem">New customer object</param> /// <returns>Customer object</returns> public IHttpActionResult Post([FromBody] CustomerAdd 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.CustomerAdd(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.CustomerId }); // Create a hypermedia representation // Attention 27 - This "add new" use case must use the second constructor CustomerLinked result = new CustomerLinked (Mapper.Map <CustomerWithLink>(addedItem), addedItem.CustomerId); return(Created(uri, result)); }