Skip to content

Commit

Permalink
Feat: 주문 정보 단일 조회(#16)
Browse files Browse the repository at this point in the history
close: #16
  • Loading branch information
mummhy0811 committed Apr 30, 2024
1 parent ea43dd9 commit e077496
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hanaro.starbucks.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -19,6 +20,9 @@ public class OrderController {
public List<OrderResDto> getOrders() {
return orderService.getOrders();
}

@GetMapping("/{orderIdx}")
public OrderResDto getOrder(@PathVariable int orderIdx) {
return orderService.getOrder(orderIdx);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/hanaro/starbucks/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand All @@ -34,6 +35,21 @@ public List<OrderResDto> getOrders() {
return orderResDtos;
}

public OrderResDto getOrder(int orderIdx) {
Optional<Orders> optionalOrders = orderRepository.findById(orderIdx);
if(optionalOrders.isEmpty()){
throw new IllegalArgumentException("주문 내역이 존재하지 않습니다.");
}
Orders order = optionalOrders.get();
return OrderResDto.builder()
.orderIdx(order.getOrderIdx())
.orderId(order.getOrderId())
.totalPrice(calculateTotalPrice(order.getOrderDetails()))
.orderStatus(order.getOrderStatus())
.orderDate(order.getOrderDate())
.build();
}

private int calculateTotalPrice(List<OrderDetail> orderDetails) {
int totalPrice = 0;
for (OrderDetail orderDetail : orderDetails) {
Expand Down

0 comments on commit e077496

Please sign in to comment.