/// <summary> /// Update existing /// </summary> /// <param name="updatedSubject">Replacement SubjectFull object</param> /// <returns>SubjectFull object</returns> public SubjectFull UpdateExisting(SubjectFull updatedSubject) { // Fetch the existing Subject object var s = ds.Subjects.Find(updatedSubject.Id); if (s == null) { return(null); } else { // Fetch the object from the data store - ds.Entry(s) // Get its current values collection - .CurrentValues // Set those to the values provided - .SetValues(updatedSubject) ds.Entry(s).CurrentValues.SetValues(updatedSubject); // The SetValues method ignores missing properties, and navigation properties ds.SaveChanges(); return(Mapper.Map <ViewModels.SubjectFull>(s)); } }
/// <summary> /// Update existing /// </summary> /// <param name="updatedSubject">Replacement SubjectFull object</param> /// <returns>SubjectFull object</returns> public SubjectFull UpdateExisting(SubjectFull updatedSubject) { // Fetch the existing Subject object var s = ds.Subjects.Find(updatedSubject.Id); if (s == null) { return null; } else { // Fetch the object from the data store - ds.Entry(s) // Get its current values collection - .CurrentValues // Set those to the values provided - .SetValues(updatedSubject) ds.Entry(s).CurrentValues.SetValues(updatedSubject); // The SetValues method ignores missing properties, and navigation properties ds.SaveChanges(); return Mapper.Map<ViewModels.SubjectFull>(s); } }
// PUT api/subjects/5 // Notice that we're using the SubjectFull type here // We will NOT permit the user to change the Program Id in a PUT request // (later, we'll build a command workflow to handle that need) /// <summary> /// Update an existing Subject object, by its identifier /// </summary> /// <param name="id">Subject object identifier</param> /// <param name="updatedSubject">Updated Subject object in the message body</param> /// <returns>Updated Subject object</returns> public HttpResponseMessage Put(int id, SubjectFull updatedSubject) { if (ModelState.IsValid & id == updatedSubject.Id) { // Update the existing program var s = r.UpdateExisting(updatedSubject); if (s == null) { // If we cannot update the object for some reason // Not sure if this is the best response return Request.CreateResponse(HttpStatusCode.InternalServerError); } else { // Return the updated object return Request.CreateResponse<SubjectFull>(HttpStatusCode.OK, s); } } else { // Not sure if this is the best response return Request.CreateResponse(HttpStatusCode.BadRequest); } }