Exemplo n.º 1
0
 public async Task <IActionResult> Update([FromBody] UpdateAuthUserDTO user)
 {
     try
     {
         return(Ok(await _userAuthService.Update(user)));
     }
     catch (Exception ex)
     {
         return(BadRequest(new { message = ex.Message }));
     }
 }
Exemplo n.º 2
0
        public async Task <UsersDTO> Update(UpdateAuthUserDTO user)
        {
            var userForUpdate = await _userService.GetUserByName(user.UserName);

            if (userForUpdate == null)
            {
                throw new AppException("User not found");
            }
            if (!string.IsNullOrWhiteSpace(userForUpdate.UserName) && userForUpdate.UserName != user.UserName)
            {
                if (await _userService.GetUserByName(user.UserName) != null)
                {
                    throw new AppException("Username " + user.UserName + " is already taken");
                }
                userForUpdate.UserName = user.UserName;
            }
            if (!string.IsNullOrWhiteSpace(user.FirstName))
            {
                userForUpdate.FirstName = user.FirstName;
            }
            if (!string.IsNullOrWhiteSpace(user.LastName))
            {
                userForUpdate.LastName = user.LastName;
            }
            if (!string.IsNullOrWhiteSpace(user.Password))
            {
                byte[] passwordHash, passwordSalt;
                CreatePasswordHash(user.Password, out passwordHash, out passwordSalt);
                userForUpdate.PasswordHash = passwordHash;
                userForUpdate.PasswordSalt = passwordSalt;
            }

            await _userService.UpdateEntity(userForUpdate);

            return(Mapping.Mapper.Map <UsersDTO>(userForUpdate));
        }