Example #1
0
        public async Task <IHttpActionResult> PutAdmin(string id, AdminDTOInUp userModel)
        {
            string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;

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

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

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

            return(Ok(updatedAdmin));
        }
Example #2
0
        public async Task <AdminDTOOutUp> UpdateAdmin(string id, AdminDTOInUp adminInUp)
        {
            logger.Info($"Converting admin {adminInUp.UserName} with simple dto converter, user service update admin");
            Admin admin = Utilities.ConverterDTO.SimpleDTOConverter <Admin>(adminInUp);

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

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

            logger.Info($"Updating user {adminInUp.UserName}");
            user.FirstName = adminInUp.FirstName;
            user.LastName  = adminInUp.LastName;
            user.Email     = adminInUp.Email;
            user.UserName  = adminInUp.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(adminInUp.Id);

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

            return(adminDTO);
        }