-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/com/hanaro/starbucks/controller/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OrderResDto> getOrders() { | ||
return orderService.getOrders(); | ||
} | ||
|
||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/hanaro/starbucks/dto/orders/OrderResDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/hanaro/starbucks/repository/OrderDetailRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OrderDetail, Integer> { | ||
List<OrderDetail> findAllByOrders(Orders orders); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/hanaro/starbucks/repository/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Orders, Integer> { | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/hanaro/starbucks/service/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OrderResDto> getOrders() { | ||
List<Orders> orders = orderRepository.findAll(); | ||
List<OrderResDto> orderResDtos = orders.stream() | ||
.map(order -> { | ||
List<OrderDetail> 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<OrderDetail> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |