본문 바로가기
Backend

[실전! 스프링부트와 JPA활용] 웹계층 개발 - 주문 목록 검색, 취소 <완강!>

by 햣둘 2025. 4. 16.

주문 목록 검색 컨트롤러

OrderController 코드에 주문 목록 검색 기능을 추가하자

package jpabook.jpashop.web;

@Controller
@RequiredArgsConstructor
public class OrderController {

	// 기존 코드 ...
	
    	@GetMapping("/orders")
	public String orderList(@ModelAttribute("orderSearch") OrderSearch orderSearch, Model model) {
    
    	List<Order> orders = orderService.findOrders(orderSearch);
    	model.addAttribute("orders", orders);

	    return "order/orderList";
	}
}

 

OrderService 코드 변경

- OrderService에 findOrders() 기능을 추가하자

- 기존 findOrders 주석을 제거했다면 findAllByString()을 호출하도록 변경하자

// 주문 검색
public List<Order> findOrders(OrderSearch orderSearch) {
    return orderRepository.findAllByString(orderSearch);
}

 

주문 목록 검색 화면 (templates/order/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="formcontrol"
                           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(jpabook.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>

 

주문 취소

기존 OrderController에 주문 취소 기능을 추가하자

package jpabook.jpashop.web;

@Controller
@RequiredArgsConstructor
public class OrderController {

	@PostMapping("/orders/{orderId}/cancel")
	public String cancelOrder(@PathVariable("orderId") Long orderId) {
    	
        	orderService.cancelOrder(orderId);
    		return "redirect:/orders";
	}
}

 

<실전! 스프링부트와 JPA활용 1편> 완강!