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}") @GetMapping(value = "/{boardId}")
public ResponseEntity<ResponseBoardDetailDto> getBoardDetail( 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); return new ResponseEntity<>(responseBoardDetailDto, HttpStatus.OK);
} }
@ -77,8 +81,7 @@ public class BoardController {
@DeleteMapping(value = "/{boardId}") @DeleteMapping(value = "/{boardId}")
public ResponseEntity<?> deleteBoard( public ResponseEntity<?> deleteBoard(
@PathVariable int boardId, @PathVariable int boardId
HttpServletRequest request
){ ){
boardService.deleteBoard(boardId); boardService.deleteBoard(boardId);
@ -87,9 +90,13 @@ public class BoardController {
@GetMapping(value = "/comment/{boardId}") @GetMapping(value = "/comment/{boardId}")
public ResponseEntity<List<ResponseCommentDto>> getComments( 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); return new ResponseEntity<>(comments, HttpStatus.OK);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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