public async Task <IHttpActionResult> PutTeacher(string id, TeacherDTOInUp userModel)
        {
            string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;

            if (!id.Equals(userModel.Id))
            {
                logger.Info("Id does not match, put teacher, account controller");
                return(BadRequest("Id not match"));
            }

            logger.Info($"Admin {userId} sent {userModel.UserName} to user service, put teacher");
            var updatedTeacher = await service.UpdateTeacher(id, userModel);

            if (updatedTeacher == null)
            {
                logger.Info($"User {userModel.UserName} has not been updated. Update failed, put teacher. Admin {userId}");
                return(NotFound());
            }

            return(Ok(updatedTeacher));
        }
Beispiel #2
0
        public async Task <TeacherDTOOutUp> UpdateTeacher(string id, TeacherDTOInUp teacherInUp)
        {
            logger.Info($"Converting teacher {teacherInUp.UserName} with simple dto converter, user service update teacher");
            Teacher teacher = Utilities.ConverterDTO.SimpleDTOConverter <Teacher>(teacherInUp);

            logger.Info($"getting user with id {teacherInUp.Id}, with db.authreposito, update teacher, user service ");
            AppUser user = await db.AuthRepository.FindUserById(id);

            if (user == null)
            {
                logger.Info("Throwing an user not found exception, no user found update teacher, user service");
                throw new UserNotFoundException($"User with ID {id} does not exists.");
            }

            logger.Info($"Updating user {teacherInUp.UserName}");
            user.FirstName = teacherInUp.FirstName;
            user.LastName  = teacherInUp.LastName;
            user.Email     = teacherInUp.Email;
            user.UserName  = teacherInUp.UserName;

            logger.Info("waiting for result from auth repository, update user, user service");
            var result = await db.AuthRepository.UpdateUser(user);

            if (!result.Succeeded)
            {
                logger.Info("Update has failed, result not succeeded, user service");
                return(null);
            }

            logger.Info("Getting updated user with auth repository");
            var userUpdated = await db.AuthRepository.FindUserById(teacherInUp.Id);

            logger.Info("Converting user to teacher dto");
            TeacherDTOOutUp teacherDTO = Utilities.ConverterDTO.SimpleDTOConverter <TeacherDTOOutUp>(userUpdated);

            return(teacherDTO);
        }