1) ReplyController
@RequestMapping("/usr/reply/doWrite")
@ResponseBody
public String doWrite(String relTypeCode, int relId, String body, String replaceUri) {
if (Ut.empty(relTypeCode)) {
return rq.jsHistoryBack("relTypeCode을(를) 입력해주세요");
}
if (Ut.empty(relId)) {
return rq.jsHistoryBack("relId을(를) 입력해주세요");
}
if (Ut.empty(body)) {
return rq.jsHistoryBack("body을(를) 입력해주세요");
}
ResultData writeReplyRd = replyService.writeReply(rq.getLoginedMemberId(),relTypeCode,relId,body);
if (Ut.empty(replaceUri)) {
switch (relTypeCode) {
case "article":
replaceUri = Ut.f("../article/detail?id=%d", relId);
break;
}
}
return rq.jsReplace(writeReplyRd.getMsg(), replaceUri);
댓글 작성 폼
으로 댓글을 작성을 시도한다면 onsubmit="ReplyWrite__submitForm(this); return false;
속성을 통해 빈 내용 시 제출 하지 않는다.uri로 요청시
relTypeCode,relId ,body에 대한 데이터가 없다면 뒤로돌아가게 구현2) ReplyService
public ResultData writeReply(int actorId, String relTypeCode, int relId, String body) {
replyRepository.writeReply(actorId,relTypeCode, relId, body);
int id = replyRepository.getLastInsertId();
return ResultData.from("S-1", Ut.f("%d번 댓글이 등록되었습니다", id), "id", id);
}
3) ReplyRepository
@Insert("""
<script>
INSERT INTO reply
SET regDate = NOW(),
updateDate = NOW(),
memberId = #{actorId},
relTypeCode = #{relTypeCode},
relId = #{relId},
`body` = #{body}
</script>
""")
void writeReply(int actorId, String relTypeCode, int relId, String body);
@Select("""
<script>
SELECT LAST_INSERT_ID()
</script>
""")
int getLastInsertId();
1) Reply (vo)
public class Reply {
private int id;
private String regDate;
private String updateDate;
private int memberId;
private String relTypeCode;
private int relId;
private String body;
private int goodReactionPoint;
private int badReactionPoint;
private String extra__writerName;
private boolean extra__actorCanDelete;
private boolean extra__actorCanModify;
goodReactionPoint
, badReactionPoint
: 댓글의 좋아요/싫어요 포인트extra__actorCanDelete
, extra__actorCanModify
: 댓글 삭제, 수정 권한 체크2) ArticleController showDetail()
List<Reply> replies = replyService.getForPrintReplies("article",id);
int repliesCount = replies.size();
model.addAttribute("repliesCount",repliesCount);
3) ReplyRepository
@Select("""
<script>
SELECT R.*, M.nickname AS extra__writerName
FROM reply AS R
LEFT JOIN `member` AS M
ON R.memberId = M.id
WHERE R.relTypeCode = #{relTypeCode}
AND R.relId = #{relId}
</script>
""")
List<Reply> getForPrintReplies(String relTypeCode, int relId);
관련데이터
) , relId(관련 번호
) 즉 게시물데이터, 게시물 번호를 토대로 게시물의 댓글 리스트를 가져오는 쿼리문ALTER TABLE `SB_AM`.`reply` ADD INDEX (`relTypeCode` , `relId`);
public String getForPrintBody() {
return body.replaceAll("\n", "<br>");
}