예제 #1
0
        public ProjectWithMediaInfo ProjectAddSharer(SharerAdd newItem)
        {
            //The app logic will then cause a new Sharer object to be created.
            var c = ds.Sharers
                    .Include("Project")
                    .Where(i => i.AccessLevel == newItem.AccessLevel && i.Username == newItem.Username && i.Project.Id == newItem.ProjectId);

            // Duplicate found. Return null
            if (c != null)
            {
                return(null);
            }

            // Get proejct reference
            var o = ds.Projects
                    .Include("Medias")
                    .Include("Sharers")
                    .SingleOrDefault(i => i.Id == newItem.ProjectId);

            // Project id is not valid
            if (o == null)
            {
                return(null);
            }

            // Add New item
            var addedItem = Mapper.Map <Sharer>(newItem);

            // Configure the association
            addedItem.Project = o;

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

            // Return the object
            return(Mapper.Map <ProjectWithMediaInfo>(addedItem));
            //Don’t create a duplicate.
        }
        public IHttpActionResult PutSetSharer(int?id, [FromBody] SharerAdd newItem)
        {
            // Ensure that an "editedItem" is in the entity body
            if (newItem == 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() != newItem.ProjectId)
            {
                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.ProjectAddSharer(newItem);

                // Notice the ApiController convenience methods
                if (changedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot edit the object"));
                }
                else
                {
                    // HTTP 200 with the changed item in the entity body
                    return(Ok <ProjectWithMediaInfo>(changedItem));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }