Esempio n. 1
0
        // POST: api/Titles
        public IHttpActionResult Post([FromBody] TitleAdd 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 item...
            var addedItem = m.TitleAdd(newItem);

            // Continue?
            if (addedItem == null)
            {
                return(BadRequest("Cannot add the object"));
            }

            // Return HTTP 201 with the new object in the message (entity) body
            // Notice how to create the URI for the required "Location" header
            var uri = Url.Link("DefaultApi", new { id = addedItem.Id });

            return(Created(uri, addedItem));
        }
Esempio n. 2
0
        public TitleBase TitleAdd(TitleAdd newItem)
        {
            // Ensure that we can continue
            if (newItem == null)
            {
                return(null);
            }
            else
            {
                // Add the new object
                var addedItem = mapper.Map <Title>(newItem);

                ds.Titles.Add(addedItem);
                ds.SaveChanges();

                // Transform and return
                return(mapper.Map <TitleBase>(addedItem));
            }
        }