jspblog : write reply
This commit is contained in:
@@ -1,4 +1,31 @@
|
||||
package com.example.jspblog.domain.reply;
|
||||
|
||||
import com.example.jspblog.config.DB;
|
||||
import com.example.jspblog.domain.reply.dto.SaveReqDto;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
public class ReplyDao {
|
||||
public int save(SaveReqDto dto) {
|
||||
String sql = "insert into reply(userId, boardId, content, createDate) values(?, ?, ?, now())";
|
||||
Connection conn = DB.getConnection();
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
if (conn != null) {
|
||||
try {
|
||||
pstmt = conn.prepareStatement(sql);
|
||||
pstmt.setInt(1, dto.getUserId());
|
||||
pstmt.setInt(2, dto.getBoardId());
|
||||
pstmt.setString(3, dto.getContent());
|
||||
int result = pstmt.executeUpdate();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
DB.close(conn, pstmt);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.jspblog.domain.reply.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class SaveReqDto {
|
||||
|
||||
private int userId;
|
||||
private int boardId;
|
||||
private String content;
|
||||
}
|
||||
@@ -1,4 +1,17 @@
|
||||
package com.example.jspblog.service;
|
||||
|
||||
import com.example.jspblog.domain.reply.ReplyDao;
|
||||
import com.example.jspblog.domain.reply.dto.SaveReqDto;
|
||||
|
||||
public class ReplyService {
|
||||
|
||||
private final ReplyDao replyDao;
|
||||
|
||||
public ReplyService() {
|
||||
this.replyDao = new ReplyDao();
|
||||
}
|
||||
|
||||
public int 댓글쓰기(SaveReqDto dto) {
|
||||
return replyDao.save(dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package com.example.jspblog.web;
|
||||
|
||||
import com.example.jspblog.domain.reply.dto.SaveReqDto;
|
||||
import com.example.jspblog.service.BoardService;
|
||||
import com.example.jspblog.service.ReplyService;
|
||||
import com.example.jspblog.util.Script;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(name = "reply")
|
||||
@WebServlet("/reply")
|
||||
public class ReplyController extends HttpServlet {
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
doProcess(request, response);
|
||||
@@ -19,11 +25,29 @@ public class ReplyController extends HttpServlet {
|
||||
|
||||
protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String cmd = request.getParameter("cmd");
|
||||
ReplyService replyService = new ReplyService();
|
||||
HttpSession session = request.getSession();
|
||||
|
||||
if(cmd.equals("loginForm")) {
|
||||
switch (cmd) {
|
||||
case "save": {
|
||||
int userId = Integer.parseInt(request.getParameter("userId"));
|
||||
int boardId = Integer.parseInt(request.getParameter("boardId"));
|
||||
String content = request.getParameter("content");
|
||||
|
||||
} else if (cmd.equals("login")) {
|
||||
SaveReqDto dto = SaveReqDto.builder()
|
||||
.userId(userId)
|
||||
.boardId(boardId)
|
||||
.content(content)
|
||||
.build();
|
||||
|
||||
int result = replyService.댓글쓰기(dto);
|
||||
if (result == 1) {
|
||||
response.sendRedirect("/jspblog/board?cmd=detail&id=" + boardId);
|
||||
} else {
|
||||
Script.back(response, "댓글쓰기 실패");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,9 +31,13 @@
|
||||
<div class="panel panel-info">
|
||||
<div class="panel-heading m-2"><b>Comment</b></div>
|
||||
<div class="panel-body">
|
||||
<textarea id="reply__write__form" class="form-control" placeholder="write a comment..." rows="2"></textarea>
|
||||
<br>
|
||||
<button onclick="" class="btn btn-primary pull-right">댓글쓰기</button>
|
||||
<form action="${pageContext.request.contextPath}/reply?cmd=save" method="post">
|
||||
<input type="hidden" name="userId" value="${sessionScope.principal.id}">
|
||||
<input type="hidden" name="boardId" value="${detail.id}">
|
||||
<textarea id="reply__write__form" name="content" class="form-control" placeholder="write a comment..." rows="2"></textarea>
|
||||
<br>
|
||||
<button class="btn btn-primary pull-right">댓글쓰기</button>
|
||||
</form>
|
||||
<div class="clearfix"></div>
|
||||
<hr />
|
||||
|
||||
|
||||
Reference in New Issue
Block a user