|
7 | 7 | import org.baeldung.persistence.model.User; |
8 | 8 | import org.springframework.beans.factory.annotation.Autowired; |
9 | 9 | import org.springframework.core.env.Environment; |
10 | | -import org.springframework.dao.DataIntegrityViolationException; |
11 | | -import org.springframework.data.repository.RepositoryDefinition; |
12 | 10 | import org.springframework.security.crypto.password.PasswordEncoder; |
13 | 11 | import org.springframework.stereotype.Service; |
14 | 12 |
|
15 | 13 | @Service |
16 | 14 | public class RepositoryService implements UserService { |
17 | | - @Autowired |
| 15 | + @Autowired |
18 | 16 | private UserRepository repository; |
19 | | - private PasswordEncoder passwordEncoder; |
20 | | - |
| 17 | + |
| 18 | + private final PasswordEncoder passwordEncoder; |
| 19 | + |
21 | 20 | @Autowired |
22 | 21 | private Environment env; |
23 | | - |
| 22 | + |
24 | 23 | @Autowired |
25 | 24 | public RepositoryService(PasswordEncoder passwordEncoder, UserRepository repository) { |
26 | 25 | this.passwordEncoder = passwordEncoder; |
27 | 26 | this.repository = repository; |
28 | | - } |
29 | | - |
| 27 | + } |
| 28 | + |
30 | 29 | @Transactional |
31 | 30 | @Override |
32 | 31 | public User registerNewUserAccount(UserDto userAccountData) throws EmailExistsException { |
33 | 32 | if (emailExist(userAccountData.getUsername())) { |
34 | | - |
| 33 | + |
35 | 34 | throw new EmailExistsException("There is an account with that email adress: " + userAccountData.getUsername()); |
36 | 35 | } |
37 | | - |
38 | | - User user = new User(); |
| 36 | + |
| 37 | + User user = new User(); |
39 | 38 | user.setFirstName(userAccountData.getFirstName()); |
40 | 39 | user.setLastName(userAccountData.getLastName()); |
41 | 40 | user.setPassword(userAccountData.getPassword()); |
42 | 41 | user.setUsername(userAccountData.getUsername()); |
43 | 42 | user.setRole(new Role(userAccountData.getRole(), user)); |
44 | | - return repository.save(user); |
| 43 | + return repository.save(user); |
45 | 44 | } |
46 | | - |
| 45 | + |
47 | 46 | private boolean emailExist(String email) { |
48 | 47 | User user = repository.findByUsername(email); |
49 | 48 | if (user != null) { |
|
0 commit comments