Browse Source

댓글, 대댓글 작성 기능

master
이준희 3 years ago
parent
commit
5a10afd9bd
  1. 18
      src/main/java/com/palnet/board/app/jh/controller/JunheeReplyRestController.java
  2. 16
      src/main/java/com/palnet/board/app/jh/dto/JunheeRequestReply.java
  3. 19
      src/main/java/com/palnet/board/app/jh/mapper/JunheeBoardMapper.xml
  4. 7
      src/main/java/com/palnet/board/app/jh/mapper/JunheeReplyMapper.java
  5. 43
      src/main/java/com/palnet/board/app/jh/mapper/JunheeReplyMapper.xml
  6. 5
      src/main/java/com/palnet/board/app/jh/service/JunheeReplyService.java
  7. 21
      src/main/java/com/palnet/board/app/jh/service/JunheeReplyServiceImpl.java
  8. 132
      src/main/webapp/WEB-INF/view/jh/board/detailView.jsp
  9. 20
      src/main/webapp/WEB-INF/view/jh/board/write.jsp

18
src/main/java/com/palnet/board/app/jh/controller/JunheeReplyRestController.java

@ -8,10 +8,13 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.palnet.board.app.jh.dto.JunheeReplyDto; import com.palnet.board.app.jh.dto.JunheeReplyDto;
import com.palnet.board.app.jh.dto.JunheeRequestBoard;
import com.palnet.board.app.jh.dto.JunheeRequestReply;
import com.palnet.board.app.jh.dto.JunheeResponseReply; import com.palnet.board.app.jh.dto.JunheeResponseReply;
import com.palnet.board.app.jh.service.JunheeReplyService; import com.palnet.board.app.jh.service.JunheeReplyService;
@ -33,4 +36,19 @@ public class JunheeReplyRestController {
return ResponseEntity.status(HttpStatus.OK).body(responseReply); return ResponseEntity.status(HttpStatus.OK).body(responseReply);
} }
@PostMapping("/commentWrite")
public ResponseEntity<Integer> writeCommnet(JunheeRequestReply requestReply) throws Exception {
int result = ReplyService.writeCommnet(requestReply);
return ResponseEntity.status(HttpStatus.OK).body(result);
}
@PostMapping("/replyWrite")
public ResponseEntity<Integer> writeReply(JunheeRequestReply requestReply) throws Exception {
int result = ReplyService.writeReply(requestReply);
return ResponseEntity.status(HttpStatus.OK).body(result);
}
} }

16
src/main/java/com/palnet/board/app/jh/dto/JunheeRequestReply.java

@ -0,0 +1,16 @@
package com.palnet.board.app.jh.dto;
import lombok.Data;
import org.apache.ibatis.type.Alias;
@Data
public class JunheeRequestReply {
/*댓글 작성(insert)시 사용 */
int id;
int boardId;
int targetId;
String content;
String regUser;
String regDtm;
}

19
src/main/java/com/palnet/board/app/jh/mapper/JunheeBoardMapper.xml

@ -68,7 +68,6 @@
REG_USER, REG_USER,
REG_DTM, REG_DTM,
UPD_USER UPD_USER
<!-- UPD_DTM -->
FROM TB_COM_BOARD FROM TB_COM_BOARD
LIMIT #{endNum} LIMIT #{endNum}
OFFSET #{startNum} OFFSET #{startNum}
@ -79,24 +78,6 @@
FROM TB_COM_BOARD FROM TB_COM_BOARD
</select> </select>
<!-- <select id="prevInfo" resultType="com.palnet.board.app.jh.dto.JunheeBoardDto">
SELECT
ID,
TITLE
FROM TB_COM_BOARD
WHERE
ID = #{prevId}
</select>
<select id="nextInfo" resultType="com.palnet.board.app.jh.dto.JunheeBoardDto">
SELECT
ID,
TITLE
FROM TB_COM_BOARD
WHERE
ID = #{nextId}
</select> -->
<select id="boardPrevNext" resultType="com.palnet.board.app.jh.dto.JunheeResponsePrevNext"> <select id="boardPrevNext" resultType="com.palnet.board.app.jh.dto.JunheeResponsePrevNext">
SELECT ID, TITLE SELECT ID, TITLE
FROM TB_COM_BOARD FROM TB_COM_BOARD

