Skip to content

Commit

Permalink
fix modify user bug
Browse files Browse the repository at this point in the history
  • Loading branch information
flyer1212 committed May 31, 2019
1 parent 131f043 commit 228066f
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 33 deletions.
142 changes: 142 additions & 0 deletions UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package user.service.impl;

import edu.fudan.common.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import user.dto.AuthDto;
import user.dto.UserDto;
import user.entity.User;
import user.repository.UserRepository;
import user.service.UserService;


import java.util.List;
import java.util.UUID;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

private static final String AUHT_SERVICE_URI = "http://ts-auth-service:12340/api/v1";

@Override
public Response saveUser(UserDto userDto, HttpHeaders headers) {
log.info("Save User Name id:" + userDto.getUserName());
UUID userId = userDto.getUserId();
if (userDto.getUserId() == null)
userId = UUID.randomUUID();

User user = User.builder()
.userId(userId)
.userName(userDto.getUserName())
.password(userDto.getPassword())
.gender(userDto.getGender())
.documentType(userDto.getDocumentType())
.documentNum(userDto.getDocumentNum())
.email(userDto.getEmail()).build();

// avoid same user name
User user1 = userRepository.findByUserName(userDto.getUserName());
if (user1 == null) {
createDefaultAuthUser(AuthDto.builder().userId(userId)
.userName(user.getUserName())
.password(user.getPassword()).build(), headers);

User userSaveResult = userRepository.save(user);
log.info("Send authorization message to ts-auth-service....");

return new Response<>(1, "REGISTER USER SUCCESS", userSaveResult);
} else {
return new Response(0, "USER HAS ALREADY EXISTS", null);
}
}

private void createDefaultAuthUser(AuthDto dto, HttpHeaders headers) {
log.info("CALL TO AUTH");
log.info("AuthDto : " + dto.toString());
RestTemplate restTemplate = new RestTemplate();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<AuthDto> httpEntity = new HttpEntity(dto, headers);
restTemplate.exchange(AUHT_SERVICE_URI + "/auth",
HttpMethod.POST,
httpEntity,
Void.class);
}

@Override
public Response getAllUsers(HttpHeaders headers) {
List<User> users = userRepository.findAll();
if (users != null && users.size() > 0)
return new Response<>(1, "Success", users);
return new Response<>(0, "NO User", null);
}

@Override
public Response findByUserName(String userName, HttpHeaders headers) {
User user = userRepository.findByUserName(userName);
if(user != null)
return new Response<>(1, "Find User Success", user);
return new Response<>(0, "No User", null);
}

@Override
public Response findByUserId(String userId, HttpHeaders headers) {
User user = userRepository.findByUserId(UUID.fromString(userId));
if(user != null)
return new Response<>(1, "Find User Success", user);
return new Response<>(0, "No User", null);
}

@Override
public Response deleteUser(UUID userId, HttpHeaders headers) {
log.info("DELETE USER BY ID :" + userId);
User user = userRepository.findByUserId(userId);
if (user != null) {
// first only admin token can delete success
deleteUserAuth(userId, headers);
// second
userRepository.deleteByUserId(userId);
log.info("DELETE SUCCESS");
return new Response<>(1, "DELETE SUCCESS", null);
} else {
return new Response<>(0, "USER NOT EXISTS", null);
}
}

@Override
public Response updateUser(UserDto userDto, HttpHeaders headers) {
log.info("UPDATE USER :" + userDto.toString());
User oldUser = userRepository.findByUserName(userDto.getUserName());
if (oldUser != null) {
User newUser = oldUser.builder().email(userDto.getEmail())
.password(userDto.getPassword())
.userName(userDto.getUserName())
.gender(userDto.getGender())
.documentNum(userDto.getDocumentNum())
.documentType(userDto.getDocumentType()).build();
userRepository.save(newUser);
return new Response<>(1, "SAVE USER SUCCESS", newUser);
} else {
return new Response(0, "USER NOT EXISTS", null);
}
}

public void deleteUserAuth(UUID userId, HttpHeaders headers) {
log.info("DELETE USER BY ID :" + userId);
RestTemplate restTemplate = new RestTemplate();

HttpEntity<Response> httpEntity = new HttpEntity<>(headers);
restTemplate.exchange(AUHT_SERVICE_URI + "/users/" + userId,
HttpMethod.DELETE,
httpEntity,
Response.class);
log.info("DELETE USER AUTH SUCCESS");
}
}
23 changes: 12 additions & 11 deletions ts-auth-service/src/main/java/auth/controller/AuthController.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package auth.controller;

