/// <summary> /// Update existing /// </summary> /// <param name="updatedProgram">Replacement ProgramBase object</param> /// <returns>ProgramFull object</returns> public ProgramFull UpdateExisting(ProgramBase updatedProgram) { // Fetch the existing Program object var p = ds.Programs.Find(updatedProgram.Id); if (p == null) { return(null); } else { // Fetch the object from the data store - ds.Entry(p) // Get its current values collection - .CurrentValues // Set those to the values provided - .SetValues(updatedProgram) ds.Entry(p).CurrentValues.SetValues(updatedProgram); // The SetValues method ignores missing properties, and navigation properties ds.SaveChanges(); return(Mapper.Map <ViewModels.ProgramFull>(p)); } }
/// <summary> /// Update existing /// </summary> /// <param name="updatedProgram">Replacement ProgramBase object</param> /// <returns>ProgramFull object</returns> public ProgramFull UpdateExisting(ProgramBase updatedProgram) { // Fetch the existing Program object var p = ds.Programs.Find(updatedProgram.Id); if (p == null) { return null; } else { // Fetch the object from the data store - ds.Entry(p) // Get its current values collection - .CurrentValues // Set those to the values provided - .SetValues(updatedProgram) ds.Entry(p).CurrentValues.SetValues(updatedProgram); // The SetValues method ignores missing properties, and navigation properties ds.SaveChanges(); return Mapper.Map<ViewModels.ProgramFull>(p); } }
// PUT api/programs/5 public HttpResponseMessage Put(int id, ProgramBase updatedProgram) { if (ModelState.IsValid & id == updatedProgram.Id) { // Update the existing program var p = r.UpdateExisting(updatedProgram); if (p == 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<ProgramFull>(HttpStatusCode.OK, p); } } else { // Not sure if this is the best response return Request.CreateResponse(HttpStatusCode.BadRequest); } }