public IHttpActionResult PostTeacher(TeacherModel teacher) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var dbTeacher = new Teacher(); dbTeacher.Update(teacher); db.Teachers.Add(dbTeacher); try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add teacher to the database"); } teacher.TeacherId = dbTeacher.TeacherId; return CreatedAtRoute("DefaultApi", new { id = teacher.TeacherId }, teacher); }
public void Update(TeacherModel teacher) { FirstName = teacher.FirstName; LastName = teacher.LastName; Email = teacher.Email; }
public IHttpActionResult PutTeacher(int id, TeacherModel teacher) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != teacher.TeacherId) { return BadRequest(); } var dbTeacher = db.Teachers.Find(id); dbTeacher.Update(teacher); db.Entry(dbTeacher).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!TeacherExists(id)) { return NotFound(); } else { throw new Exception("Unable to update teacher in the database."); } } return StatusCode(HttpStatusCode.NoContent); }