-
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.
Merge pull request #20 from Digital-Hana-Starbucks/feature/admin-orders
Feat: 관리자 페이지 주문 관련 API
- Loading branch information
Showing
9 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
package com.hanaro.starbucks.controller; | ||
|
||
import com.hanaro.starbucks.dto.orders.OrderEditReqDto; | ||
import com.hanaro.starbucks.dto.orders.OrderResDto; | ||
import com.hanaro.starbucks.service.OrderService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/orders") | ||
public class OrderController { | ||
private final OrderService orderService; | ||
|
||
@GetMapping("") | ||
public List<OrderResDto> getOrders() { | ||
return orderService.getOrders(); | ||
} | ||
@GetMapping("/{orderIdx}") | ||
public OrderResDto getOrder(@PathVariable int orderIdx) { | ||
return orderService.getOrder(orderIdx); | ||
} | ||
|
||
@PutMapping("/{orderIdx}") | ||
public void updateOrder(@PathVariable int orderIdx, @RequestBody OrderEditReqDto orderEditReqDto) { | ||
orderService.updateOrder(orderIdx, orderEditReqDto); | ||
} | ||
|
||
@DeleteMapping("/{orderIdx}") | ||
public void deleteOrder(@PathVariable int orderIdx) { | ||
orderService.deleteOrder(orderIdx); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/hanaro/starbucks/dto/orders/OrderEditReqDto.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,8 @@ | ||
package com.hanaro.starbucks.dto.orders; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class OrderEditReqDto { | ||
private String orderStatus; | ||
} |
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
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
package com.hanaro.starbucks.repository; | ||
|
||
import com.hanaro.starbucks.entity.OrderDetail; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface OrderDetailRepository extends JpaRepository<OrderDetail, Integer> { | ||
} |
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> { | ||
} |
81 changes: 81 additions & 0 deletions
81
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,81 @@ | ||
package com.hanaro.starbucks.service; | ||
|
||
import com.hanaro.starbucks.dto.orders.OrderEditReqDto; | ||
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.List; | ||
import java.util.Optional; | ||
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 = order.getOrderDetails(); | ||
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; | ||
} | ||
|
||
public OrderResDto getOrder(int orderIdx) { | ||
Orders order = findOrderById(orderIdx); | ||
return OrderResDto.builder() | ||
.orderIdx(order.getOrderIdx()) | ||
.orderId(order.getOrderId()) | ||
.totalPrice(calculateTotalPrice(order.getOrderDetails())) | ||
.orderStatus(order.getOrderStatus()) | ||
.orderDate(order.getOrderDate()) | ||
.build(); | ||
} | ||
|
||
public void updateOrder(int orderIdx, OrderEditReqDto orderEditReqDto) { | ||
Orders order = findOrderById(orderIdx); | ||
order.updateOrderStatus(orderEditReqDto.getOrderStatus()); | ||
orderRepository.save(order); | ||
} | ||
|
||
public void deleteOrder(int orderIdx){ | ||
Orders order = findOrderById(orderIdx); | ||
order.getOrderDetails().forEach(orderDetailRepository::delete); | ||
orderRepository.deleteById(orderIdx); | ||
} | ||
private Orders findOrderById(int orderIdx) { | ||
Optional<Orders> optionalOrders = orderRepository.findById(orderIdx); | ||
return optionalOrders.orElseThrow(() -> new IllegalArgumentException("주문 내역이 존재하지 않습니다.")); | ||
} | ||
|
||
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; | ||
|
||
} |