jpablog : update post

This commit is contained in:
kim
2021-02-01 16:24:44 +09:00
parent 2b4fd9e3ec
commit 3f66ed663d
6 changed files with 93 additions and 10 deletions

View File

@@ -31,6 +31,12 @@ public class BoardController {
return "board/detail";
}
@GetMapping("/board/{id}/updateForm")
public String updateForm(@PathVariable Long id, Model model){
model.addAttribute("board", boardService.글상세보기(id));
return "board/updateForm";
}
@GetMapping("/board/saveForm")
public String save() {
return "board/saveForm";

View File

@@ -20,16 +20,21 @@ public class BoardApiController {
private final BoardService boardService;
@PostMapping("/api/board")
public ResponseDto<Integer> save(@RequestBody Board board, @AuthenticationPrincipal PrincipalDetail principal, Principal p) {
System.out.println(p.getName());
public ResponseDto<Integer> save(@RequestBody Board board, @AuthenticationPrincipal PrincipalDetail principal) {
boardService.글쓰기(board, principal.getUser());
return new ResponseDto<>(1, HttpStatus.OK.value());
}
@DeleteMapping("/api/board/{id}")
public ResponseDto<Integer> deleteById(@PathVariable Long id) {
boardService.글삭제하기(id);
return new ResponseDto<>(1, HttpStatus.OK.value());
public ResponseDto<Integer> deleteById(@PathVariable Long id, Principal principal) {
int result = boardService.글삭제하기(id, principal.getName());
return new ResponseDto<>(result, HttpStatus.OK.value());
}
@PatchMapping("/api/board/{id}")
public ResponseDto<Integer> update(@PathVariable Long id, @RequestBody Board board, Principal principal) {
int result = boardService.글수정하기(id,board, principal.getName());
return new ResponseDto<>(result, HttpStatus.OK.value());
}
/*// 기본 로그인

View File

@@ -38,7 +38,26 @@ public class BoardService {
}
@Transactional
public void 글삭제하기(Long id) {
boardRepository.deleteById(id);
public int 글삭제하기(Long id, String name) {
String writer = boardRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("아이디 없음"))
.getUser().getUsername();
if (writer.equals(name)) {
boardRepository.deleteById(id);
return 1;
}
return -1;
}
@Transactional
public int 글수정하기(Long id, Board requestBoard, String name) {
Board board = boardRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("아이디 없음"));
if (board.getUser().getUsername().equals(name)) {
board.setTitle(requestBoard.getTitle());
board.setContent(requestBoard.getContent());
return 1;
}
return -1;
}
}

View File

@@ -6,6 +6,9 @@ let index = {
$("#btn-delete").on("click", () => {
this.deleteById();
})
$("#btn-update").on("click", () => {
this.update();
})
},
save : function () {
let data = {
@@ -32,8 +35,31 @@ let index = {
url: "/api/board/" + id,
dataType: "json"
}).done(function (resp){
alert("삭제 완료!");
location.href = "/";
if (resp.data === 1) {
alert("삭제 완료!");
location.href = "/";
}
}).fail(function (error){
alert(JSON.stringify(error));
});
},
update : function () {
let id = $("#id").val();
let data = {
title: $("#title").val(),
content: $("#content").val(),
};
$.ajax({
type: "patch",
url: "/api/board/"+id,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function (resp){
if (resp.data === 1) {
alert("글수정 완료!");
history.back();
}
}).fail(function (error){
alert(JSON.stringify(error));
});

View File

@@ -5,7 +5,7 @@
<div class="container">
<button class="btn btn-secondary" onclick="history.back()">돌아가기</button>
<c:if test="${board.user.id == principal.user.id}" >
<button id="btn-update" class="btn btn-warning">수정</button>
<a href="/board/${board.id}/updateForm" class="btn btn-warning">수정</a>
<button id="btn-delete" class="btn btn-danger">삭제</button>
</c:if>
<br/><br/>

View File

@@ -0,0 +1,27 @@
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="../layout/header.jsp"%>
<div class="container">
<form>
<input type="hidden" id="id" value="${board.id}">
<div class="form-group">
<label for="title">Title:</label>
<input value="${board.title}" type="text" class="form-control" placeholder="Enter title" id="title">
</div>
<div class="form-group">
<label for="content">Content:</label>
<textarea class="form-control summernote" rows="5" id="content">${board.content}</textarea>
</div>
<button id="btn-update" type="button" class="btn btn-primary">글수정</button>
</form>
</div>
<script>
$('.summernote').summernote({
placeholder: '글쓰기 ㅎㅎ',
tabsize: 2,
height: 300
});
</script>
<script src="/js/board.js"></script>
<%@include file="../layout/footer.jsp"%>