|
| 1 | +package user.service.impl; |
| 2 | + |
| 3 | +import edu.fudan.common.util.Response; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.http.*; |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import org.springframework.web.client.RestTemplate; |
| 9 | +import user.dto.AuthDto; |
| 10 | +import user.dto.UserDto; |
| 11 | +import user.entity.User; |
| 12 | +import user.repository.UserRepository; |
| 13 | +import user.service.UserService; |
| 14 | + |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | + |
| 20 | +@Service |
| 21 | +@Slf4j |
| 22 | +public class UserServiceImpl implements UserService { |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private UserRepository userRepository; |
| 26 | + |
| 27 | + private static final String AUHT_SERVICE_URI = "http://ts-auth-service:12340/api/v1"; |
| 28 | + |
| 29 | + @Override |
| 30 | + public Response saveUser(UserDto userDto, HttpHeaders headers) { |
| 31 | + log.info("Save User Name id:" + userDto.getUserName()); |
| 32 | + UUID userId = userDto.getUserId(); |
| 33 | + if (userDto.getUserId() == null) |
| 34 | + userId = UUID.randomUUID(); |
| 35 | + |
| 36 | + User user = User.builder() |
| 37 | + .userId(userId) |
| 38 | + .userName(userDto.getUserName()) |
| 39 | + .password(userDto.getPassword()) |
| 40 | + .gender(userDto.getGender()) |
| 41 | + .documentType(userDto.getDocumentType()) |
| 42 | + .documentNum(userDto.getDocumentNum()) |
| 43 | + .email(userDto.getEmail()).build(); |
| 44 | + |
| 45 | + // avoid same user name |
| 46 | + User user1 = userRepository.findByUserName(userDto.getUserName()); |
| 47 | + if (user1 == null) { |
| 48 | + createDefaultAuthUser(AuthDto.builder().userId(userId) |
| 49 | + .userName(user.getUserName()) |
| 50 | + .password(user.getPassword()).build(), headers); |
| 51 | + |
| 52 | + User userSaveResult = userRepository.save(user); |
| 53 | + log.info("Send authorization message to ts-auth-service...."); |
| 54 | + |
| 55 | + return new Response<>(1, "REGISTER USER SUCCESS", userSaveResult); |
| 56 | + } else { |
| 57 | + return new Response(0, "USER HAS ALREADY EXISTS", null); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void createDefaultAuthUser(AuthDto dto, HttpHeaders headers) { |
| 62 | + log.info("CALL TO AUTH"); |
| 63 | + log.info("AuthDto : " + dto.toString()); |
| 64 | + RestTemplate restTemplate = new RestTemplate(); |
| 65 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 66 | + HttpEntity<AuthDto> httpEntity = new HttpEntity(dto, headers); |
| 67 | + restTemplate.exchange(AUHT_SERVICE_URI + "/auth", |
| 68 | + HttpMethod.POST, |
| 69 | + httpEntity, |
| 70 | + Void.class); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Response getAllUsers(HttpHeaders headers) { |
| 75 | + List<User> users = userRepository.findAll(); |
| 76 | + if (users != null && users.size() > 0) |
| 77 | + return new Response<>(1, "Success", users); |
| 78 | + return new Response<>(0, "NO User", null); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Response findByUserName(String userName, HttpHeaders headers) { |
| 83 | + User user = userRepository.findByUserName(userName); |
| 84 | + if(user != null) |
| 85 | + return new Response<>(1, "Find User Success", user); |
| 86 | + return new Response<>(0, "No User", null); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Response findByUserId(String userId, HttpHeaders headers) { |
| 91 | + User user = userRepository.findByUserId(UUID.fromString(userId)); |
| 92 | + if(user != null) |
| 93 | + return new Response<>(1, "Find User Success", user); |
| 94 | + return new Response<>(0, "No User", null); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Response deleteUser(UUID userId, HttpHeaders headers) { |
| 99 | + log.info("DELETE USER BY ID :" + userId); |
| 100 | + User user = userRepository.findByUserId(userId); |
| 101 | + if (user != null) { |
| 102 | + // first only admin token can delete success |
| 103 | + deleteUserAuth(userId, headers); |
| 104 | + // second |
| 105 | + userRepository.deleteByUserId(userId); |
| 106 | + log.info("DELETE SUCCESS"); |
| 107 | + return new Response<>(1, "DELETE SUCCESS", null); |
| 108 | + } else { |
| 109 | + return new Response<>(0, "USER NOT EXISTS", null); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Response updateUser(UserDto userDto, HttpHeaders headers) { |
| 115 | + log.info("UPDATE USER :" + userDto.toString()); |
| 116 | + User oldUser = userRepository.findByUserName(userDto.getUserName()); |
| 117 | + if (oldUser != null) { |
| 118 | + User newUser = oldUser.builder().email(userDto.getEmail()) |
| 119 | + .password(userDto.getPassword()) |
| 120 | + .userName(userDto.getUserName()) |
| 121 | + .gender(userDto.getGender()) |
| 122 | + .documentNum(userDto.getDocumentNum()) |
| 123 | + .documentType(userDto.getDocumentType()).build(); |
| 124 | + userRepository.save(newUser); |
| 125 | + return new Response<>(1, "SAVE USER SUCCESS", newUser); |
| 126 | + } else { |
| 127 | + return new Response(0, "USER NOT EXISTS", null); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public void deleteUserAuth(UUID userId, HttpHeaders headers) { |
| 132 | + log.info("DELETE USER BY ID :" + userId); |
| 133 | + RestTemplate restTemplate = new RestTemplate(); |
| 134 | + |
| 135 | + HttpEntity<Response> httpEntity = new HttpEntity<>(headers); |
| 136 | + restTemplate.exchange(AUHT_SERVICE_URI + "/users/" + userId, |
| 137 | + HttpMethod.DELETE, |
| 138 | + httpEntity, |
| 139 | + Response.class); |
| 140 | + log.info("DELETE USER AUTH SUCCESS"); |
| 141 | + } |
| 142 | +} |
0 commit comments