|
3 | 3 | import com.appsdeveloperblog.app.ws.mobile_app_ws.UserRepository;
|
4 | 4 | import com.appsdeveloperblog.app.ws.mobile_app_ws.io.entity.UserEntity;
|
5 | 5 | import com.appsdeveloperblog.app.ws.mobile_app_ws.service.UserService;
|
| 6 | +import com.appsdeveloperblog.app.ws.mobile_app_ws.shared.Utils; |
6 | 7 | import com.appsdeveloperblog.app.ws.mobile_app_ws.shared.dto.UserDto;
|
7 | 8 | import org.springframework.beans.BeanUtils;
|
8 | 9 | import org.springframework.beans.factory.annotation.Autowired;
|
| 10 | +import org.springframework.security.core.userdetails.UserDetails; |
| 11 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 12 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
9 | 13 | import org.springframework.stereotype.Service;
|
10 | 14 |
|
11 | 15 | @Service
|
12 | 16 | public class UserServiceImpl implements UserService {
|
13 | 17 |
|
14 | 18 | @Autowired
|
15 | 19 | UserRepository userRepository;
|
| 20 | + |
| 21 | + @Autowired |
| 22 | + Utils utils; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + BCryptPasswordEncoder bCryptPasswordEncoder; |
| 26 | + |
16 | 27 | @Override
|
17 | 28 | public UserDto createUser(UserDto user) {
|
18 | 29 |
|
19 |
| - UserEntity userEntity=new UserEntity(); |
20 |
| - BeanUtils.copyProperties(user,userEntity); |
| 30 | + if (userRepository.findByEmail(user.getEmail()) != null) throw new RuntimeException("Record already exists"); |
21 | 31 |
|
22 |
| - userEntity.setEncryptedPassword("test"); |
23 |
| - userEntity.setUserId("testUserId"); |
| 32 | + UserEntity userEntity = new UserEntity(); |
| 33 | + BeanUtils.copyProperties(user, userEntity); |
24 | 34 |
|
25 |
| - UserEntity storedUserDetails=userRepository.save(userEntity); |
| 35 | + String publicUserId=utils.generateUserId(30); |
26 | 36 |
|
27 |
| - UserDto returnValue=new UserDto(); |
28 |
| - BeanUtils.copyProperties(storedUserDetails,returnValue); |
| 37 | + userEntity.setEncryptedPassword(bCryptPasswordEncoder.encode(user.getPassword())); |
| 38 | + userEntity.setUserId(publicUserId); |
| 39 | + |
| 40 | + UserEntity storedUserDetails = userRepository.save(userEntity); |
| 41 | + |
| 42 | + UserDto returnValue = new UserDto(); |
| 43 | + BeanUtils.copyProperties(storedUserDetails, returnValue); |
29 | 44 |
|
30 | 45 | return returnValue;
|
31 | 46 | }
|
| 47 | + |
| 48 | + @Override |
| 49 | + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
| 50 | + return null; |
| 51 | + } |
32 | 52 | }
|
0 commit comments