이번에는 주로 AJAX를 활용하는 게시글 내 댓글 작성을 구현하고자 한다.
게시글에 댓글을 함께 다는데, 댓글을 작성할 때 VIEW 상에 영향받지 않고 DB에 댓글을 등록하고 게시글 상세화면으로 유지되면 된다.
$("#btn-comment-write").click(function() {
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
var nowUser = $("#nowUser").val();
var comment = $("#comment").val();
if (confirm("댓글을 작성하시겠습니까?")) {
$.ajax({
type : "post",
url : "/comment/write",
beforeSend : function(xhr) {xhr.setRequestHeader(header, token);},
data : JSON.stringify({
"boardId" : $("#boardId").val(),
"commentWriter" : $("#nowUser").val(),
"comment" : $("#comment").val()
}),
contentType : "application/json; charset=UTF-8",
success : function() {
alert("댓글이 작성되었습니다.");
window.location.href="/board/" + $("#boardId").val();
}
})
}
})
@ResponseBody
@PostMapping("/comment/write")
public void commentWrite(@RequestBody Map<String, Object> map) {
CommentDTO commentDTO = new CommentDTO();
commentDTO.setBid(Long.valueOf((String) map.get("boardId")));
commentDTO.setCommentWriter((String) map.get("commentWriter"));
commentDTO.setComment((String) map.get("comment"));
commentDTO.setCommRegTime(String.valueOf(LocalDate.now()));
commentDTO.setCommModTime(String.valueOf(LocalDate.now()));
commentService.commentWrite(commentDTO);
}
public void commentWrite(CommentDTO commentDTO) {
CommentEntity commentEntity = new CommentEntity();
commentEntity.setBid(commentDTO.getBid());
commentEntity.setCommentWriter(commentDTO.getCommentWriter());
commentEntity.setComment(commentDTO.getComment());
commentEntity.setCommRegTime(commentDTO.getCommRegTime());
commentEntity.setCommModTime(commentDTO.getCommModTime());
commentRepository.save(commentEntity);
}
게시글 상세 VIEW에서 댓글을 닮

댓글 작성 후 게시글 상세 VIEW

댓글 삭제
댓글을 삭제하는 로직은 댓글 작성자와 현재 로그인한 유저의 아이디가 같을 때 삭제할 지 여부를 물어보게 하도록 하면 된다.
var commentDeleteButtons = document.querySelectorAll(".btn-comment-delete");
var commentIds = document.querySelectorAll(".commentId");
var commentWriters = document.querySelectorAll(".commentWriter");
var nowUser = $("#nowUser").val();
for(let i = 0; i < commentDeleteButtons.length; i++) {
commentDeleteButtons[i].addEventListener("click", function() {
var commentWriter = commentWriters[i].value;
var commentId = commentIds[i].value;
if (nowUser === commentWriter) {
if (confirm("댓글을 삭제하시겠습니까?")) {
$.ajax({
type : "get",
url : "/comment/delete?cid=" + commentId,
contentType : "json",
success : function() {
alert("삭제했습니다!!");
window.location.href = "/board/" + $("#boardId").val();
}
});
}
} else {alert("댓글 작성자가 아닙니다.");}
});
}
@ResponseBody
@GetMapping("/comment/delete")
public void commentDelete(@RequestParam Long cid) {
commentService.commentDelete(cid);
}
public void commentDelete(Long cid) {
commentRepository.deleteById(cid);
}
댓글 삭제 VIEW

댓글 삭제 후 VIEW

댓글 수정
댓글을 수정하는 방법은 댓글의 내용을 보여주는 태그를 textarea와 같은 데이터를 입력받을 수 있는 태그로 바꾸고 해당 태그에 입력된 내용으로 댓글을 변경하면 된다.
var commentIds = document.querySelectorAll(".commentId");
var commentWriters = document.querySelectorAll(".commentWriter");
var nowUser = $("#nowUser").val();
var commentModifyButtons = document.querySelectorAll(".btn-comment-modify");
for (let j = 0; j < commentModifyButtons.length; j++) {
commentModifyButtons[j].addEventListener("click", (e) => {
e.preventDefault();
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
var commentWriter = commentWriters[j].value;
var commentId = commentIds[j].value;
if (nowUser === commentWriter) {
if (confirm("댓글을 수정하시겠습니까?")) {
var nowElement = document.getElementById("comm_" + commentId);
var beforeValue = nowElement.textContent;
console.log(beforeValue);
nowElement.value = "";
nowElement.innerHTML = "<textarea id='comm_modi_" + commentId + "'>" + beforeValue + "</textarea><br><button type='button' id='modi_btn_" + commentId + "' class='btn btn-secondary'>수정</button>"
document.getElementById("modi_btn_" + commentId).addEventListener("click", function() {
$.ajax({
type : "post",
url : "/comment/update",
beforeSend : function(xhr) {xhr.setRequestHeader(header, token);},
data : JSON.stringify({
"commentID" : commentId,
"commentUpdate" : document.getElementById("comm_modi_" + commentId).value
}),
contentType : "application/json; charset=UTF-8",
success : function() {
window.location.href = "/board/" + $("#boardId").val();
}
})
});
}
} else {alert("댓글 작성자가 아닙니다.");}
})
}
@ResponseBody
@PostMapping("/comment/update")
public void commentUpdate(@RequestBody Map<String, Object> map) {
Long cid = Long.valueOf((String) map.get("commentID"));
CommentDTO commentDTO = commentService.getComment(cid);
commentDTO.setComment((String) map.get("commentUpdate"));
commentDTO.setCommModTime(String.valueOf(LocalDate.now()));
서비스단을 호출하여 변경된 댓글을 DB에 저장한다.
commentService.commentUpdate(commentDTO);}
public void commentUpdate(CommentDTO commentDTO) {
CommentEntity commentEntity = new CommentEntity();
commentEntity.setId(commentDTO.getId());
commentEntity.setBid(commentDTO.getBid());
commentEntity.setCommentWriter(commentDTO.getCommentWriter());
commentEntity.setComment(commentDTO.getComment());
commentEntity.setCommRegTime(commentDTO.getCommRegTime());
commentEntity.setCommModTime(commentDTO.getCommModTime());
commentRepository.save(commentEntity);}
댓글 수정창 호출

댓글 수정

댓글 수정 후 VIEW

구현 후 필요한 개선사항
댓글이 여러 개인 경우, 수정 버튼을 모두 누르면 모든 댓글에 대해 수정창이 나타나게 되므로, 이러한 점이 수정이 필요한데 이는 자바스크립트 상에서 수정 하나를 누를 때 모든 태그들을 원상복구 시킨 후에 해당 댓글에 대해서만 textarea로 바꾸도록 하면 될 것이다.

다음으로 구현해볼 사항
게시글을 등록할 때 사진을 등록하는 것 그리고 RestfulAPI를 활용할 때 PutMapping이나 DeleteMapping을 활용하는 것을 구현할 것이다.