public CustomerBase CustomerAdd(CustomerAdd newItem) { // Attempt to add the object var addedItem = ds.Customers.Add(Mapper.Map <Customer>(newItem)); ds.SaveChanges(); // Return the result, or null if there was an error return(CustomerGetById(addedItem.CustomerId)); }
// Attention 26 - Customer add new, returns extra data // Must do a bit more than a normal "add new", because of the association // First, check whether an identifier (for Employee) was provided // It may or may not be there - it's optional // If present, validate, and if valid, must do one extra fetch before return // so that the associated data comes back; if invalid, set it to null // Please be aware that there could be alternative coding plans - // the following plan is not authoritative, and others could work public CustomerWithData CustomerAdd(CustomerAdd newItem) { // Employee object (support rep) Employee associatedItem = null; // Is there a SupportRepId value? if (newItem.SupportRepId != null) { // Attempt to get the associated item associatedItem = ds.Employees.Find(newItem.SupportRepId.GetValueOrDefault()); // If the attempt fails, must remove the invalid support rep identifier if (associatedItem == null) { newItem.SupportRepId = null; } } // Attempt to add the object var addedItem = ds.Customers.Add(Mapper.Map <Customer>(newItem)); ds.SaveChanges(); if (addedItem == null) { return(null); } else { // If associated item is null, just return the added item // Else, do the extra fetch if (associatedItem == null) { return(Mapper.Map <CustomerWithData>(addedItem)); } else { // Set the association addedItem.Employee = associatedItem; ds.SaveChanges(); // Fetch the just-added object, with extra data var o = ds.Customers.Include("Employee") .SingleOrDefault(c => c.CustomerId == addedItem.CustomerId); // Return the result return(Mapper.Map <CustomerWithData>(o)); } } }
/// <summary> /// Add new customer info /// </summary> /// <param name="newItem">Json structured customer information see tempate for details</param> /// <returns>Newly created custoemr information</returns> // POST: api/Customers 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 // ****** Error during automapping ******* //CustomerLinked result = new CustomerLinked(Mapper.Map<CustomerWithLink>(addedItem)); //return Created(uri, result); // ******** error end ************ return(Created(uri, addedItem)); }