public async Task <IActionResult> CreateTeacher([FromBody] TeacherViewModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var teacher = MapTeacher(model); var user = await _userManager.FindByIdAsync(model.UserId.ToString()); _dataContext.Teachers.Add(teacher); try { await _dataContext.SaveChangesAsync(); await _userManager.AddToRoleAsync(user, "Teacher"); } catch (Exception e) { return(new JsonResult(e) { StatusCode = 500 }); } return(Ok()); }
private Teacher MapTeacher(TeacherViewModel model) { return(new Teacher() { UserId = new Guid(model.UserId) }); }
public async Task <IActionResult> UpdateTeacher([FromBody] TeacherViewModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var teacher = await _dataContext.Teachers.FindAsync(model.Id); await TryUpdateModelAsync(teacher); return(Ok()); }