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