// PUT: Updates employer given a new employer object and matching id // PATH: api/employer/{id} public HttpResponseMessage PutEmployer(Employer employer, int id) { // Validate request if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (employer.EmployerId != id) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Non-matching Ids")); } // inform context entity has been modified db.Entry(employer).State = EntityState.Modified; // save changes try { db.SaveChanges(); } catch (DBConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT: Replace student with new student object and matching id // PATH: api/student/{id} public HttpResponseMessage PutStudent(Student student, int id) { // validate object and verify matching ids if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (student.StudentId != id) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "INCORRECT IDS")); } // inform context the student entry has been modified db.Entry(student).State = EntityState.Modified; // save changes try { db.SaveChanges(); } catch (DBConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// PUT: Replaces project with new project object with matching id public HttpResponseMessage PutProject(Project project, int id) { // Validate project model and verify matching IDs if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (project.ProjectId != id) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "BAD IDS")); } // inform the context that the entity has been modified db.Entry(project).State = EntityState.Modified; // save changes try { db.SaveChanges(); } catch (DBConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public IHttpActionResult PutDiscipline(int id, Discipline discipline) { // If Discipline model is not valid, throw a bad request if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // If the primary key's don't match, throw a bad request if (id != discipline.DisciplineId) { return(BadRequest()); } // track that the entity has been modified db.Entry(discipline).State = EntityState.Modified; // Save changes, or the id doesn't exist, throw a not found try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!DisciplineExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }