spring cloud : e-commerce(user-service) - get user, get userlist
This commit is contained in:
@@ -21,6 +21,10 @@ spring:
|
||||
preLogger: true
|
||||
postLogger: true
|
||||
routes:
|
||||
- id: user-service
|
||||
uri: lb://USER-SERVICE
|
||||
predicates:
|
||||
- Path=/user-service/**
|
||||
- id: first-service
|
||||
uri: lb://MY-FIRST-SERVICE
|
||||
predicates:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.userservice.controller;
|
||||
|
||||
import com.example.userservice.dto.UserDto;
|
||||
import com.example.userservice.entity.UserEntity;
|
||||
import com.example.userservice.service.UserService;
|
||||
import com.example.userservice.vo.Greeting;
|
||||
import com.example.userservice.vo.RequestUser;
|
||||
@@ -8,24 +9,32 @@ import com.example.userservice.vo.ResponseUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/")
|
||||
@RequestMapping("/user-service")
|
||||
public class UserController {
|
||||
|
||||
private final Greeting greeting;
|
||||
private final Environment env;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final ModelMapper mapper = new ModelMapper();
|
||||
|
||||
|
||||
@GetMapping("/health_check")
|
||||
public String status() {
|
||||
return "It's working in User Service";
|
||||
return String.format("It's working in User Service on PORT %s",
|
||||
env.getProperty("local.server.port"));
|
||||
}
|
||||
|
||||
@GetMapping("/welcome")
|
||||
@@ -36,7 +45,6 @@ public class UserController {
|
||||
@PostMapping("/users")
|
||||
public ResponseEntity<ResponseUser> createUser(@Valid @RequestBody RequestUser requestUser) {
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
|
||||
UserDto userDto = mapper.map(requestUser, UserDto.class);
|
||||
@@ -46,4 +54,24 @@ public class UserController {
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(responseUser);
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public ResponseEntity<List<ResponseUser>> getUsers() {
|
||||
List<UserEntity> userList = userService.gerUserByAll();
|
||||
|
||||
List<ResponseUser> result = new ArrayList<>();
|
||||
userList.forEach(userEntity -> {
|
||||
result.add(mapper.map(userEntity, ResponseUser.class));
|
||||
});
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK).body(result);
|
||||
}
|
||||
|
||||
@GetMapping("/users/{userId}")
|
||||
public ResponseEntity<ResponseUser> getUser(@PathVariable("userId") String userId) {
|
||||
UserDto userDto = userService.getUserByUserId(userId);
|
||||
ResponseUser result = mapper.map(userDto, ResponseUser.class);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK).body(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.example.userservice.dto;
|
||||
|
||||
import com.example.userservice.vo.ResponseOrder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UserDto {
|
||||
@@ -14,4 +16,7 @@ public class UserDto {
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private String encryptedPwd;
|
||||
|
||||
private List<ResponseOrder> orders;
|
||||
|
||||
}
|
||||
|
||||
@@ -3,5 +3,9 @@ package com.example.userservice.repository;
|
||||
import com.example.userservice.entity.UserEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||
|
||||
Optional<UserEntity> findByUserId(String userId);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package com.example.userservice.service;
|
||||
|
||||
import com.example.userservice.dto.UserDto;
|
||||
import com.example.userservice.entity.UserEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void createUser(UserDto userDto);
|
||||
UserDto getUserByUserId(String userId);
|
||||
List<UserEntity> gerUserByAll();
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ import com.example.userservice.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@@ -17,16 +20,32 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final BCryptPasswordEncoder passwordEncoder;
|
||||
private final ModelMapper mapper = new ModelMapper();
|
||||
|
||||
@Override
|
||||
public void createUser(UserDto userDto) {
|
||||
userDto.setUserId(UUID.randomUUID().toString());
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
UserEntity userEntity = mapper.map(userDto, UserEntity.class);
|
||||
userEntity.setEncryptedPwd(passwordEncoder.encode(userDto.getPwd()));
|
||||
|
||||
userRepository.save(userEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDto getUserByUserId(String userId) {
|
||||
UserEntity userEntity = userRepository.findByUserId(userId)
|
||||
.orElseThrow(() -> new UsernameNotFoundException("user not found"));
|
||||
|
||||
UserDto userDto = mapper.map(userEntity, UserDto.class);
|
||||
userDto.setOrders(new ArrayList<>());
|
||||
|
||||
return userDto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserEntity> gerUserByAll() {
|
||||
return userRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.userservice.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class ResponseOrder {
|
||||
|
||||
private String productId;
|
||||
private Integer qty;
|
||||
private Integer unitPrice;
|
||||
private Integer totalPrice;
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private String orderId;
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
package com.example.userservice.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class ResponseUser {
|
||||
|
||||
private String email;
|
||||
private String name;
|
||||
private String userId;
|
||||
|
||||
private List<ResponseOrder> orders;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8081
|
||||
port: 0
|
||||
|
||||
spring:
|
||||
application:
|
||||
|
||||
Reference in New Issue
Block a user