@Service public class UserServiceImpl implements UserService { UserRepository userRepository; BCryptPasswordEncoder passwordEncoder;
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserEntity userEntity = userRepository.findByEmail(username);
if (userEntity == null) { throw new UsernameNotFoundException(username); }
return new User(userEntity.getEmail() , userEntity.getEncryptedPwd() , true , true , true , true , new ArrayList<>()); }
public UserServiceImpl(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) { this.userRepository = userRepository; this.passwordEncoder = passwordEncoder; }
@Override public UserDto createUser(UserDto userDto) { userDto.setUserId(UUID.randomUUID().toString());
ModelMapper modelMapper = new ModelMapper(); modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); UserEntity userEntity = modelMapper.map(userDto, UserEntity.class); userEntity.setEncryptedPwd(passwordEncoder.encode(userDto.getPwd()));
userRepository.save(userEntity);
UserDto returnUserDto = modelMapper.map(userEntity, UserDto.class);
return returnUserDto; }
@Override public UserDto getUserByUserId(String userId) { UserEntity userEntity = userRepository.findByUserId(userId);
if (userEntity == null) throw new UsernameNotFoundException("User not found");
UserDto userDto = new ModelMapper().map(userEntity, UserDto.class);
List<ResponseOrder> orders = new ArrayList<>(); userDto.setOrders(orders);
return userDto; }
@Override public Iterable<UserEntity> getUserByAll() { return userRepository.findAll(); }
@Override public UserDto getUserDetailsByEmail(String email) { UserEntity userEntity = userRepository.findByEmail(email);
if(userEntity == null){ throw new UsernameNotFoundException(email); }
UserDto userDto = new ModelMapper().map(userEntity, UserDto.class); return userDto; } }
|