/// <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 public HttpResponseMessage Put(int id, SubjectFull updatedSubject) { return null; 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); } }
/// <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); } }