[1ad8e64] | 1 | package finki.diplomska.tripplanner.service.impl;
|
---|
| 2 |
|
---|
| 3 | import finki.diplomska.tripplanner.models.User;
|
---|
| 4 | import finki.diplomska.tripplanner.models.dto.UserDto;
|
---|
| 5 | import finki.diplomska.tripplanner.models.exceptions.UsernameAlreadyExistsException;
|
---|
| 6 | import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
|
---|
| 7 | import finki.diplomska.tripplanner.service.UserService;
|
---|
| 8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[b738035] | 9 | import org.springframework.http.ResponseEntity;
|
---|
[1ad8e64] | 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
| 11 | import org.springframework.stereotype.Service;
|
---|
| 12 |
|
---|
[b738035] | 13 | import java.util.List;
|
---|
[84d0fbb] | 14 | import java.util.Optional;
|
---|
| 15 |
|
---|
[1ad8e64] | 16 |
|
---|
| 17 | @Service
|
---|
| 18 | public class UserServiceImpl implements UserService {
|
---|
| 19 |
|
---|
| 20 | @Autowired
|
---|
| 21 | private JpaUserRepository userRepository;
|
---|
| 22 |
|
---|
| 23 | @Autowired
|
---|
| 24 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
| 25 |
|
---|
| 26 | public User saveUser (User newUser){
|
---|
| 27 | try{
|
---|
| 28 | newUser.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));
|
---|
| 29 | //Username has to be unique (exception)
|
---|
| 30 | newUser.setUsername(newUser.getUsername());
|
---|
| 31 | // Make sure that password and confirmPassword match
|
---|
| 32 | // We don't persist or show the confirmPassword
|
---|
| 33 | newUser.setConfirmPassword("");
|
---|
[84d0fbb] | 34 | return this.userRepository.save(newUser);
|
---|
[1ad8e64] | 35 | }catch(Exception e){
|
---|
| 36 | throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
|
---|
| 37 | }
|
---|
[b738035] | 38 | }
|
---|
| 39 |
|
---|
| 40 | @Override
|
---|
| 41 | public List<String> getAllUsernames() {
|
---|
| 42 | return this.userRepository.getAllUsernames();
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public Optional<String> getPassword(UserDto userDto) {
|
---|
| 47 | this.userRepository.getPassword(userDto.getUsername());
|
---|
| 48 | return null;
|
---|
| 49 | }
|
---|
[1ad8e64] | 50 |
|
---|
[b738035] | 51 | @Override
|
---|
| 52 | public Optional<User> findById(Long id) {
|
---|
| 53 | return this.userRepository.findById(id);
|
---|
[1ad8e64] | 54 | }
|
---|
| 55 |
|
---|
| 56 | }
|
---|