jpablog : write comment
This commit is contained in:
@@ -3,6 +3,7 @@ package com.example.jpablog.controller.api;
|
|||||||
import com.example.jpablog.config.auth.PrincipalDetail;
|
import com.example.jpablog.config.auth.PrincipalDetail;
|
||||||
import com.example.jpablog.dto.ResponseDto;
|
import com.example.jpablog.dto.ResponseDto;
|
||||||
import com.example.jpablog.model.Board;
|
import com.example.jpablog.model.Board;
|
||||||
|
import com.example.jpablog.model.Reply;
|
||||||
import com.example.jpablog.model.User;
|
import com.example.jpablog.model.User;
|
||||||
import com.example.jpablog.service.BoardService;
|
import com.example.jpablog.service.BoardService;
|
||||||
import com.example.jpablog.service.UserService;
|
import com.example.jpablog.service.UserService;
|
||||||
@@ -37,6 +38,16 @@ public class BoardApiController {
|
|||||||
return new ResponseDto<>(result, HttpStatus.OK.value());
|
return new ResponseDto<>(result, HttpStatus.OK.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/api/board/{boardId}/reply")
|
||||||
|
public ResponseDto<Integer> replySave(
|
||||||
|
@PathVariable Long boardId,
|
||||||
|
@RequestBody Reply reply,
|
||||||
|
@AuthenticationPrincipal PrincipalDetail principal) {
|
||||||
|
|
||||||
|
boardService.댓글쓰기(principal.getUser(), boardId, reply);
|
||||||
|
return new ResponseDto<>(1, HttpStatus.OK.value());
|
||||||
|
}
|
||||||
|
|
||||||
/*// 기본 로그인
|
/*// 기본 로그인
|
||||||
@PostMapping("/user/login")
|
@PostMapping("/user/login")
|
||||||
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
|
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ public class Board {
|
|||||||
|
|
||||||
@OneToMany(mappedBy = "board")
|
@OneToMany(mappedBy = "board")
|
||||||
@JsonIgnoreProperties({"board"})
|
@JsonIgnoreProperties({"board"})
|
||||||
|
@OrderBy("id desc")
|
||||||
private List<Reply> replies = new ArrayList<>();
|
private List<Reply> replies = new ArrayList<>();
|
||||||
|
|
||||||
@CreationTimestamp
|
@CreationTimestamp
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package com.example.jpablog.service;
|
|||||||
|
|
||||||
import com.example.jpablog.config.auth.PrincipalDetail;
|
import com.example.jpablog.config.auth.PrincipalDetail;
|
||||||
import com.example.jpablog.model.Board;
|
import com.example.jpablog.model.Board;
|
||||||
|
import com.example.jpablog.model.Reply;
|
||||||
import com.example.jpablog.model.User;
|
import com.example.jpablog.model.User;
|
||||||
import com.example.jpablog.repository.BoardRepository;
|
import com.example.jpablog.repository.BoardRepository;
|
||||||
|
import com.example.jpablog.repository.ReplyRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
@@ -20,6 +22,7 @@ import java.util.List;
|
|||||||
public class BoardService {
|
public class BoardService {
|
||||||
|
|
||||||
private final BoardRepository boardRepository;
|
private final BoardRepository boardRepository;
|
||||||
|
private final ReplyRepository replyRepository;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void 글쓰기(Board board, User user) {
|
public void 글쓰기(Board board, User user) {
|
||||||
@@ -60,4 +63,12 @@ public class BoardService {
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void 댓글쓰기(User user, Long boardId, Reply requestReply) {
|
||||||
|
Board board = boardRepository.findById(boardId).orElseThrow(() -> new IllegalArgumentException("댓글쓰기 실패"));
|
||||||
|
requestReply.setUser(user);
|
||||||
|
requestReply.setBoard(board);
|
||||||
|
replyRepository.save(requestReply);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ let index = {
|
|||||||
$("#btn-update").on("click", () => {
|
$("#btn-update").on("click", () => {
|
||||||
this.update();
|
this.update();
|
||||||
})
|
})
|
||||||
|
$("#btn-reply-save").on("click", () => {
|
||||||
|
this.replySave();
|
||||||
|
})
|
||||||
},
|
},
|
||||||
save : function () {
|
save : function () {
|
||||||
let data = {
|
let data = {
|
||||||
@@ -64,6 +67,24 @@ let index = {
|
|||||||
alert(JSON.stringify(error));
|
alert(JSON.stringify(error));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
replySave : function () {
|
||||||
|
let data = {
|
||||||
|
boardId: $("#boardId").val(),
|
||||||
|
content: $("#reply-content").val(),
|
||||||
|
};
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: `/api/board/${data.boardId}/reply`,
|
||||||
|
data: JSON.stringify(data),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
|
dataType: "json"
|
||||||
|
}).done(function (resp){
|
||||||
|
alert("댓글 작성 완료!");
|
||||||
|
location.href = `/board/${data.boardId}`;
|
||||||
|
}).fail(function (error){
|
||||||
|
alert(JSON.stringify(error));
|
||||||
|
});
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
index.init();
|
index.init();
|
||||||
@@ -24,8 +24,11 @@
|
|||||||
<hr/>
|
<hr/>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-body"><textarea class="form-control" rows="1"></textarea></div>
|
<input type="hidden" id="boardId" value="${board.id}">
|
||||||
<div class="card-footer"><button class="btn btn-primary">등록</button></div>
|
<div class="card-body">
|
||||||
|
<textarea id="reply-content" class="form-control" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer"><button id="btn-reply-save" class="btn btn-primary">등록</button></div>
|
||||||
</div>
|
</div>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="card">
|
<div class="card">
|
||||||
|
|||||||
Reference in New Issue
Block a user