diff --git a/src/main/java/com/hanaro/starbucks/controller/OrderController.java b/src/main/java/com/hanaro/starbucks/controller/OrderController.java new file mode 100644 index 0000000..3eabd09 --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/controller/OrderController.java @@ -0,0 +1,24 @@ +package com.hanaro.starbucks.controller; + +import com.hanaro.starbucks.dto.orders.OrderResDto; +import com.hanaro.starbucks.service.OrderService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/orders") +public class OrderController { + private final OrderService orderService; + + @GetMapping("") + public List getOrders() { + return orderService.getOrders(); + } + + +} diff --git a/src/main/java/com/hanaro/starbucks/dto/orders/OrderResDto.java b/src/main/java/com/hanaro/starbucks/dto/orders/OrderResDto.java new file mode 100644 index 0000000..10cc1e0 --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/dto/orders/OrderResDto.java @@ -0,0 +1,22 @@ +package com.hanaro.starbucks.dto.orders; + + +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDate; + + +@Builder +@Getter +public class OrderResDto { + private int orderIdx; + + private String orderId; + private Integer totalPrice; + + private String orderStatus; + + private LocalDate orderDate; + +} diff --git a/src/main/java/com/hanaro/starbucks/entity/OrderDetail.java b/src/main/java/com/hanaro/starbucks/entity/OrderDetail.java index 03cc597..c217ed8 100644 --- a/src/main/java/com/hanaro/starbucks/entity/OrderDetail.java +++ b/src/main/java/com/hanaro/starbucks/entity/OrderDetail.java @@ -1,9 +1,11 @@ package com.hanaro.starbucks.entity; import jakarta.persistence.*; +import lombok.Getter; import org.hibernate.annotations.ColumnDefault; @Entity +@Getter @Table(name = "order_detail") public class OrderDetail { diff --git a/src/main/java/com/hanaro/starbucks/entity/Orders.java b/src/main/java/com/hanaro/starbucks/entity/Orders.java index ec20050..645648e 100644 --- a/src/main/java/com/hanaro/starbucks/entity/Orders.java +++ b/src/main/java/com/hanaro/starbucks/entity/Orders.java @@ -1,11 +1,13 @@ package com.hanaro.starbucks.entity; import jakarta.persistence.*; +import lombok.Getter; -import java.time.LocalDateTime; +import java.time.LocalDate; import java.util.List; @Entity +@Getter @Table(name = "orders") public class Orders { @@ -21,7 +23,7 @@ public class Orders { private String orderStatus; @Column(name = "order_date") - private LocalDateTime orderDate; + private LocalDate orderDate; @ManyToOne @JoinColumn(name = "user_idx") diff --git a/src/main/java/com/hanaro/starbucks/repository/OrderDetailRepository.java b/src/main/java/com/hanaro/starbucks/repository/OrderDetailRepository.java new file mode 100644 index 0000000..1f51b8a --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/repository/OrderDetailRepository.java @@ -0,0 +1,13 @@ +package com.hanaro.starbucks.repository; + +import com.hanaro.starbucks.entity.OrderDetail; +import com.hanaro.starbucks.entity.Orders; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface OrderDetailRepository extends JpaRepository { + List findAllByOrders(Orders orders); +} diff --git a/src/main/java/com/hanaro/starbucks/repository/OrderRepository.java b/src/main/java/com/hanaro/starbucks/repository/OrderRepository.java new file mode 100644 index 0000000..39d4ce4 --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/repository/OrderRepository.java @@ -0,0 +1,9 @@ +package com.hanaro.starbucks.repository; + +import com.hanaro.starbucks.entity.Orders; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface OrderRepository extends JpaRepository { +} diff --git a/src/main/java/com/hanaro/starbucks/service/OrderService.java b/src/main/java/com/hanaro/starbucks/service/OrderService.java new file mode 100644 index 0000000..c1e7750 --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/service/OrderService.java @@ -0,0 +1,53 @@ +package com.hanaro.starbucks.service; + +import com.hanaro.starbucks.dto.orders.OrderResDto; +import com.hanaro.starbucks.entity.OrderDetail; +import com.hanaro.starbucks.entity.Orders; +import com.hanaro.starbucks.repository.OrderDetailRepository; +import com.hanaro.starbucks.repository.OrderRepository; +import com.hanaro.starbucks.util.Constant; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class OrderService { + private final OrderRepository orderRepository; + private final OrderDetailRepository orderDetailRepository; + + public List getOrders() { + List orders = orderRepository.findAll(); + List orderResDtos = orders.stream() + .map(order -> { + List orderDetails = orderDetailRepository.findAllByOrders(order); + int totalPrice = calculateTotalPrice(orderDetails); + return OrderResDto.builder() + .orderIdx(order.getOrderIdx()) + .orderId(order.getOrderId()) + .totalPrice(totalPrice) + .orderStatus(order.getOrderStatus()) + .orderDate(order.getOrderDate()) + .build(); + }) + .collect(Collectors.toList()); + return orderResDtos; + } + + private int calculateTotalPrice(List orderDetails) { + int totalPrice = 0; + for (OrderDetail orderDetail : orderDetails) { + int menuPrice = orderDetail.getMenu().getMenuPrice(); + if (orderDetail.getMenuSize().equals(Constant.GRANDE_SIZE)) { + menuPrice += Constant.GRANDE; + } else if (orderDetail.getMenuSize().equals(Constant.VENTI_SIZE)) { + menuPrice += Constant.VENTI; + } + totalPrice += menuPrice * orderDetail.getOrderDetailCount(); + } + return totalPrice; + } +} diff --git a/src/main/java/com/hanaro/starbucks/util/Constant.java b/src/main/java/com/hanaro/starbucks/util/Constant.java new file mode 100644 index 0000000..0fd5c2b --- /dev/null +++ b/src/main/java/com/hanaro/starbucks/util/Constant.java @@ -0,0 +1,9 @@ +package com.hanaro.starbucks.util; + +public class Constant { + public static final String GRANDE_SIZE="grande"; + public static final String VENTI_SIZE="venti"; + public static final int GRANDE = 500; + public static final int VENTI = 1000; + +}