// POST: api/Projects
        /// <summary>
        /// Add a new Project object
        /// </summary>
        /// <param name="newItem">New Project object (the template has the schema)</param>
        /// <returns>New Project object</returns>
        public IHttpActionResult Post([FromBody] ProjectAdd 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));
            }

            // TODO - Fix this logic in the future
            // This is a clumsy way to do it, but it's quick
            var visibilityChoices = new List <string> {
                "Private", "Public", "Shared"
            };
            var match = visibilityChoices
                        .SingleOrDefault(v => v.ToLower() == newItem.Visibility.Trim().ToLower());

            if (match == null)
            {
                return(BadRequest("Invalid value for the Visibility property"));
            }
            else
            {
                // Overwrite the visibility property with the proper-case version
                newItem.Visibility = match;
            }

            // Attempt to add the new object
            var addedItem = m.ProjectAdd(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.Id });

            // Use the factory constructor for the "add new" use case
            ProjectLinked result = new ProjectLinked
                                       (Mapper.Map <ProjectWithLink>(addedItem), addedItem.Id);

            return(Created(uri, result));
        }
Exemplo n.º 2
0
        public ProjectBase ProjectAdd(ProjectAdd newItem)
        {
            // Create a new object
            var addedItem = Mapper.Map <Project>(newItem);

            // Add the user name
            addedItem.Owner = User.Name;

            // Save
            ds.Projects.Add(addedItem);
            ds.SaveChanges();

            // Return the object
            return(Mapper.Map <ProjectWithMediaInfo>(addedItem));
        }