Ejemplo n.º 1
0
        public IHttpActionResult Post([FromBody] TrackAdd 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.TrackAdd(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.TrackId });

            return(Created(uri, addedItem));
        }
Ejemplo n.º 2
0
        // Add a Track
        public TrackBase TrackAdd(TrackAdd newItem)
        {
            // To add a Track,
            // We must validate that Track first by attempting to fetch it
            // If successful, then we can continue

            var a = ds.Tracks;

            if (a == null)
            {
                return(null);
            }
            else
            {
                // Attempt to add the object
                var addedItem = ds.Tracks.Add(mapper.Map <Track>(newItem));

                ds.SaveChanges();

                // Return the result, or null if there was an error
                return((addedItem == null) ? null : mapper.Map <TrackBase>(addedItem));
            }
        }