jspblog : delete post

This commit is contained in:
kim
2021-01-22 13:53:20 +09:00
parent 8012cb97a5
commit 3e31e38789
7 changed files with 89 additions and 0 deletions

View File

@@ -107,6 +107,7 @@ public class BoardDao {
.content(rs.getString("b.content"))
.readCount(rs.getInt("b.readCount"))
.username(rs.getString("u.username"))
.userId(rs.getInt("b.userId"))
.build();
return dto;
}
@@ -138,4 +139,24 @@ public class BoardDao {
}
return -1;
}
public int deleteById(int id) {
String sql = "delete from board where id=?";
Connection conn = DB.getConnection();
PreparedStatement pstmt = null;
if (conn != null) {
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
DB.close(conn, pstmt);
}
}
return -1;
}
}

View File

@@ -0,0 +1,8 @@
package com.example.jspblog.domain.board.dto;
import lombok.Data;
@Data
public class DeleteReqDto {
private int boardId;
}

View File

@@ -0,0 +1,8 @@
package com.example.jspblog.domain.board.dto;
import lombok.Data;
@Data
public class DeleteResDto {
private String status;
}

View File

@@ -11,6 +11,7 @@ public class DetailResDto {
private String content;
private int readCount;
private String username;
private int userId;
// public String getContent() {
// return content.replaceAll("&lt;", "<").replaceAll("&gt;", ">");

View File

@@ -31,4 +31,8 @@ public class BoardService {
int result = boardDao.updateReadCount(id);
return result == 1 ? boardDao.findById(id) : null;
}
public int 글삭제(int id) {
return boardDao.deleteById(id);
}
}

View File

@@ -1,12 +1,15 @@
package com.example.jspblog.web;
import com.example.jspblog.domain.board.Board;
import com.example.jspblog.domain.board.dto.DeleteReqDto;
import com.example.jspblog.domain.board.dto.DeleteResDto;
import com.example.jspblog.domain.board.dto.DetailResDto;
import com.example.jspblog.domain.board.dto.WriteReqDto;
import com.example.jspblog.domain.user.User;
import com.example.jspblog.service.BoardService;
import com.example.jspblog.service.UserService;
import com.example.jspblog.util.Script;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
@@ -14,7 +17,9 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/board")
@@ -79,7 +84,24 @@ public class BoardController extends HttpServlet {
request.setAttribute("detail", dto);
request.getRequestDispatcher("board/detail.jsp").forward(request, response);
}
}
case "delete" : {
BufferedReader br = request.getReader();
String data = br.readLine();
Gson gson = new Gson();
DeleteReqDto dto = gson.fromJson(data, DeleteReqDto.class);
int result = boardService.글삭제(dto.getBoardId());
DeleteResDto resDto = new DeleteResDto();
if (result == 1) {
resDto.setStatus("ok");
} else {
resDto.setStatus("fail");
}
String resData = gson.toJson(resDto);
PrintWriter out = response.getWriter();
out.print(resData);
out.flush();
}
}
}

View File

@@ -3,6 +3,10 @@
<%@ include file="../layout/header.jsp" %>
<div class="container">
<c:if test="${sessionScope.principal.id == detail.userId}">
<button onclick="deleteById(${detail.id})" class="btn btn-danger">삭제</button>
</c:if>
<br />
<br />
<h6 class="m-2">
@@ -67,5 +71,26 @@
$('#content').append(content[0].data)
})
</script>
<script>
function deleteById(boardId) {
const data = {
boardId : boardId
}
$.ajax({
type: "post",
url: "/jspblog/board?cmd=delete",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function(result){
if (result.status === 'ok') {
location.href = "index.jsp";
} else {
alert("삭제 실패");
}
});
}
</script>
</body>
</html>