jpashop : get data api v2 (entity -> DTO)

This commit is contained in:
kim
2021-01-26 20:16:19 +09:00
parent 4e9ead35d7
commit a2b9015d04
2 changed files with 44 additions and 0 deletions

View File

@@ -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());
}
}

View File

@@ -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 초기화
}
}