Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
858 B
1
Indexable
Never
public UserDTO updateUser(Long id, UserDTO userDTO) {

        User userDB = userRepository.findById(id).get();

        if (Objects.nonNull(userDTO.getEmail()) && !"".equalsIgnoreCase(userDTO.getEmail())) {
            userDB.setEmail(userDTO.getEmail());
        }
        if (Objects.nonNull(userDTO.getPassword()) &&
                !"".equalsIgnoreCase(userDTO.getPassword())) {
            userDB.setPassword(bCryptPasswordEncoder.encode(userDTO.getPassword()));
        }
        if (Objects.nonNull(userDTO.getUsername()) &&
                !"".equalsIgnoreCase(userDTO.getUsername())) {
            userDB.setUsername(userDTO.getUsername());
        }

        userDB.setFirstName(userDTO.getFirstName());
        userDB.setLastName(userDTO.getLastName());
        userRepository.save(userDB);
        return userconverter.entityToDto(userDB);
    }