jpashop : 컬렉션 조회 v2 (entity -> dto 응답)
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
31
jpashop/src/main/java/com/example/jpashop/api/OrderDto.java
Normal file
31
jpashop/src/main/java/com/example/jpashop/api/OrderDto.java
Normal 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());
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user