jpashop : get data api v2 (entity -> DTO)
This commit is contained in:
@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* xToOne (ManyToOne, OneToOne)
|
||||
@@ -30,4 +31,19 @@ public class OrderSimpleApiController {
|
||||
});
|
||||
return all;
|
||||
}
|
||||
|
||||
/**
|
||||
* ORDER ->
|
||||
*
|
||||
*/
|
||||
@GetMapping("/api/v2/simple-orders")
|
||||
public List<SimpleOrderDto> ordersV2() {
|
||||
// LAZY 로딩
|
||||
// ORDER N개 조회
|
||||
// N + 1 -> 1 + 회원 N + 배송 N -> 불필요 조회 문제
|
||||
List<Order> orders = orderRepository.findAllByString(new OrderSearch());
|
||||
return orders.stream()
|
||||
.map(SimpleOrderDto::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.jpashop.api;
|
||||
|
||||
import com.example.jpashop.domain.Address;
|
||||
import com.example.jpashop.domain.Order;
|
||||
import com.example.jpashop.domain.OrderStatus;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class SimpleOrderDto {
|
||||
|
||||
private Long orderId;
|
||||
private String name;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
private Address address;
|
||||
|
||||
public SimpleOrderDto(Order o) {
|
||||
this.orderId = o.getId();
|
||||
this.name = o.getMember().getName(); // LAZY 초기화
|
||||
this.orderDate = o.getOrderDate();
|
||||
this.orderStatus = o.getStatus();
|
||||
this.address = o.getDelivery().getAddress(); // LAZY 초기화
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user