Springboot/블로그만들기

[Springboot] 블로그 만들기 (41)_ 댓글 삭제

21종 2023. 12. 8. 22:37

detail.jsp

<!-- 댓글 리스트 영역 -->
<div class="card">
    <div class="card-header">댓글 리스트</div>
    <ul id="reply-box" class="list-group">
        <!-- 내가 만든 클래스는 작대기 두개 -->
        <c:forEach var="reply" items="${ board.replys }">
            <li id="reply-${reply.id }" class="list-group-item d-flex justify-content-between">
                <div>${ reply.content }</div>
                <div class="d-flex">
                    <div class="font-italic">작성자 : ${ reply.user.userName } &nbsp;</div>
                    <c:if test="${reply.user.id == principal.user.id}">
                        <button onClick="index.replyDelete(${board.id}, ${ reply.id })" class="badge">삭제</button>
                    </c:if>
                </div>
            </li>
        </c:forEach>
    </ul>
</div>

board.js

let index = {
	init: function() {

	}

	, replyDelete: function(boardId, replyId) {

		$.ajax({
			type: "DELETE",
			url: `/api/board/${boardId}/reply/${replyId}`,
			dataType: "json"
		}).done(function(resp) {
			alert("댓글 삭제가 완료되었습니다.");
			location.href = `/board/${boardId}`;
		}).fail(function(error) {
			alert(JSON.stringify(error));
		});
	}
}
index.init();

BoardApiController

@DeleteMapping("/api/board/{boardId}/reply/{replyId}")
public ResponseDto<Integer> replyDelete(@PathVariable int replyId){
    boardService.댓글삭제(replyId);
    return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);
}

BoardService

@Transactional
public void 댓글삭제(int replyId) {
    replyRepository.deleteById(replyId);
}

참고 유튜브 (메타코딩님 강의)

https://youtu.be/pT2cYoQiImY?si=IqLj3NHbH_p1drQV