7
src/main/java/com/palnet/board/app/jh/mapper/JunheeReplyMapper.java

@ -5,6 +5,7 @@ import java.util.List;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import com.palnet.board.app.jh.dto.JunheeReplyDto; import com.palnet.board.app.jh.dto.JunheeReplyDto;
import com.palnet.board.app.jh.dto.JunheeRequestReply;
import com.palnet.board.app.jh.dto.JunheeResponseReply; import com.palnet.board.app.jh.dto.JunheeResponseReply;
@Mapper @Mapper
@ -14,4 +15,10 @@ public interface JunheeReplyMapper {
List<JunheeReplyDto> targetDivide(int id) throws Exception; List<JunheeReplyDto> targetDivide(int id) throws Exception;
int writeCommnet(JunheeRequestReply requestReply) throws Exception;
int anonymousCount(int boardId) throws Exception;
int writeReply(JunheeRequestReply requestReply) throws Exception;
} }

43
src/main/java/com/palnet/board/app/jh/mapper/JunheeReplyMapper.xml

@ -25,5 +25,48 @@
FROM TB_COM_COMMENT FROM TB_COM_COMMENT
WHERE TARGET_ID = #{id} WHERE TARGET_ID = #{id}
</select> </select>
<insert id="writeCommnet">
INSERT INTO TB_COM_COMMENT(
ID,
BOARD_ID,
TARGET_ID,
CONTENT,
REG_USER,
REG_DATE
) VALUES (
null,
#{boardId},
null,
#{content},
#{regUser},
SYSDATE()
)
</insert>
<select id="anonymousCount" resultType="int">
SELECT COUNT(*)
FROM TB_COM_COMMENT
WHERE BOARD_ID = #{boardId}
</select>
<insert id="writeReply">
INSERT INTO TB_COM_COMMENT(
ID,
BOARD_ID,
TARGET_ID,
CONTENT,
REG_USER,
REG_DATE
) VALUES (
null,
#{boardId},
#{targetId},
#{content},
#{regUser},
SYSDATE()
)
</insert>
</mapper> </mapper>

5
src/main/java/com/palnet/board/app/jh/service/JunheeReplyService.java

@ -3,10 +3,15 @@ package com.palnet.board.app.jh.service;
import java.util.List; import java.util.List;
import com.palnet.board.app.jh.dto.JunheeReplyDto; import com.palnet.board.app.jh.dto.JunheeReplyDto;
import com.palnet.board.app.jh.dto.JunheeRequestReply;
import com.palnet.board.app.jh.dto.JunheeResponseReply; import com.palnet.board.app.jh.dto.JunheeResponseReply;
public interface JunheeReplyService { public interface JunheeReplyService {
List<JunheeResponseReply> viewReply(int boardId) throws Exception; List<JunheeResponseReply> viewReply(int boardId) throws Exception;
int writeCommnet(JunheeRequestReply requestReply) throws Exception;
int writeReply(JunheeRequestReply requestReply) throws Exception;
} }

21
src/main/java/com/palnet/board/app/jh/service/JunheeReplyServiceImpl.java

@ -8,6 +8,7 @@ import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.palnet.board.app.jh.dto.JunheeReplyDto; import com.palnet.board.app.jh.dto.JunheeReplyDto;
import com.palnet.board.app.jh.dto.JunheeRequestReply;
import com.palnet.board.app.jh.dto.JunheeResponseReply; import com.palnet.board.app.jh.dto.JunheeResponseReply;
import com.palnet.board.app.jh.mapper.JunheeReplyMapper; import com.palnet.board.app.jh.mapper.JunheeReplyMapper;
@ -40,4 +41,24 @@ public class JunheeReplyServiceImpl implements JunheeReplyService {
return responseReply; return responseReply;
} }
@Override
public int writeCommnet(JunheeRequestReply requestReply) throws Exception {
int regUser = ReplyMapper.anonymousCount(requestReply.getBoardId()) + 1;
requestReply.setRegUser("익명" + regUser);
int result = ReplyMapper.writeCommnet(requestReply);
return result;
}
@Override
public int writeReply(JunheeRequestReply requestReply) throws Exception {
int regUser = ReplyMapper.anonymousCount(requestReply.getBoardId()) + 1;
requestReply.setRegUser("익명" + regUser);
int result = ReplyMapper.writeReply(requestReply);
return result;
}
} }

