Skip to content

Commit b17db11

Browse files
authored
Merge pull request #68 from xdorro/tuananh
Update checkout address
2 parents 79a112d + 8c2a7ac commit b17db11

File tree

9 files changed

+106
-48
lines changed

9 files changed

+106
-48
lines changed

src/main/java/com/example/multikart/domain/dto/CheckoutRequestDTO.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public class CheckoutRequestDTO {
2121
@Email
2222
private String email;
2323

24-
private String province;
24+
private String provinceId;
2525

26-
private String district;
26+
private String districtId;
2727

28-
private String ward;
28+
private String wardId;
2929

3030
private String address;
3131

src/main/java/com/example/multikart/domain/dto/OrderDTO.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public class OrderDTO implements Serializable {
3434

3535
private String address;
3636

37-
private String province;
37+
private String provinceId;
3838

39-
private String district;
39+
private String districtId;
4040

41-
private String ward;
41+
private String wardId;
4242

4343
private Float totalPrice;
4444

@@ -57,11 +57,11 @@ public OrderDTO(Order order, Customer customer) {
5757

5858
this.address = order.getAddress();
5959

60-
this.province = order.getProvince();
60+
this.provinceId = order.getProvinceId();
6161

62-
this.district = order.getDistrict();
62+
this.districtId = order.getDistrictId();
6363

64-
this.ward = order.getWard();
64+
this.wardId = order.getWardId();
6565

6666
this.totalPrice = order.getTotalPrice();
6767

@@ -87,11 +87,11 @@ public OrderDTO(Order order, Customer customer, Payment payment, Transport trans
8787

8888
this.address = order.getAddress();
8989

90-
this.province = order.getProvince();
90+
this.provinceId = order.getProvinceId();
9191

92-
this.district = order.getDistrict();
92+
this.districtId = order.getDistrictId();
9393

94-
this.ward = order.getWard();
94+
this.wardId = order.getWardId();
9595

9696
this.totalPrice = order.getTotalPrice();
9797

src/main/java/com/example/multikart/domain/model/Order.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public class Order extends BaseModel {
3333

3434
private String address;
3535

36-
private String province;
36+
private String provinceId;
3737

38-
private String district;
38+
private String districtId;
3939

40-
private String ward;
40+
private String wardId;
4141

4242
// Ngày giao hàng
4343
@Column(name = "delivery_date")

src/main/java/com/example/multikart/repo/OrderDetailRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public interface OrderDetailRepository extends CrudRepository<OrderDetail, Long>
1616
"LEFT JOIN Order o on od.orderId = o.orderId\n" +
1717
"LEFT JOIN Product p on od.productId = p.productId\n" +
1818
"LEFT JOIN ProductImage pi on od.productId = pi.productId\n" +
19-
"WHERE o.status = :status\n" +
20-
" AND p.status = :status\n" +
21-
" AND pi.status = :status\n" +
19+
"WHERE o.status <> :status\n" +
20+
" AND p.status <> :status\n" +
21+
" AND pi.status <> :status\n" +
2222
" AND o.orderId = :orderId\n" +
2323
" AND od.orderId = :orderId\n" +
2424
" AND pi.position = (Select min(position) from ProductImage where productId = od.productId)")
25-
List<OrderDetailDTO> findAllByOrderIdStatus(Long orderId, Integer status);
25+
List<OrderDetailDTO> findAllByOrderIdStatusNot(Long orderId, Integer status);
2626
}

src/main/java/com/example/multikart/repo/OrderRepository.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ public interface OrderRepository extends CrudRepository<Order, Long> {
3232
" AND o.orderId = :orderId")
3333
OrderDTO findByOrderIdAndStatus(Long orderId, Integer status);
3434

35+
@Query("SELECT new com.example.multikart.domain.dto.OrderDTO(o, c, p, t)\n" +
36+
"FROM Order o\n" +
37+
"LEFT JOIN Customer c on o.customerId = c.customerId\n" +
38+
"LEFT JOIN Payment p on o.paymentId = p.paymentId\n" +
39+
"LEFT JOIN Transport t on o.transportId = t.transportId\n" +
40+
"WHERE o.status <> :status\n" +
41+
" AND p.status <> :status\n" +
42+
" AND t.status <> :status\n" +
43+
" AND o.orderId = :orderId")
44+
OrderDTO findByOrderIdAndStatusNot(Long orderId, Integer status);
45+
3546
@Query("SELECT new com.example.multikart.domain.dto.OrderDTO(o, c, p, t)\n" +
3647
"FROM Order o\n" +
3748
"LEFT JOIN Customer c on o.customerId = c.customerId\n" +
@@ -45,6 +56,19 @@ public interface OrderRepository extends CrudRepository<Order, Long> {
4556
" AND o.orderId = :orderId")
4657
OrderDTO findByOrderIdAndCustomerIdAndStatus(Long orderId, Long customerId, Integer status);
4758

59+
@Query("SELECT new com.example.multikart.domain.dto.OrderDTO(o, c, p, t)\n" +
60+
"FROM Order o\n" +
61+
"LEFT JOIN Customer c on o.customerId = c.customerId\n" +
62+
"LEFT JOIN Payment p on o.paymentId = p.paymentId\n" +
63+
"LEFT JOIN Transport t on o.transportId = t.transportId\n" +
64+
"WHERE o.status <> :status\n" +
65+
" AND o.customerId = :customerId\n" +
66+
" AND c.status <> :status\n" +
67+
" AND p.status <> :status\n" +
68+
" AND t.status <> :status\n" +
69+
" AND o.orderId = :orderId")
70+
OrderDTO findByOrderIdAndCustomerIdAndStatusNot(Long orderId, Long customerId, Integer status);
71+
4872
@Query("SELECT new com.example.multikart.domain.dto.OrderDTO(o, c)\n" +
4973
"FROM Order o\n" +
5074
"LEFT JOIN Customer c on o.customerId = c.customerId\n" +
@@ -53,5 +77,13 @@ public interface OrderRepository extends CrudRepository<Order, Long> {
5377
" AND c.status = :status")
5478
List<OrderDTO> findAllByCustomerIdAndStatus(Long customerId, Integer status);
5579

80+
@Query("SELECT new com.example.multikart.domain.dto.OrderDTO(o, c)\n" +
81+
"FROM Order o\n" +
82+
"LEFT JOIN Customer c on o.customerId = c.customerId\n" +
83+
"WHERE o.status <> :status\n" +
84+
" AND o.customerId = :customerId\n" +
85+
" AND c.status <> :status")
86+
List<OrderDTO> findAllByCustomerIdAndStatusNot(Long customerId, Integer status);
87+
5688
int countByNameAndStatus(String name, Integer status);
5789
}

src/main/java/com/example/multikart/service/impl/CheckoutServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public String view(HttpSession session, Model model) {
4040
.name(customer.getName())
4141
.email(customer.getEmail())
4242
.phone(customer.getPhone())
43-
// .address(customer.getAddress())
43+
.address(customer.getAddress())
44+
.districtId(customer.getDistrictId())
45+
.provinceId(customer.getProvinceId())
46+
.wardId(customer.getWardId())
47+
.transportId(0L)
48+
.paymentId(0L)
4449
.build();
4550

4651
model.addAttribute("checkout", checkout);

src/main/java/com/example/multikart/service/impl/OrderServiceImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.example.multikart.service.impl;
22

3+
import com.example.multikart.common.Const;
34
import com.example.multikart.common.Const.DefaultStatus;
5+
import com.example.multikart.common.Const.OrderStatus;
46
import com.example.multikart.common.DataUtils;
57
import com.example.multikart.common.Utils;
68
import com.example.multikart.domain.model.Customer;
@@ -31,7 +33,7 @@ public String findAllOrders(Model model) {
3133

3234
@Override
3335
public String viewOrder(Long id, Model model, RedirectAttributes redirect) {
34-
var order = orderRepository.findByOrderIdAndStatus(id, DefaultStatus.ACTIVE);
36+
var order = orderRepository.findByOrderIdAndStatusNot(id, OrderStatus.DELETED);
3537
if (DataUtils.isNullOrEmpty(order)) {
3638
redirect.addFlashAttribute("error", "Hóa đơn không tồn tại");
3739

@@ -40,7 +42,7 @@ public String viewOrder(Long id, Model model, RedirectAttributes redirect) {
4042

4143
model.addAttribute("order", order);
4244

43-
var orderDetails = orderDetailRepository.findAllByOrderIdStatus(id, DefaultStatus.ACTIVE);
45+
var orderDetails = orderDetailRepository.findAllByOrderIdStatusNot(id, OrderStatus.DELETED);
4446
model.addAttribute("orderDetails", orderDetails);
4547

4648
return "backend/order/detail";
@@ -50,15 +52,15 @@ public String viewOrder(Long id, Model model, RedirectAttributes redirect) {
5052
public String frontendViewOrder(Long id, HttpSession session, Model model, RedirectAttributes redirect) {
5153
var customer = Utils.getCustomerSession(session);
5254

53-
var order = orderRepository.findByOrderIdAndCustomerIdAndStatus(id, customer.getCustomerId(), DefaultStatus.ACTIVE);
55+
var order = orderRepository.findByOrderIdAndCustomerIdAndStatusNot(id, customer.getCustomerId(), OrderStatus.DELETED);
5456
if (DataUtils.isNullOrEmpty(order)) {
5557
redirect.addFlashAttribute("error", "Hóa đơn không tồn tại");
5658

5759
return "redirect:/";
5860
}
5961
model.addAttribute("order", order);
6062

61-
var orderDetails = orderDetailRepository.findAllByOrderIdStatus(id, DefaultStatus.ACTIVE);
63+
var orderDetails = orderDetailRepository.findAllByOrderIdStatusNot(id, OrderStatus.DELETED);
6264
model.addAttribute("orderDetails", orderDetails);
6365

6466
return "frontend/order-success";
@@ -68,7 +70,7 @@ public String frontendViewOrder(Long id, HttpSession session, Model model, Redir
6870
public String frontendListOrder(HttpSession session, Model model, RedirectAttributes redirect) {
6971
var customer = Utils.getCustomerSession(session);
7072

71-
var orders = orderRepository.findAllByCustomerIdAndStatus(customer.getCustomerId(), DefaultStatus.ACTIVE);
73+
var orders = orderRepository.findAllByCustomerIdAndStatusNot(customer.getCustomerId(), OrderStatus.DELETED);
7274
model.addAttribute("orders", orders);
7375

7476
return "frontend/order";

src/main/resources/templates/frontend/checkout.html

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ <h3>Chi tiết hóa đơn</h3>
5353
</div>
5454
<div class="form-group col-md-12 col-sm-12 col-xs-12">
5555
<div class="field-label">Tỉnh/Thành phố <span>*</span></div>
56-
<select name="province" th:field="*{province}" id="provinces" required>
56+
<select name="provinceId" th:field="*{provinceId}" id="provinces" required>
5757
<option value="">--Tỉnh/Thành phố--</option>
5858
</select>
5959
</div>
6060
<div class="form-group col-md-12 col-sm-12 col-xs-12">
6161
<div class="field-label">Quận/Huyện <span>*</span></div>
62-
<select name="district" th:field="*{district}" id="districts" required>
62+
<select name="districtId" th:field="*{districtId}" id="districts" required>
6363
<option value="">--Quận/Huyện--</option>
6464
</select>
6565
</div>
6666
<div class="form-group col-md-12 col-sm-12 col-xs-12">
6767
<div class="field-label">Phường/Xã <span>*</span></div>
68-
<select name="ward" th:field="*{ward}" id="wards" required>
68+
<select name="wardId" th:field="*{wardId}" id="wards" required>
6969
<option value="">--Xã/Phường--</option>
7070
</select>
7171
</div>
@@ -95,7 +95,7 @@ <h3>Chi tiết hóa đơn</h3>
9595
<li>Vận chuyển
9696
<div class="shipping">
9797
<div class="shopping-option" th:each="transport : ${transports}">
98-
<input name="transportId" th:field="*{transportId}" th:id="'transport-' + ${transport.transportId}" type="radio" th:value="${transport.transportId}" th:checked="${transportStat.index == 0}">
98+
<input name="transportId" th:id="'transport-' + ${transport.transportId}" type="radio" th:value="${transport.transportId}" th:checked="${transportStat.index == 0}">
9999
<label th:for="'transport-' + ${transport.transportId}" th:text="${transport.name}"></label>
100100
</div>
101101
</div>
@@ -111,7 +111,7 @@ <h3>Chi tiết hóa đơn</h3>
111111
<ul>
112112
<li th:each="payment : ${payments}">
113113
<div class="radio-option">
114-
<input name="paymentId" th:field="*{paymentId}" th:id="'payment-' + ${payment.paymentId}" type="radio" th:value="${payment.paymentId}" th:checked="${paymentStat.index == 0}">
114+
<input name="paymentId" th:id="'payment-' + ${payment.paymentId}" type="radio" th:value="${payment.paymentId}" th:checked="${paymentStat.index == 0}">
115115
<label th:for="'payment-' + ${payment.paymentId}" th:text="${payment.name}"></label>
116116
</div>
117117
</li>
@@ -141,56 +141,75 @@ <h3>Chi tiết hóa đơn</h3>
141141
<script th:inline="javascript">
142142
/*<![CDATA[*/
143143

144+
let provinceId = [[${checkout.provinceId}]];
145+
let districtId = [[${checkout.districtId}]];
146+
let wardId = [[${checkout.wardId}]];
147+
148+
// preload
144149
$(function() {
150+
$('#districts').find('option:not(:first)').remove();
151+
$('#wards').find('option:not(:first)').remove();
152+
loadProvince();
153+
loadDistrict(provinceId);
154+
loadWard(districtId);
155+
});
156+
157+
$("#provinces").on('change', function(e) {
158+
e.preventDefault();
159+
let code = $(this).find(':selected').data("code");
160+
$('#districts').find('option:not(:first)').remove();
161+
$('#wards').find('option:not(:first)').remove();
162+
loadDistrict(code);
163+
});
164+
165+
$("#districts").on('change', function(e) {
166+
e.preventDefault();
167+
let code = $(this).find(':selected').data("code");
168+
$('#wards').find('option:not(:first)').remove();
169+
loadWard(code);
170+
});
171+
172+
function loadProvince() {
145173
$.ajax({
146174
url: "[(@{/dvhc/provinces})]",
147175
method: 'GET',
148176
success: function(data) {
149-
$('#districts').find('option:not(:first)').remove();
150-
$('#wards').find('option:not(:first)').remove();
151177
$.each(data, function(key, value) {
152178
$("#provinces").append(
153-
"<option data-code=" + value.code + " value=" + value.name + ">" + value.name + "</option>"
179+
"<option "+ (provinceId === value.code ? "selected" : "") +" data-code=\"" + value.code + "\" value=\"" + value.code + "\">" + value.name + "</option>"
154180
);
155181
});
156182
}
157183
});
158-
});
184+
}
159185

160-
$("#provinces").on('change', function(e) {
161-
e.preventDefault();
162-
let code = $(this).find(':selected').data("code");
186+
function loadDistrict(code) {
163187
$.ajax({
164188
url: "[(@{/dvhc/provinces/})]" + code,
165189
method: 'GET',
166190
success: function(data) {
167-
$('#districts').find('option:not(:first)').remove();
168-
$('#wards').find('option:not(:first)').remove();
169191
$.each(data, function(key, value) {
170192
$("#districts").append(
171-
"<option data-code=" + value.code + " value=" + value.name + ">" + value.name + "</option>"
193+
"<option "+ (districtId === value.code ? "selected" : "") +" data-code=\"" + value.code + "\" value=\"" + value.code + "\">" + value.name + "</option>"
172194
);
173195
});
174196
}
175197
});
176-
});
198+
}
177199

178-
$("#districts").on('change', function(e) {
179-
e.preventDefault();
180-
let code = $(this).find(':selected').data("code");
200+
function loadWard(code) {
181201
$.ajax({
182202
url: "[(@{/dvhc/districts/})]" + code,
183203
method: 'GET',
184204
success: function(data) {
185-
$('#wards').find('option:not(:first)').remove();
186205
$.each(data, function(key, value) {
187206
$("#wards").append(
188-
"<option data-code=" + value.code + " value=" + value.name + ">" + value.name + "</option>"
207+
"<option "+ (wardId === value.code ? "selected" : "") +" data-code=\"" + value.code + "\" value=\"" + value.code + "\">" + value.name + "</option>"
189208
);
190209
});
191210
}
192211
});
193-
});
212+
}
194213
/*]]>*/
195214
</script>
196215
</div>

src/main/resources/templates/frontend/order-success.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ <h4>Thông tin người nhận</h4>
147147
<li th:text="${order.phone}"></li>
148148
<li th:text="${order.email}"></li>
149149
<li th:text="${
150-
(order.address != null ? order.address + ', ' : '') + (order.ward != null ? order.ward + ', ' : '') + (order.district != null ? order.district + ', ' : '') + (order.province != null ? order.province : '')}"></li>
150+
(order.address != null ? order.address + ', ' : '') + (order.wardId != null ? order.wardId + ', ' : '') + (order.districtId != null ? order.districtId + ', ' : '') + (order.provinceId != null ? order.provinceId : '')}"></li>
151151
</ul>
152152
</div>
153153
</div>

0 commit comments

Comments
 (0)