| 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;
|
|---|
| 9 | import org.springframework.http.ResponseEntity;
|
|---|
| 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|---|
| 11 | import org.springframework.stereotype.Service;
|
|---|
| 12 |
|
|---|
| 13 | import java.util.List;
|
|---|
| 14 | import java.util.Optional;
|
|---|
| 15 |
|
|---|
| 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("");
|
|---|
| 34 | return this.userRepository.save(newUser);
|
|---|
| 35 | }catch(Exception e){
|
|---|
| 36 | throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
|
|---|
| 37 | }
|
|---|
| 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 | }
|
|---|
| 50 |
|
|---|
| 51 | @Override
|
|---|
| 52 | public Optional<User> findById(Long id) {
|
|---|
| 53 | return this.userRepository.findById(id);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | }
|
|---|