import auth.dto.AuthDto;
import auth.dto.BasicAuthDto;
import auth.dto.TokenDto;
import auth.service.TokenService;
import auth.service.UserService;
import edu.fudan.common.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/api/v1")
@RequestMapping("/api/v1/auth")
public class AuthController {

@Autowired
Expand All @@ -23,17 +22,19 @@ public class AuthController {
/**
* only while user register, this method will be called by ts-user-service
* to create a default role use
*
* @return
*/
@GetMapping("/hello")
public String getHello(){
public String getHello() {
return "hello";
}
@PostMapping("/auth")
public ResponseEntity<Void> createDefaultUser(@RequestBody AuthDto dto, HttpHeaders headers) {
log.info(dto.getUserName() +" USER NAME");
userService.createDefaultAuthUser(dto , headers);
return ResponseEntity.status(HttpStatus.CREATED).build();

@PostMapping
public HttpEntity<Response> createDefaultUser(@RequestBody AuthDto authDto) {
userService.createDefaultAuthUser(authDto);
Response response = new Response(1, "SUCCESS", authDto);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth.controller;


import auth.dto.AuthDto;
import auth.dto.BasicAuthDto;
import auth.dto.TokenDto;
import auth.entity.User;
Expand All @@ -10,6 +11,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down
4 changes: 0 additions & 4 deletions ts-auth-service/src/main/java/auth/dto/AuthDto.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package auth.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

/***
* user-service register user , then send to auth-service
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AuthDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface UserService {

List<User> getAllUser(HttpHeaders headers);

User createDefaultAuthUser(AuthDto dto, HttpHeaders headers);
User createDefaultAuthUser(AuthDto dto);

Response deleteByUserId(UUID userId, HttpHeaders headers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<User> getAllUser(HttpHeaders headers) {
* @return
*/
@Override
public User createDefaultAuthUser(AuthDto dto, HttpHeaders headers) {
public User createDefaultAuthUser(AuthDto dto) {
log.info("Register User Info is: " + dto.getUserName());
User user = User.builder()
.userId(UUID.fromString(dto.getUserId()))
Expand Down
2 changes: 1 addition & 1 deletion ts-ui-dashboard/static/admin_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<div class="tpl-portlet-components">
<div class="portlet-title">
<div class="caption font-green bold">
Travel List
User List
</div>

</div>
Expand Down
10 changes: 5 additions & 5 deletions ts-ui-dashboard/static/assets/js/admin_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ app.controller('indexCtrl', function ($scope, $http, $window, loadDataService) {
data: {
accountId: $scope.update_user_id,
userName: $scope.update_user_name,
newPassword: $scope.update_user_password,
newGender: $scope.update_user_gender,
newEmail: $scope.update_user_email,
newDocumentType: $scope.update_user_document_type,
newDocumentNumber: $scope.update_user_document_number
password: $scope.update_user_password,
gender: $scope.update_user_gender,
email: $scope.update_user_email,
documentType: $scope.update_user_document_type,
documentNum: $scope.update_user_document_number
}
}).success(function (data, status, headers, config) {
if (data.status == 1) {
Expand Down
3 changes: 2 additions & 1 deletion ts-user-service/src/main/java/user/dto/AuthDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;


/***
* user-service register user , then send to auth-service
Expand All @@ -15,6 +15,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class AuthDto {

private String userId;
private String userName;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.fudan.common.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -45,9 +46,10 @@ public Response saveUser(UserDto userDto, HttpHeaders headers) {
// avoid same user name
User user1 = userRepository.findByUserName(userDto.getUserName());
if (user1 == null) {
createDefaultAuthUser(AuthDto.builder().userId(UUID.randomUUID() +"")

createDefaultAuthUser(AuthDto.builder().userId(userId + "")
.userName(user.getUserName())
.password(user.getPassword()).build(), headers);
.password(user.getPassword()).build());

User userSaveResult = userRepository.save(user);
log.info("Send authorization message to ts-auth-service....");
Expand All @@ -58,14 +60,18 @@ public Response saveUser(UserDto userDto, HttpHeaders headers) {
}
}

private void createDefaultAuthUser(AuthDto dto, HttpHeaders headers) {
private Response createDefaultAuthUser(AuthDto dto) {
log.info("CALL TO AUTH");
log.info("AuthDto : " + dto.toString());
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(dto, headers);
restTemplate.exchange(AUHT_SERVICE_URI + "/auth",
HttpHeaders headers = new HttpHeaders();
HttpEntity<AuthDto> entity = new HttpEntity<AuthDto>(dto, headers);
ResponseEntity<Response<AuthDto>> res = restTemplate.exchange("http://ts-auth-service:12340/api/v1/auth",
HttpMethod.POST,
httpEntity,
Void.class);
entity,
new ParameterizedTypeReference<Response<AuthDto>>() {
});
return res.getBody();
}

@Override
Expand All @@ -79,15 +85,15 @@ public Response getAllUsers(HttpHeaders headers) {
@Override
public Response findByUserName(String userName, HttpHeaders headers) {
User user = userRepository.findByUserName(userName);
if(user != null)
if (user != null)
return new Response<>(1, "Find User Success", user);
return new Response<>(0, "No User", null);
}

@Override
public Response findByUserId(String userId, HttpHeaders headers) {
User user = userRepository.findByUserId(UUID.fromString(userId));
if(user != null)
if (user != null)
return new Response<>(1, "Find User Success", user);
return new Response<>(0, "No User", null);
}
Expand Down Expand Up @@ -115,10 +121,12 @@ public Response updateUser(UserDto userDto, HttpHeaders headers) {
if (oldUser != null) {
User newUser = oldUser.builder().email(userDto.getEmail())
.password(userDto.getPassword())
.userId(oldUser.getUserId())
.userName(userDto.getUserName())
.gender(userDto.getGender())
.documentNum(userDto.getDocumentNum())
.documentType(userDto.getDocumentType()).build();
userRepository.deleteByUserId(oldUser.getUserId());
userRepository.save(newUser);
return new Response<>(1, "SAVE USER SUCCESS", newUser);
} else {
Expand Down

0 comments on commit 228066f

Please sign in to comment.