feat : 게시판 댓글 작성자 체크

This commit is contained in:
yulmam 2024-08-07 11:08:26 +09:00
parent a4b3bab290
commit 208bd69d5b
7 changed files with 25 additions and 16 deletions

View File

@ -45,9 +45,13 @@ public class BoardController {
@GetMapping(value = "/{boardId}")
public ResponseEntity<ResponseBoardDetailDto> getBoardDetail(
@PathVariable int boardId
@PathVariable int boardId,
HttpServletRequest request
){
ResponseBoardDetailDto responseBoardDetailDto = boardService.findBoardDetail(boardId);
String token = request.getHeader("Authorization");
long userId = Long.parseLong(jwtUtil.getUserId(token));
ResponseBoardDetailDto responseBoardDetailDto = boardService.findBoardDetail(userId, boardId);
return new ResponseEntity<>(responseBoardDetailDto, HttpStatus.OK);
}
@ -77,8 +81,7 @@ public class BoardController {
@DeleteMapping(value = "/{boardId}")
public ResponseEntity<?> deleteBoard(
@PathVariable int boardId,
HttpServletRequest request
@PathVariable int boardId
){
boardService.deleteBoard(boardId);
@ -87,9 +90,13 @@ public class BoardController {
@GetMapping(value = "/comment/{boardId}")
public ResponseEntity<List<ResponseCommentDto>> getComments(
@PathVariable int boardId
@PathVariable int boardId,
HttpServletRequest request
){
List<ResponseCommentDto> comments = boardService.findComments(boardId);
String token = request.getHeader("Authorization");
long userId = Long.parseLong(jwtUtil.getUserId(token));
List<ResponseCommentDto> comments = boardService.findComments(userId, boardId);
return new ResponseEntity<>(comments, HttpStatus.OK);
}

View File

@ -16,7 +16,7 @@ public class ResponseBoardDetailDto {
private String name;
private String title;
private String content;
private int viewCount;
private boolean isMine;
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;
}

View File

@ -14,6 +14,7 @@ public class ResponseCommentDto {
private long id;
private String name;
private String content;
private boolean isMine;
private LocalDateTime createAt;
private LocalDateTime modifiedAt;
}

View File

@ -67,13 +67,13 @@ public class Board {
.build();
}
public ResponseBoardDetailDto makeDetailDto(){
public ResponseBoardDetailDto makeDetailDto(long userId){
return ResponseBoardDetailDto.builder()
.id(id)
.name(user.getName())
.title(title)
.content(content)
.viewCount(viewCount)
.isMine(user.getId() == userId)
.createdAt(createdAt)
.modifiedAt(modifiedAt)
.build();

View File

@ -39,11 +39,12 @@ public class Comment {
@JoinColumn(name = "board_id")
Board board;
public ResponseCommentDto makeCommentDto() {
public ResponseCommentDto makeCommentDto(long userId) {
return ResponseCommentDto.builder()
.id(id)
.name(user.getName())
.content(content)
.isMine(user.getId() == userId)
.createAt(createdAt)
.modifiedAt(modifiedAt)
.build();

View File

@ -10,7 +10,7 @@ public interface BoardService {
public List<ResponseBoardSummaryDto> findBoards(int pageNo, String category, long lectureId);
public ResponseBoardDetailDto findBoardDetail(long boardId);
public ResponseBoardDetailDto findBoardDetail(long userId, long boardId);
public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto);
@ -18,7 +18,7 @@ public interface BoardService {
public void createComment(long userId, long boardId, RequestCommentDto requestCommentDto);
public List<ResponseCommentDto> findComments(long boardId);
public List<ResponseCommentDto> findComments(long userId, long boardId);
public void updateComment(long commentId, RequestCommentDto requestCommentDto);

View File

@ -48,10 +48,10 @@ public class BoardServiceImpl implements BoardService {
}
@Transactional
public ResponseBoardDetailDto findBoardDetail(long boardId) {
public ResponseBoardDetailDto findBoardDetail(long userId, long boardId) {
return boardRepository.findById(boardId)
.orElseThrow(NoSuchElementException::new)
.makeDetailDto();
.makeDetailDto(userId);
}
@Transactional
@ -89,9 +89,9 @@ public class BoardServiceImpl implements BoardService {
}
@Transactional
public List<ResponseCommentDto> findComments(long boardId) {
public List<ResponseCommentDto> findComments(long userId, long boardId) {
return commentRepository.findByBoardId(boardId).stream()
.map(Comment::makeCommentDto)
.map((Comment c) -> c.makeCommentDto(userId))
.collect(Collectors.toList());
}