jpashop : 컬렉션 조회 v2 (entity -> dto 응답)

This commit is contained in:
kim
2021-01-27 17:18:19 +09:00
parent 484b99f670
commit 288d6ca162
3 changed files with 58 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequiredArgsConstructor
@@ -27,4 +28,12 @@ public class OrderApiController {
}
return all;
}
@GetMapping("/api/v2/orders")
public List<OrderDto> ordersV2() {
List<Order> orders = orderRepository.findAllByString(new OrderSearch());
return orders.stream()
.map(OrderDto::new)
.collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,31 @@
package com.example.jpashop.api;
import com.example.jpashop.domain.Address;
import com.example.jpashop.domain.Order;
import com.example.jpashop.domain.OrderItem;
import lombok.Data;
import org.springframework.http.ResponseEntity;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class OrderDto {
private Long orderId;
private String name;
private LocalDateTime orderDate;
private Address address;
private List<OrderItemDto> orderItems;
public OrderDto(Order order) {
orderId = order.getId();
name = order.getMember().getName();
orderDate = order.getOrderDate();
address = order.getDelivery().getAddress();
orderItems = order.getOrderItems().stream()
.map(OrderItemDto::new)
.collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,18 @@
package com.example.jpashop.api;
import com.example.jpashop.domain.OrderItem;
import lombok.Data;
@Data
public class OrderItemDto {
private String itemName;
private int orderPrice;
private int count;
public OrderItemDto(OrderItem orderItem) {
itemName = orderItem.getItem().getName();
orderPrice = orderItem.getOrderPrice();
count = orderItem.getCount();
}
}