[Spring JPA 실습 #15] 주문목록 검색
- 📚 Spring/Spring JPA 실습
- 2021. 3. 13.
orderList.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments/header :: header"/>
<body>
<div class="container">
<div th:replace="fragments/bodyHeader :: bodyHeader"/>
<div>
<div>
<form th:object="${orderSearch}" class="form-inline">
<div class="form-group mb-2">
<input type="text" th:field="*{memberName}" class="form-control" placeholder="회원명"/>
</div>
<div class="form-group mx-sm-1 mb-2">
<select th:field="*{orderStatus}" class="form-control">
<option value="">주문상태</option>
<option th:each="status : ${T(com.jpabok.jpashop.domain.OrderStatus).values()}"
th:value="${status}"
th:text="${status}">option</option>
</select>
</div>
<button type="submit" class="btn btn-primary mb-2">검색</button>
</form>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>회원명</th>
<th>대표상품 이름</th>
<th>대표상품 주문가격</th>
<th>대표상품 주문수량</th>
<th>상태</th>
<th>일시</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${orders}">
<td th:text="${item.id}"></td>
<td th:text="${item.member.name}"></td>
<td th:text="${item.orderItems[0].item.name}"></td>
<td th:text="${item.orderItems[0].orderPrice}"></td>
<td th:text="${item.orderItems[0].count}"></td>
<td th:text="${item.status}"></td>
<td th:text="${item.orderDate}"></td>
<td>
<a th:if="${item.status.name() == 'ORDER'}" href="#" th:href="'javascript:cancel('+${item.id}+')'" class="btn btn-danger">CANCEL</a>
</td>
</tr>
</tbody>
</table>
</div>
<div th:replace="fragments/footer :: footer"/>
</div> <!-- /container -->
</body>
<script>
function cancel(id) {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "/orders/" + id + "/cancel");
document.body.appendChild(form);
form.submit();
}
</script>
</html>
OrderRepository.java
public List<Order> findAllByString(OrderSearch orderSearch) {
//language = JPQL
String jpql = "select o from Order o join o.member m";
boolean isFirstCondition = true;
//주문상태 검색
if(orderSearch.getOrderStatus() != null) {
if(isFirstCondition) {
jpql += " where";
isFirstCondition = false;
}else{
jpql += " and";
}
jpql += " o.status = :status";
}
//회원 이름 검색
if(StringUtils.hasText(orderSearch.getMemberName())) {
if(isFirstCondition) {
jpql += " where";
isFirstCondition = false;
}else{
jpql += " and";
}
jpql += " m.name like :name";
}
TypedQuery<Order> query = em.createQuery(jpql, Order.class).setMaxResults(1000);
if(orderSearch.getOrderStatus() != null) {
query = query.setParameter("status", orderSearch.getOrderStatus());
}
if(StringUtils.hasText(orderSearch.getMemberName())) {
query = query.setParameter("name", orderSearch.getMemberName());
}
return query.getResultList();
}
OrderService.java
/**
* 주문취소
*/
@Transactional
public void cancelOrder(Long orderId) {
//주문엔티티 조회
Order order = orderRepository.findOne(orderId);
//주문취소
order.cancel(); // 한줄로 끝난다.
}
//검색
public List<Order> findOrders(OrderSearch orderSearch) {
return orderRepository.findAllByString(orderSearch);
}
'📚 Spring > Spring JPA 실습' 카테고리의 다른 글
[Spring API 실습 #-02] 회원 수정 API (0) | 2021.03.14 |
---|---|
[Spring API 실습 #-01] 회원 등록 API (0) | 2021.03.14 |
[Spring JPA 실습 #14] 상품 주문 (0) | 2021.03.13 |
[Spring JPA 실습 #12] 상품 수정 JPA 실습 #12] 상품 수정 (0) | 2021.03.11 |
[Spring JPA 실습 #11] 상품 등록 및 조회 (0) | 2021.03.10 |