public async Task<IHttpActionResult> PostTeacher(TeacherModel model) { if (!ModelState.IsValid) return BadRequest(ModelState); var newTeacher = new Teacher() { FirstName = model.name, MiddleName = model.middleName, LastName = model.surname, }; _context.Teachers.Add(newTeacher); try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { // if (ex.IsCausedByUniqueConstraintViolation()) throw new ApplicationException("Group with such Key is already exists."); // else // throw; } model.id = newTeacher.Id; return CreatedAtRoute("DefaultApi", new { id = model.id }, model); }
public static TeacherModel FromTeacher(Teacher teacher) { return new TeacherModel { id = teacher.Id, name = teacher.FirstName, middleName = teacher.MiddleName, surname = teacher.LastName }; }