132
src/main/webapp/WEB-INF/view/jh/board/detailView.jsp

@ -62,8 +62,8 @@
<div class="comment-box"> <div class="comment-box">
<span class="total"></span> <span class="total"></span>
<div class="comment-input"> <div class="comment-input">
<textarea placeholder="댓글을 입력해주세요"></textarea> <textarea id="cmtContent" name="cmtContent" placeholder="댓글을 입력해주세요"></textarea>
<button type="button">등록</button> <button id="cmtSave" type="button">등록</button>
</div> </div>
<!-- <div class="comment-view" id="test3"> <!-- <div class="comment-view" id="test3">
@ -71,7 +71,7 @@
class="date">05.02 13:00</span><span>답글</span></span> <span class="text" id="text">여기는 댓글 class="date">05.02 13:00</span><span>답글</span></span> <span class="text" id="text">여기는 댓글
정보3333!</span> 정보3333!</span>
** **
<div class="reply-box"> <div class="reply-box">
<button type="button">답글</button> <button type="button">답글</button>
<div class="reply-input"> <div class="reply-input">
<textarea placeholder="댓글을 입력해주세요"></textarea> <textarea placeholder="댓글을 입력해주세요"></textarea>
@ -82,11 +82,6 @@
class="date">05.02 13:00</span></span> <span class="text">여기는 댓글의 class="date">05.02 13:00</span></span> <span class="text">여기는 댓글의
댓글(답글)정보!3333</span> 댓글(답글)정보!3333</span>
</div> </div>
<div class="reply-view">
<span class="user"><span class="id">익명6</span><span
class="date">05.02 13:00</span></span> <span class="text">여기는 댓글의
댓글(답글)정보!3333</span>
</div>
</div> </div>
** **
</div> --> </div> -->
@ -186,48 +181,123 @@
success: function(response) { success: function(response) {
console.log(response) console.log(response)
/* comment */ /* comment (each를 역으로 가져와서 idx는 거꾸로.. 엉망)*/
$($(response).get().reverse()).each( function(i, item) { $($(response).get().reverse()).each( function(i, item) {
if(item.targetId==0) { if(item.targetId==0) {
var comment = var comment =
"<div class='comment-view' id='user" + (i+1) + "'>" + "<div class='comment-view' id='comment-view" + (i+1) + "'>" +
"<span class='user'>" + "<span class='user'>" +
"<span class='id'>" + item.regUser + "</span>" + "<span class='id'>" + item.regUser + "</span>" +
"<span class='date'>" + item.regDate + "</span>" + "<span class='date'>" + item.regDate + "</span>" +
"<span>답글</span>" + "<span onclick='replyInputCreate("+ (i+1) + "," + item.id +")'>답글</span>" +
"<input type='checkbox' name='replyCheck" + (i+1) + "' style='display:none'/>" +
"</span>" + "</span>" +
"<span class='text' id='text'>" + item.content + "</span>" + "<span class='text'>" + item.content + "</span>" +
"</div>" "</div>"
$(".comment-input").after(comment) $(".comment-input").after(comment)
/* reply */ var rb = $("<div class='reply-box' id='reply-box" + (i+1) + "'/>");
$.each(item.targetReplys, function(idx, it) { $(".comment-view").append(rb);
/* onclick으로 답글다는곳 나타나면 걔는 $('.reply-box').prepend(머시기) 하면 될듯 */
var rb = $("<div class='reply-box' id='user" + (i+1) + "'/>"); /* reply */
$(".comment-view").append(rb); $.each(item.targetReplys, function(idx, it) {
var reply =
var reply = "<div class='reply-view' id='reply-view" + (i+1) + "'>" +
"<div class='reply-view' id='user" + (i+1) + "'>" + "<span class='user'>" +
"<span class='user'>" + "<span class='id'>" + it.regUser + "</span>" +
"<span class='id'>" + it.regUser + "</span>" + "<span class='date'>" + it.regDate + "</span>" +
"<span class='date'>" + it.regDate + "</span>" + "</span>" +
"</span>" + "<span class='text'>" + it.content + "</span>" +
"<span class='text'>" + it.content + "</span>" + "</div>"
"</div>"
$('#reply-box' + (i+1)).append(reply)
$('#user' + (i+1)).append(reply) })
})
} }
}) })
/* 댓글 개수 */ /* 댓글 개수 */
var count = response.length var count = response.length
$('.total').text('댓글 ' + count); $('.total').text('댓글 ' + count);
} }
}) })
} }
/* 답글 클릭시 답글 입력창 생성 */
function replyInputCreate(index, id) {
var chk = document.querySelector("input[name=replyCheck" + index + "]").checked;
if(chk) {
document.querySelector("input[name=replyCheck" + index + "]").checked = false;
$('.reply-input').remove();
} else {
document.querySelector("input[name=replyCheck" + index + "]").checked = true;
var replyInput =
"<div class='reply-input'>" +
"<textarea id='replyContent' name='replyContent' placeholder='댓글을 입력해주세요'></textarea>" +
"<button class='reply-button' id='replySave' type='button'>등록</button>" +
"<span id='targetNum' style='display:none'>" + id + "</span>"+
"</div>"
$('#reply-box' + index).before(replyInput)
}
}
/* comment 작성 */
$(document).on("click", "#cmtSave", function() {
var boardNum = '<%=boardId%>';
var boardId = boardNum;
var content = $('.comment-input').find('textarea[id="cmtContent"]').val();
var obj = {
boardId: boardId,
content: content
}
$.ajax({
url: "/api/jh/reply/commentWrite",
type: "POST",
data: obj,
dataType: 'JSON',
success: function(res) {
if(res == '1') {
alert("등록 성공")
location.href="/view/jh/board/detailView/" + boardNum
}
}
})
})
/* reply 작성 */
$(document).on("click", "#replySave", function() {
var boardNum = '<%=boardId%>';
var boardId = boardNum;
var targetId = $('.reply-input').find('span[id="targetNum"]').text();
var content = $('.reply-input').find('textarea[id="replyContent"]').val();
var obj = {
boardId: boardId,
targetId: targetId,
content: content
}
$.ajax({
url: "/api/jh/reply/replyWrite",
type: "POST",
data: obj,
dataType: 'JSON',
success: function(res) {
if(res == '1') {
alert("등록 성공")
location.href="/view/jh/board/detailView/" + boardNum
}
}
})
})
/* 글 삭제 */ /* 글 삭제 */
$(document).on("click", "#delete", function() { $(document).on("click", "#delete", function() {

20
src/main/webapp/WEB-INF/view/jh/board/write.jsp

@ -71,25 +71,9 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
autoSetting() $('input[name=regDtm]').attr('value', dayjs().format('YYYY-MM-DD'))
}) })
/* 글번호 / 작성일 자동지정 */
function autoSetting() {
$.ajax({
url : "/api/jh/board/boardMaxId",
type : "GET",
dataType : 'TEXT',
success : function(response) {
var id = parseInt(response) + 1
console.log(id)
$('input[name=id]').attr('value', id)
$('input[name=regDtm]').attr('value', dayjs().format('YYYY-MM-DD'))
}
})
}
/* 저장 버튼 클릭 */ /* 저장 버튼 클릭 */
$(document).on("click", "#save", function() { $(document).on("click", "#save", function() {
checkAll(); checkAll();
@ -120,7 +104,7 @@
/* 글 저장 */ /* 글 저장 */
function setBoard() { function setBoard() {
var boardBox = $('#boardBox'); var boardBox = $('#boardBox');
var id = boardBox.find('input[id="id"]').val(); var id = 0;
var title = boardBox.find('input[id="title"]').val(); var title = boardBox.find('input[id="title"]').val();
var content = boardBox.find('textarea[id="content"]').val(); var content = boardBox.find('textarea[id="content"]').val();
var password = boardBox.find('input[id="password"]').val(); var password = boardBox.find('input[id="password"]').val();

Loading…
Cancel
Save