forked from madvirus/ddd-start2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderController.java
107 lines (96 loc) · 4.6 KB
/
OrderController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.myshop.order.ui;
import com.myshop.catalog.query.product.ProductData;
import com.myshop.catalog.query.product.ProductQueryService;
import com.myshop.common.ValidationErrorException;
import com.myshop.member.command.domain.MemberId;
import com.myshop.order.command.application.NoOrderProductException;
import com.myshop.order.command.application.OrderProduct;
import com.myshop.order.command.application.OrderRequest;
import com.myshop.order.command.application.PlaceOrderService;
import com.myshop.order.command.domain.OrderNo;
import com.myshop.order.command.domain.OrdererService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Controller
public class OrderController {
private ProductQueryService productQueryService;
private PlaceOrderService placeOrderService;
private OrdererService ordererService;
public OrderController(ProductQueryService productQueryService,
PlaceOrderService placeOrderService,
OrdererService ordererService) {
this.productQueryService = productQueryService;
this.placeOrderService = placeOrderService;
this.ordererService = ordererService;
}
@PostMapping("/orders/orderConfirm")
public String orderConfirm(@ModelAttribute("orderReq") OrderRequest orderRequest,
ModelMap modelMap) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
orderRequest.setOrdererMemberId(MemberId.of(user.getUsername()));
populateProductsAndTotalAmountsModel(orderRequest, modelMap);
return "order/confirm";
}
private void populateProductsAndTotalAmountsModel(OrderRequest orderRequest, ModelMap modelMap) {
List<ProductData> products = getProducts(orderRequest.getOrderProducts());
modelMap.addAttribute("products", products);
int totalAmounts = 0;
for (int i = 0 ; i < orderRequest.getOrderProducts().size() ; i++) {
OrderProduct op = orderRequest.getOrderProducts().get(i);
ProductData prod = products.get(i);
totalAmounts += op.getQuantity() * prod.getPrice().getValue();
}
modelMap.addAttribute("totalAmounts", totalAmounts);
}
private List<ProductData> getProducts(List<OrderProduct> orderProducts) {
List<ProductData> results = new ArrayList<>();
for (OrderProduct op : orderProducts) {
Optional<ProductData> productOpt = productQueryService.getProduct(op.getProductId());
ProductData product = productOpt.orElseThrow(() -> new NoOrderProductException(op.getProductId()));
results.add(product);
}
return results;
}
@PostMapping("/orders/order")
public String order(@ModelAttribute("orderReq") OrderRequest orderRequest,
BindingResult bindingResult,
ModelMap modelMap) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
orderRequest.setOrdererMemberId(MemberId.of(user.getUsername()));
try {
OrderNo orderNo = placeOrderService.placeOrder(orderRequest);
modelMap.addAttribute("orderNo", orderNo.getNumber());
return "order/orderComplete";
} catch (ValidationErrorException e) {
// 응용 서비스가 발생시킨 검증 에러 목록을 뷰에서 사용할 형태로 변환
e.getErrors().forEach(err -> {
if (err.hasName()) {
bindingResult.rejectValue(err.getName(), err.getCode());
} else {
bindingResult.reject(err.getCode());
}
});
populateProductsAndTotalAmountsModel(orderRequest, modelMap);
return "order/confirm";
}
}
@ExceptionHandler(NoOrderProductException.class)
public String handleNoOrderProduct() {
return "order/noProduct";
}
@InitBinder
public void init(WebDataBinder binder) {
binder.initDirectFieldAccess();
}
}