Order Entity
@Data @Entity @Table(name = "orders") public class OrderEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String productId; @Column(nullable = false) private Integer qty; @Column(nullable = false) private Integer unitPrice; @Column(nullable = false) private Integer totalPrice; @Column(nullable = false) private String userId; @Column(nullable = false, unique = true) private String orderId; @ColumnDefault(value = "CURRENT_TIMESTAMP") private Date createdAt; }
Order Entity
@Data @Entity @Table(name = "orders") public class OrderEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String productId; @Column(nullable = false) private Integer qty; @Column(nullable = false) private Integer unitPrice; @Column(nullable = false) private Integer totalPrice; @Column(nullable = false) private String userId; @Column(nullable = false, unique = true) private String orderId; @ColumnDefault(value = "CURRENT_TIMESTAMP") private Date createdAt; }
Order Dto
@Data public class OrderDto { private String productId; private Integer qty; private Integer unitPrice; private Integer totalPrice; private String orderId; private String userId; }
ResponseOrder
@Data @JsonInclude(JsonInclude.Include.NON_NULL) public class ResponseOrder { private String productId; private Integer qty; private Integer unitPrice; private Integer totalPrice; private Date createdAt; private String orderId; }
RequestOrder
@Data public class RequestOrder { private String productId; private Integer qty; private Integer unitPrice; }
Order Repository
public interface OrderRepository extends CrudRepository<OrderEntity, Long> { OrderEntity findByOrderId(String orderId); Iterable<OrderEntity> findByUserId(String userId); }
Order Service
public interface OrderService { OrderDto createOrder(OrderDto orderDto); OrderDto getOrderByOrderId(String orderId); Iterable<OrderEntity> getOrderByUserId(String userId); }
@Service @RequiredArgsConstructor public class OrderServiceImpl implements OrderService{ private final OrderRepository orderRepository; @Override public OrderDto createOrder(OrderDto orderDto) { orderDto.setOrderId(UUID.randomUUID().toString()); orderDto.setTotalPrice(orderDto.getQty() * orderDto.getUnitPrice()); ModelMapper mapper = new ModelMapper(); mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); OrderEntity orderEntity = mapper.map(orderDto, OrderEntity.class); orderRepository.save(orderEntity); OrderDto returnValue = mapper.map(orderEntity, OrderDto.class); return returnValue; } @Override public OrderDto getOrderByOrderId(String orderId) { OrderEntity orderEntity = orderRepository.findByOrderId(orderId); OrderDto orderDto = new ModelMapper().map(orderEntity, OrderDto.class); return orderDto; } //단일 @Override public Iterable<OrderEntity> getOrderByUserId(String userId) { return orderRepository.findByUserId(userId); } }
Order Controller
@RestController @RequestMapping("/order-service") @RequiredArgsConstructor public class OrderController { private final Environment env; private final OrderService orderService; @GetMapping("/health_check") public String status(){ return String.format("orderService 포트는 %s",env.getProperty("local.server.port")); } @PostMapping("/{userId}/orders") public ResponseEntity<ResponseOrder> createOrder(@PathVariable("userId") String userId, @RequestBody RequestOrder requestOrder){ ModelMapper mapper = new ModelMapper(); mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); OrderDto orderDto = mapper.map(requestOrder, OrderDto.class); orderDto.setUserId(userId); OrderDto createOrder = orderService.createOrder(orderDto); ResponseOrder responseOrder = mapper.map(createOrder, ResponseOrder.class); return ResponseEntity.status(HttpStatus.CREATED).body(responseOrder); } @GetMapping("/{userId}/orders") public ResponseEntity<List<ResponseOrder>> getOrderByUserId(@PathVariable("userId") String userId){ Iterable<OrderEntity> orderList = orderService.getOrderByUserId(userId); List<ResponseOrder> result = new ArrayList<>(); orderList.forEach(v -> { result.add(new ModelMapper().map(v, ResponseOrder.class)); }); return ResponseEntity.ok().body(result); } }