INTP의 멋대로 개발 세상

[📚상품 구매 사이트 3단계] 구매자 서버 만들기 - 7. 주문 취소하기 기능 만들기 본문

KDT 풀스택 국비 과정/파이널 프로젝트(미니)

[📚상품 구매 사이트 3단계] 구매자 서버 만들기 - 7. 주문 취소하기 기능 만들기

인팁구름 2023. 4. 24. 11:55

 

구매하기를 만들었으니 구매 취소 기능도 만들어야 한다.

구매하기 때, 구매수량만큼 상품 재고(Qty) 값을 차감시켰는데,

취소를 하면 다시 구매수량을 상품 재고에 더해주어야 하는 게 핵심 내용이다🙃

 


🎤 코드 리뷰 🎤

 

 

구매하기 기능이랑 큰 차이는 없다.

(매개변수 productId는 필요없어서 삭제했다)

 

로그인 한 유저만 삭제할 수 있도록 한다.

 

 

코드 순서가 좀 이상하지만(?) 구매목록 페이지를 표시해줄 것이기 때문에 userId가 필요해서 변수로 만들어 주었다.

(redirect라 하지만 페이지 변화는 없다.. 구매목록 페이지에서 삭제하고 구매목록 페이지를 보여주는 뭐 그런 상태..)

 

(매개변수 productId는 필요없어서 삭제했다)


자, 이제 본격적으로 구매 취소 기능을 만들어 보자😚🤗

먼저 ordersId를 findById 메서드에 넣어준다

 

ordersRepository

findById 메서드는 ordersId를 매개변수로 받고, Orders 타입으로 리턴하게 되어있다.

 

 

매개변수로 받은 ordersId는 orders 테이블에서 해당 주문 Id에 대한 모든 정보를 찾는 데에 사용된다.

 

 

 

가져온 정보를 담은 orders를 다시 productQtyReupdate 메서드에 넣어준다.

productQtyReupdate는 productRepository의 메서드이다 ❗

orders 정보를 이용해서 product 정보를 업데이트 해 주는거니까!

 

productRepository

구매와 다르게 Reupdate라는 이름을 붙여주었다.

 

 

쿼리문은 구매하기와 똑같고 - 였던 것을 + 로 바꾸어 주면 끝이다!!

(findById로 찾은 orders의 정보에 productId 데이터도 들어있기 때문에 따로  매개변수로 넣지 않아도 된다!)

 

 

 

ordersId를 이용해서 주문 정보를 삭제하자.

ordersId가 주문 정보를 포함한 PK 이므로 Id를 삭제해 주면 된다!!

 

ordersRepository
orders.xml

매개변수로 받은 ordersId와 같은 정보의 컬럼인 orders 테이블을 삭제📦📦

 


취소하기 버튼 form 태그의 주소와 컨트롤러의 주소를 일치시켜 준다!

 

 

@PostMapping("/orderList/delete")
    public String deleteOrder(Integer ordersId) {

        // 로그인 한 사람만
        User principal = (User) session.getAttribute("principal");
        if (principal == null) {
            throw new CustomException("로그인을 먼저 해 주세요.", HttpStatus.FORBIDDEN);
        }

        int userId = principal.getUserId();

        System.out.println("userId : " + userId);

        Orders orders = ordersRepository.findById(ordersId);
        productRepository.productQtyReupdate(orders);

        // 주문 정보 삭제
        ordersRepository.deleteById(ordersId);

        return "redirect:/orderList/" + userId;
    }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%@ include file="../layout/header.jsp" %>
        <div class="text-center m-4">
            <h1>구매목록 페이지</h1>
        </div>
        <div class="container">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>주문번호</th>
                        <th>상품명</th>
                        <th>상품가격</th>
                        <th>구매수량</th>
                        <th>가격</th>
                        <th>비고</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${orderedProduct}" var="op" varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${op.ordersName}</a></td>
                            <td>${op.ordersPrice}원</td>
                            <td>${op.ordersQty}개</td>
                            <td>${op.ordersPrice * op.ordersQty}원</td>
                            <td>
                                <form action="/orderListForm/delete" method="post">
                                    <input name="ordersId" type="hidden" value="${op.ordersId}">
                                    <button class="btn btn-success btn-sm" type="submit">취소하기</button>
                                </form>
                            </td>

                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>

        <%@ include file="../layout/footer.jsp" %>

 

Comments