public MediaBase MediaAdd(MediaAdd newItem) { // Ensure that we can continue if (newItem == null) { return(null); } // Must validate the associated object var associatedItem = ds.Projects .SingleOrDefault(i => i.Id == newItem.ProjectId && i.Owner == User.Name); if (associatedItem == null) { return(null); } // Create a new object var addedItem = Mapper.Map <Media>(newItem); // Configure the association addedItem.Project = associatedItem; // Add the user name addedItem.Owner = User.Name; // Save ds.Medias.Add(addedItem); ds.SaveChanges(); // Return the object return(Mapper.Map <MediaBase>(addedItem)); }
// POST: api/Medias /// <summary> /// Add a new Media object /// </summary> /// <param name="newItem">New Media object (the template has the schema)</param> /// <returns>New Media object</returns> public IHttpActionResult Post([FromBody] MediaAdd 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.MediaAdd(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 MediaLinked result = new MediaLinked (Mapper.Map <MediaWithLink>(addedItem), addedItem.Id); return(Created(uri, result)); }
public MediaBase MediaAdd(MediaAdd newItem) { // Ensure that we can continue if (newItem == null) { return(null); } // Must validate the associated object var associatedItem = ds.Projects .SingleOrDefault(i => i.Id == newItem.ProjectId && i.Owner == User.Name); if (associatedItem == null) { // Using the project identifier, attempt to fetch, from the Sharers entity collection, a Sharer object that matches the project identifier, and sharer name var t = ds.Sharers.Include("Project").SingleOrDefault(i => i.Project.Id == newItem.ProjectId && i.Username == User.Name); // If the Sharer object is null, return null if (t == null) { return(null); } // Alternatively, attempt to fetch (again) the matching project object, based only on a match with the project identifier var a = ds.Projects.Include("Medias").Include("Sharers").SingleOrDefault(i => i.Id == t.Project.Id); // If null, return null if (a == null) { return(null); } // Otherwise, attempt to add the new media item // Create a new object var addedItem2 = Mapper.Map <Media>(newItem); // Set its association, ownership, and contributor properties addedItem2.Project = a; addedItem2.Owner = a.Owner; addedItem2.Contributor = User.Name; // Save ds.Medias.Add(addedItem2); ds.SaveChanges(); // Return the media object return(Mapper.Map <MediaBase>(addedItem2)); } // Create a new object var addedItem = Mapper.Map <Media>(newItem); // Configure the association addedItem.Project = associatedItem; // Add the user name addedItem.Owner = User.Name; // Save ds.Medias.Add(addedItem); ds.SaveChanges(); // Return the object return(Mapper.Map <MediaBase>(addedItem)); }