コード例 #1
0
        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());
        }
コード例 #2
0
 private Teacher MapTeacher(TeacherViewModel model)
 {
     return(new Teacher()
     {
         UserId = new Guid(model.UserId)
     });
 }
コード例 #3
0
        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());
        }