From a0dc8850afa42fd18335a298e4551dcbb562893f Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 01:49:47 +0900 Subject: [PATCH 01/16] =?UTF-8?q?feat:=20#137=20=EB=8B=B5=EB=B3=80=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20API=20Response=20DTO=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=20&=20=ED=95=B4=EB=8B=B9=20=EB=8B=B5=EB=B3=80=EC=97=90=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EB=88=8C=EB=A0=80=EB=8A=94?= =?UTF-8?q?=EC=A7=80=20=EC=97=AC=EB=B6=80=20=EC=A1=B0=EC=82=AC=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/capple/domain/answer/dto/AnswerResponse.java | 4 ++++ .../server/capple/domain/answer/mapper/AnswerMapper.java | 8 ++++++-- .../answer/repository/AnswerHeartRedisRepository.java | 6 ++++++ .../capple/domain/answer/service/AnswerServiceImpl.java | 4 ++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java b/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java index 9c4594e4..2f397a34 100644 --- a/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java +++ b/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java @@ -16,11 +16,14 @@ public static class AnswerId { @Builder public static class AnswerInfo { private Long answerId; + private Long writerId; private String profileImage; private String nickname; private String content; private Boolean isMyAnswer; private Boolean isReported; + private Boolean isLiked; + private String writeAt; } @Getter @@ -44,6 +47,7 @@ public static class AnswerLike { public static class MemberAnswerInfo { private Long questionId; private Long answerId; + private Long writerId; private String nickname; private String profileImage; private String content; diff --git a/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java b/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java index 295adb23..8cec8c5b 100644 --- a/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java +++ b/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java @@ -29,14 +29,17 @@ public AnswerList toAnswerList(List answerInfoList) { .build(); } - public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported) { + public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported, Boolean isLiked) { return AnswerInfo.builder() .answerId(answer.getId()) + .writerId(answer.getMember().getId()) .profileImage(answer.getMember().getProfileImage()) .nickname(answer.getMember().getNickname()) .content(answer.getContent()) .isMyAnswer(answer.getMember().getId() == memberId) .isReported(isReported) + .isLiked(isLiked) + .writeAt(answer.getCreatedAt().toString()) .build(); } @@ -44,11 +47,12 @@ public MemberAnswerInfo toMemberAnswerInfo(Answer answer, int heartCount) { return MemberAnswerInfo.builder() .questionId(answer.getQuestion().getId()) .answerId(answer.getId()) + .writerId(answer.getMember().getId()) .nickname(answer.getMember().getNickname()) .profileImage(answer.getMember().getProfileImage()) .content(answer.getContent()) .heartCount(heartCount) - .writeAt(answer.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy.MM.dd"))) + .writeAt(answer.getCreatedAt().toString()) .build(); } diff --git a/src/main/java/com/server/capple/domain/answer/repository/AnswerHeartRedisRepository.java b/src/main/java/com/server/capple/domain/answer/repository/AnswerHeartRedisRepository.java index 81bc8814..1b5bab35 100644 --- a/src/main/java/com/server/capple/domain/answer/repository/AnswerHeartRedisRepository.java +++ b/src/main/java/com/server/capple/domain/answer/repository/AnswerHeartRedisRepository.java @@ -54,4 +54,10 @@ public Set getMemberHeartsAnswer(Long memberId) { } return answerIds; } + + public boolean isMemberLikedAnswer(Long memberId, Long answerId) { + String key = ANSWER_HEART_KEY_PREFIX + answerId; + String memberKey = MEMBER_KEY_PREFIX + memberId; + return redisTemplate.opsForSet().isMember(key, memberKey); + } } diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index 027bbb24..2511ac62 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -90,14 +90,14 @@ public AnswerList getAnswerList(Long memberId, Long questionId, String keyword, answerRepository.findByQuestion(questionId, pageable).orElseThrow(() -> new RestApiException(AnswerErrorCode.ANSWER_NOT_FOUND)) .stream() - .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer))) + .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()))) .toList()); } else { return answerMapper.toAnswerList( answerRepository.findByQuestionAndKeyword(questionId, keyword, pageable).orElseThrow(() -> new RestApiException(AnswerErrorCode.ANSWER_NOT_FOUND)) .stream() - .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer))) + .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()))) .toList()); } From cca161c51db8d7a2eed748b3c4fab5bbb7997c44 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 01:51:44 +0900 Subject: [PATCH 02/16] =?UTF-8?q?feat:=20#137=20=ED=95=B4=EB=8B=B9=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=EC=97=90=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94=20=EB=88=8C=EB=A0=80=EB=8A=94=EC=A7=80=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=20=EC=A1=B0=EC=82=AC=20=ED=95=A8=EC=88=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/board/repository/BoardHeartRedisRepository.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java b/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java index 7f1432fa..445af7a2 100644 --- a/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java +++ b/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java @@ -71,4 +71,10 @@ public Set getMemberHeartsBoard(Long memberId) { } return boardIds; } + + public boolean isMemberLikedBoard(Long memberId, Long boardId) { + String key = BOARD_HEART_KEY_PREFIX + boardId; + String memberKey = MEMBER_KEY_PREFIX + memberId; + return redisTemplate.opsForSet().isMember(key, memberKey); + } } \ No newline at end of file From 8ec46a511a710a6b0aa9bba0c5ee608b3cc32f72 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 02:22:20 +0900 Subject: [PATCH 03/16] =?UTF-8?q?feat:=20#137=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20=EB=8C=93=EA=B8=80=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20API=20Response=20DTO=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../capple/domain/board/controller/BoardController.java | 3 ++- .../server/capple/domain/board/dto/BoardResponse.java | 1 + .../com/server/capple/domain/board/entity/Board.java | 9 +++++++++ .../server/capple/domain/board/mapper/BoardMapper.java | 7 ++++--- .../server/capple/domain/board/service/BoardService.java | 2 +- .../capple/domain/board/service/BoardServiceImpl.java | 5 +++-- .../domain/boardComment/dto/BoardCommentResponse.java | 2 +- .../domain/boardComment/mapper/BoardCommentMapper.java | 2 +- .../boardComment/service/BoardCommentServiceImpl.java | 2 ++ 9 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/server/capple/domain/board/controller/BoardController.java b/src/main/java/com/server/capple/domain/board/controller/BoardController.java index 82035305..d5ede657 100644 --- a/src/main/java/com/server/capple/domain/board/controller/BoardController.java +++ b/src/main/java/com/server/capple/domain/board/controller/BoardController.java @@ -46,11 +46,12 @@ private BaseResponse createBoard( }) @GetMapping() private BaseResponse getBoardsByBoardType( + @AuthMember Member member, @RequestParam(name = "boardType", required = false) BoardType boardType // TODO: 페이징 프론트 이슈로 추후 구현 // @PageableDefault(sort = "created_at", direction = Sort.Direction.DESC) @Parameter(hidden = true) Pageable pageable ) { - return BaseResponse.onSuccess(boardService.getBoardsByBoardType(boardType)); + return BaseResponse.onSuccess(boardService.getBoardsByBoardType(member, boardType)); } @Operation(summary = "게시글 삭제 API", description = "게시글을 삭제합니다.") diff --git a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java index 5627f505..54991aec 100644 --- a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java +++ b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java @@ -39,6 +39,7 @@ public static class BoardsGetByBoardTypeBoardInfo { private Integer heartCount; private Integer commentCount; private LocalDateTime createAt; + private Boolean isLiked; } @Getter diff --git a/src/main/java/com/server/capple/domain/board/entity/Board.java b/src/main/java/com/server/capple/domain/board/entity/Board.java index af811828..a6ea3e33 100644 --- a/src/main/java/com/server/capple/domain/board/entity/Board.java +++ b/src/main/java/com/server/capple/domain/board/entity/Board.java @@ -1,5 +1,6 @@ package com.server.capple.domain.board.entity; +import com.server.capple.domain.answer.dto.AnswerRequest; import com.server.capple.domain.member.entity.Member; import com.server.capple.global.common.BaseEntity; import jakarta.persistence.*; @@ -31,4 +32,12 @@ public class Board extends BaseEntity { @Column(nullable = false) private Integer commentCount; + + public void increaseCommentCount() { + this.commentCount += 1; + } + + public void decreaseCommentCount() { + this.commentCount -= 1; + } } diff --git a/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java b/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java index 337ffe5a..cd1a2134 100644 --- a/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java +++ b/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java @@ -44,15 +44,16 @@ public BoardResponse.BoardsGetByBoardType toBoardsGetByBoardType( public BoardResponse.BoardsGetByBoardTypeBoardInfo toBoardsGetByBoardTypeBoardInfo( Board board, - Integer boardHeartsCount) { + Integer boardHeartsCount, + Boolean isLiked) { return BoardResponse.BoardsGetByBoardTypeBoardInfo.builder() .boardId(board.getId()) .writerId(board.getWriter().getId()) .content(board.getContent()) .heartCount(boardHeartsCount) - // TODO : 댓글 작성 API 나오면 추후 구현 - .commentCount(0) + .commentCount(board.getCommentCount()) .createAt(board.getCreatedAt()) + .isLiked(isLiked) .build(); } diff --git a/src/main/java/com/server/capple/domain/board/service/BoardService.java b/src/main/java/com/server/capple/domain/board/service/BoardService.java index 92c5a005..b8c6ee45 100644 --- a/src/main/java/com/server/capple/domain/board/service/BoardService.java +++ b/src/main/java/com/server/capple/domain/board/service/BoardService.java @@ -10,7 +10,7 @@ public interface BoardService { BoardCreate createBoard(Member member, BoardType boardType, String content); - BoardsGetByBoardType getBoardsByBoardType(BoardType boardType); + BoardsGetByBoardType getBoardsByBoardType(Member member, BoardType boardType); BoardDelete deleteBoard(Member member, Long boardId); diff --git a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java index ad3bd6c4..32ded00e 100644 --- a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java +++ b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java @@ -6,6 +6,7 @@ import com.server.capple.domain.board.mapper.BoardMapper; import com.server.capple.domain.board.repository.BoardHeartRedisRepository; import com.server.capple.domain.board.repository.BoardRepository; +import com.server.capple.domain.boardComment.repository.BoardCommentHeartRedisRepository; import com.server.capple.domain.member.entity.Member; import com.server.capple.global.exception.RestApiException; import com.server.capple.global.exception.errorCode.BoardErrorCode; @@ -37,7 +38,7 @@ public BoardResponse.BoardCreate createBoard(Member member, BoardType boardType, } @Override - public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardType) { + public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(Member member, BoardType boardType) { List boards = new ArrayList<>(); if (boardType == null) { boards = boardRepository.findAll(); @@ -49,7 +50,7 @@ public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardTy throw new RestApiException(BoardErrorCode.BOARD_BAD_REQUEST); } return boardMapper.toBoardsGetByBoardType(boards.stream() - .map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId()))) + .map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId()), boardHeartRedisRepository.isMemberLikedBoard(member.getId(), board.getId()))) .toList() ); } diff --git a/src/main/java/com/server/capple/domain/boardComment/dto/BoardCommentResponse.java b/src/main/java/com/server/capple/domain/boardComment/dto/BoardCommentResponse.java index b87ed7cd..c006c4e6 100644 --- a/src/main/java/com/server/capple/domain/boardComment/dto/BoardCommentResponse.java +++ b/src/main/java/com/server/capple/domain/boardComment/dto/BoardCommentResponse.java @@ -26,7 +26,7 @@ public static class BoardCommentHeart { @Builder public static class BoardCommentInfo { private Long boardCommentId; - private String writer; + private Long writerId; private String content; private Long heartCount; private Boolean isLiked; diff --git a/src/main/java/com/server/capple/domain/boardComment/mapper/BoardCommentMapper.java b/src/main/java/com/server/capple/domain/boardComment/mapper/BoardCommentMapper.java index dbc38ff7..7e693444 100644 --- a/src/main/java/com/server/capple/domain/boardComment/mapper/BoardCommentMapper.java +++ b/src/main/java/com/server/capple/domain/boardComment/mapper/BoardCommentMapper.java @@ -19,7 +19,7 @@ public BoardComment toBoardCommentEntity(Member member, Board board, String comm public BoardCommentInfo toBoardCommentInfo(BoardComment comment, Long heartCount, Boolean isLiked) { return BoardCommentInfo.builder() .boardCommentId(comment.getId()) - .writer(comment.getMember().getNickname()) + .writerId(comment.getMember().getId()) .content(comment.getContent()) .heartCount(heartCount) .isLiked(isLiked) diff --git a/src/main/java/com/server/capple/domain/boardComment/service/BoardCommentServiceImpl.java b/src/main/java/com/server/capple/domain/boardComment/service/BoardCommentServiceImpl.java index 1890632b..d4564ede 100644 --- a/src/main/java/com/server/capple/domain/boardComment/service/BoardCommentServiceImpl.java +++ b/src/main/java/com/server/capple/domain/boardComment/service/BoardCommentServiceImpl.java @@ -39,6 +39,7 @@ public BoardCommentId createBoardComment(Member member, Long boardId, BoardComme BoardComment boardComment = boardCommentRepository.save( boardCommentMapper.toBoardCommentEntity(loginMember, board, request.getComment())); + board.increaseCommentCount(); return new BoardCommentId(boardComment.getId()); } @@ -59,6 +60,7 @@ public BoardCommentId deleteBoardComment(Member member, Long commentId) { BoardComment boardComment = findBoardComment(commentId); checkPermission(member, boardComment); + boardComment.getBoard().decreaseCommentCount();; boardComment.delete(); return new BoardCommentId(boardComment.getId()); From 236272eeca9716e7056bee1683a9c2cdfadfdda7 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 02:37:52 +0900 Subject: [PATCH 04/16] =?UTF-8?q?fix:=20#137=20=EA=B2=8C=EC=8B=9C=EA=B8=80?= =?UTF-8?q?=20=EC=A2=8B=EC=95=84=EC=9A=94/=EC=B7=A8=EC=86=8C=20API=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/board/repository/BoardHeartRedisRepository.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java b/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java index 445af7a2..1477a93d 100644 --- a/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java +++ b/src/main/java/com/server/capple/domain/board/repository/BoardHeartRedisRepository.java @@ -23,10 +23,10 @@ public class BoardHeartRedisRepository implements Serializable { private final RedisTemplate redisTemplate; // 게시판 좋아요 토글 - public Boolean toggleBoardHeart(Long boardId, Long memberId) { + public Boolean toggleBoardHeart(Long memberId, Long boardId) { String key = BOARD_HEART_KEY_PREFIX + boardId.toString(); String member = MEMBER_KEY_PREFIX + memberId.toString(); - String createAtKey = key + ":" + member + ":createAt"; // member ID를 포함한 createAtKeyㄱ + String createAtKey = key + ":" + member + ":createAt"; // member ID를 포함한 createAtKey SetOperations setOperations = redisTemplate.opsForSet(); ValueOperations valueOperations = redisTemplate.opsForValue(); @@ -44,7 +44,7 @@ public Boolean toggleBoardHeart(Long boardId, Long memberId) { } } - // + public String getBoardHeartCreateAt(Long boardId, Long memberId) { String createAtKey = BOARD_HEART_KEY_PREFIX + boardId.toString() + ":" + MEMBER_KEY_PREFIX + memberId.toString() + ":createAt"; return redisTemplate.opsForValue().get(createAtKey); From 1e027e80c66e6a0253341e2da56b825aae10ebfa Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 03:14:31 +0900 Subject: [PATCH 05/16] =?UTF-8?q?fix:=20#137=20=EC=A7=88=EB=AC=B8=20commen?= =?UTF-8?q?tCount=20=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../answer/service/AnswerServiceImpl.java | 2 + .../board/service/BoardServiceImpl.java | 2 +- .../dto/response/QuestionResponse.java | 7 +- .../domain/question/entity/Question.java | 11 +++ .../question/mapper/QuestionMapper.java | 12 +-- .../QuestionHeartRedisRepository.java | 80 +++++++++++++++++++ .../repository/QuestionRepository.java | 2 +- .../question/service/QuestionServiceImpl.java | 9 ++- 8 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index 2511ac62..49af7d69 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -42,6 +42,7 @@ public AnswerResponse.AnswerId createAnswer(Member loginMember, Long questionId, //답변 저장 Answer answer = answerRepository.save(answerMapper.toAnswerEntity(request, member, question)); + answer.getQuestion().increaseCommentCount(); return new AnswerResponse.AnswerId(answer.getId()); } @@ -66,6 +67,7 @@ public AnswerResponse.AnswerId deleteAnswer(Member loginMember, Long answerId) { Answer answer = findAnswer(answerId); checkPermission(loginMember, answer); + answer.getQuestion().decreaseCommentCount(); answer.delete(); diff --git a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java index 32ded00e..b7876968 100644 --- a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java +++ b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java @@ -77,7 +77,7 @@ public BoardResponse.BoardsSearchByKeyword searchBoardsByKeyword(String keyword) @Override public BoardResponse.BoardToggleHeart toggleBoardHeart(Member member, Long boardId) { Board board = findBoard(boardId); - System.out.println(boardHeartRedisRepository.getBoardHeartCreateAt(board.getId(), member.getId())); +// System.out.println(boardHeartRedisRepository.getBoardHeartCreateAt(board.getId(), member.getId())); Boolean isLiked = boardHeartRedisRepository.toggleBoardHeart(member.getId(), board.getId()); return new BoardResponse.BoardToggleHeart(boardId, isLiked); diff --git a/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java b/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java index 157ac337..d8051110 100644 --- a/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java +++ b/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java @@ -18,6 +18,8 @@ public static class QuestionSummary { private Long questionId; private QuestionStatus questionStatus; private String content; + private Integer likeCount; + private Integer commentCount; private Boolean isAnswered; } @@ -31,9 +33,8 @@ public static class QuestionInfo { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") private LocalDateTime livedAt; private String content; - // 추후 추가 예정 -// private Long likeCount; -// private Long commentCount; + private Integer likeCount; + private Integer commentCount; private Boolean isAnswered; } diff --git a/src/main/java/com/server/capple/domain/question/entity/Question.java b/src/main/java/com/server/capple/domain/question/entity/Question.java index f1d77239..4e39a1cb 100644 --- a/src/main/java/com/server/capple/domain/question/entity/Question.java +++ b/src/main/java/com/server/capple/domain/question/entity/Question.java @@ -36,6 +36,9 @@ public class Question extends BaseEntity { private LocalDateTime livedAt; + @Column(nullable = false) + private Integer commentCount; + //question Status를 바꾸는 함수 public void setQuestionStatus(QuestionStatus questionStatus) { this.questionStatus = questionStatus; @@ -44,4 +47,12 @@ public void setQuestionStatus(QuestionStatus questionStatus) { this.livedAt = LocalDateTime.now(); } + public void increaseCommentCount() { + this.commentCount += 1; + } + + public void decreaseCommentCount() { + this.commentCount -= 1; + } + } diff --git a/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java b/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java index e404bd07..3825fee9 100644 --- a/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java +++ b/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java @@ -16,28 +16,30 @@ public Question toQuestion(QuestionCreate request) { return Question.builder() .questionStatus(request.getQuestionStatus()) .content(request.getContent()) + .commentCount(0) .build(); } - public QuestionSummary toQuestionSummary(Question question, boolean isAnswered) { + public QuestionSummary toQuestionSummary(Question question, boolean isAnswered, Integer likeCount) { return QuestionSummary.builder() .questionId(question.getId()) .questionStatus(question.getQuestionStatus()) .content(question.getContent()) .isAnswered(isAnswered) + .likeCount(likeCount) + .commentCount(question.getCommentCount()) .build(); } - public QuestionInfo toQuestionInfo(Question question, boolean isAnswered) { + public QuestionInfo toQuestionInfo(Question question, boolean isAnswered, Integer likeCount) { return QuestionInfo.builder() .questionId(question.getId()) .questionStatus(question.getQuestionStatus()) .livedAt(question.getLivedAt()) .content(question.getContent()) - // Count는 추후 수정 예정(필드 수정해야함...) -// .likeCount(100L) -// .commentCount(0L) + .likeCount(likeCount) + .commentCount(question.getCommentCount()) .isAnswered(isAnswered) .build(); } diff --git a/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java b/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java new file mode 100644 index 00000000..db14a4ff --- /dev/null +++ b/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java @@ -0,0 +1,80 @@ +package com.server.capple.domain.question.repository; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.SetOperations; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Repository; + +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.HashSet; +import java.util.Set; + +import static java.lang.Boolean.FALSE; +import static java.lang.Boolean.TRUE; + +@Repository +@RequiredArgsConstructor +public class QuestionHeartRedisRepository implements Serializable { + public static final String QUESTION_HEART_KEY_PREFIX = "questionHeart-"; + public static final String MEMBER_KEY_PREFIX = "member-"; + + private final RedisTemplate redisTemplate; + + // 질문 좋아요 토글 + public Boolean toggleBoardHeart(Long memberId, Long boardId) { + String key = QUESTION_HEART_KEY_PREFIX + boardId.toString(); + String member = MEMBER_KEY_PREFIX + memberId.toString(); + String createAtKey = key + ":" + member + ":createAt"; // member ID를 포함한 createAtKey + SetOperations setOperations = redisTemplate.opsForSet(); + ValueOperations valueOperations = redisTemplate.opsForValue(); + + //해당 key에 member가 존재하지 않으면 추가, 존재하면 삭제 + if (FALSE.equals(setOperations.isMember(key, member))) { + setOperations.add(key, member); + // 현재 시간을 createAtKey로 저장 + valueOperations.set(createAtKey, LocalDateTime.now().toString()); + return TRUE; + } else { + setOperations.remove(key, member); + // 좋아요 취소 시 생성 시간도 삭제할 수 있음 + redisTemplate.delete(createAtKey); + return FALSE; + } + } + + + public String getQuestionHeartCreateAt(Long questionId, Long memberId) { + String createAtKey = QUESTION_HEART_KEY_PREFIX + questionId.toString() + ":" + MEMBER_KEY_PREFIX + memberId.toString() + ":createAt"; + return redisTemplate.opsForValue().get(createAtKey); + } + + // 질문 좋아요 수 조회 + public Integer getQuestionHeartsCount(Long questionId) { + String key = QUESTION_HEART_KEY_PREFIX + questionId.toString(); + Set members = redisTemplate.opsForSet().members(key); + return members != null ? members.size() : 0; + } + + // 좋아요 누른 질문 조회 + public Set getMemberHeartsQuestion(Long memberId) { + String member = MEMBER_KEY_PREFIX + memberId.toString(); + Set keys = redisTemplate.keys(QUESTION_HEART_KEY_PREFIX + "*"); // 모든 키 조회 + Set questionIds = new HashSet<>(); + + for (String key : keys) { + if (redisTemplate.opsForSet().isMember(key, member)) { + String questionId = key.substring(QUESTION_HEART_KEY_PREFIX.length()); + questionIds.add(Long.parseLong(questionId)); + } + } + return questionIds; + } + + public boolean isMemberLikedQuestion(Long memberId, Long questionId) { + String key = QUESTION_HEART_KEY_PREFIX + questionId; + String memberKey = MEMBER_KEY_PREFIX + memberId; + return redisTemplate.opsForSet().isMember(key, memberKey); + } +} \ No newline at end of file diff --git a/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java b/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java index ffe97da0..b9685036 100644 --- a/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java +++ b/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java @@ -26,7 +26,7 @@ public interface QuestionRepository extends JpaRepository { Optional findByQuestionStatusIsLiveAndOldOrderByLivedAt(); - @Query("SELECT q AS question, (a IS NOT NULL) AS isAnsweredByMember " + + @Query("SELECT DISTINCT q AS question, (a IS NOT NULL) AS isAnsweredByMember " + "FROM Question q LEFT JOIN Answer a ON q = a.question AND a.deletedAt is NULL AND a.member = :member " + "WHERE q.questionStatus = 'OLD' OR q.questionStatus = 'LIVE' " + "ORDER BY q.livedAt DESC") diff --git a/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java b/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java index 03fc1a83..2529b8ab 100644 --- a/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java +++ b/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java @@ -1,12 +1,14 @@ package com.server.capple.domain.question.service; import com.server.capple.domain.answer.repository.AnswerRepository; +import com.server.capple.domain.answerComment.repository.AnswerCommentHeartRedisRepository; import com.server.capple.domain.member.entity.Member; import com.server.capple.domain.question.dao.QuestionInfoInterface; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionInfos; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionSummary; import com.server.capple.domain.question.entity.Question; import com.server.capple.domain.question.mapper.QuestionMapper; +import com.server.capple.domain.question.repository.QuestionHeartRedisRepository; import com.server.capple.domain.question.repository.QuestionRepository; import com.server.capple.global.exception.RestApiException; import com.server.capple.global.exception.errorCode.QuestionErrorCode; @@ -23,6 +25,8 @@ public class QuestionServiceImpl implements QuestionService { private final QuestionRepository questionRepository; private final AnswerRepository answerRepository; private final QuestionMapper questionMapper; + private final QuestionHeartRedisRepository questionHeartRepository; + private final AnswerCommentHeartRedisRepository answerCommentHeartRepository; @Override public Question findQuestion(Long questionId) { @@ -37,7 +41,7 @@ public QuestionSummary getMainQuestion(Member member) { boolean isAnswered = answerRepository.existsByQuestionAndMember(mainQuestion, member); - return questionMapper.toQuestionSummary(mainQuestion, isAnswered); + return questionMapper.toQuestionSummary(mainQuestion, isAnswered, questionHeartRepository.getQuestionHeartsCount(mainQuestion.getId())); } @Override @@ -47,7 +51,8 @@ public QuestionInfos getQuestions(Member member) { return questionMapper.toQuestionInfos(questions.stream() .map(questionInfo -> questionMapper.toQuestionInfo(questionInfo.getQuestion(), - questionInfo.getIsAnsweredByMember()) + questionInfo.getIsAnsweredByMember(), + questionHeartRepository.getQuestionHeartsCount(questionInfo.getQuestion().getId())) ).toList()); } } From fe0e850b08e55cd1a30dbf458b6703f61326ac08 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 03:31:15 +0900 Subject: [PATCH 06/16] =?UTF-8?q?feat:=20#137=20=EC=A7=88=EB=AC=B8=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94/=EC=B7=A8=EC=86=8C=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=EC=A7=88=EB=AC=B8=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API=EC=97=90=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../question/controller/QuestionController.java | 13 ++++++++++--- .../question/dto/response/QuestionResponse.java | 10 ++++++++++ .../domain/question/service/QuestionService.java | 4 ++++ .../question/service/QuestionServiceImpl.java | 11 +++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/server/capple/domain/question/controller/QuestionController.java b/src/main/java/com/server/capple/domain/question/controller/QuestionController.java index 2720784e..95c59553 100644 --- a/src/main/java/com/server/capple/domain/question/controller/QuestionController.java +++ b/src/main/java/com/server/capple/domain/question/controller/QuestionController.java @@ -1,7 +1,9 @@ package com.server.capple.domain.question.controller; import com.server.capple.config.security.AuthMember; +import com.server.capple.domain.board.dto.BoardResponse; import com.server.capple.domain.member.entity.Member; +import com.server.capple.domain.question.dto.response.QuestionResponse; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionSummary; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionInfos; import com.server.capple.domain.question.service.QuestionService; @@ -11,9 +13,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Tag(name = "질문 API", description = "질문 관련 API") @RestController @@ -41,6 +41,13 @@ private BaseResponse getQuestions(@AuthMember Member member) { return BaseResponse.onSuccess(questionService.getQuestions(member)); } + @Operation(summary = "질문 좋아요/취소 API", description = " 질문 좋아요/취소 API 입니다." + + "pathvariable 으로 questionId를 주세요.") + @PostMapping("/{questionId}/heart") + public BaseResponse toggleBoardHeart(@AuthMember Member member, @PathVariable(value = "questionId") Long questionId) { + return BaseResponse.onSuccess(questionService.toggleQuestionHeart(member, questionId)); + } + // @Operation(summary = "최근 지난 질문 조회 API", description = "최근 지난 질문을 조회합니다.") // @ApiResponses(value = { diff --git a/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java b/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java index d8051110..4de8f6ca 100644 --- a/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java +++ b/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import java.time.LocalDateTime; import java.util.List; @@ -51,4 +52,13 @@ public static class QuestionId { public static class QuestionInfos { private List questionInfos; } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class QuestionToggleHeart { + private Long questionId; + private Boolean isLiked; + } } diff --git a/src/main/java/com/server/capple/domain/question/service/QuestionService.java b/src/main/java/com/server/capple/domain/question/service/QuestionService.java index 5b4e8415..1fefb3ec 100644 --- a/src/main/java/com/server/capple/domain/question/service/QuestionService.java +++ b/src/main/java/com/server/capple/domain/question/service/QuestionService.java @@ -1,6 +1,8 @@ package com.server.capple.domain.question.service; import com.server.capple.config.security.AuthMember; +import com.server.capple.domain.board.dto.BoardResponse; import com.server.capple.domain.member.entity.Member; +import com.server.capple.domain.question.dto.response.QuestionResponse; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionSummary; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionInfos; import com.server.capple.domain.question.entity.Question; @@ -10,4 +12,6 @@ public interface QuestionService { QuestionSummary getMainQuestion(Member member); QuestionInfos getQuestions(Member member); + + QuestionResponse.QuestionToggleHeart toggleQuestionHeart(Member member, Long questionId); } diff --git a/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java b/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java index 2529b8ab..33d2f2ca 100644 --- a/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java +++ b/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java @@ -2,8 +2,11 @@ import com.server.capple.domain.answer.repository.AnswerRepository; import com.server.capple.domain.answerComment.repository.AnswerCommentHeartRedisRepository; +import com.server.capple.domain.board.dto.BoardResponse; +import com.server.capple.domain.board.entity.Board; import com.server.capple.domain.member.entity.Member; import com.server.capple.domain.question.dao.QuestionInfoInterface; +import com.server.capple.domain.question.dto.response.QuestionResponse; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionInfos; import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionSummary; import com.server.capple.domain.question.entity.Question; @@ -55,4 +58,12 @@ public QuestionInfos getQuestions(Member member) { questionHeartRepository.getQuestionHeartsCount(questionInfo.getQuestion().getId())) ).toList()); } + + @Override + public QuestionResponse.QuestionToggleHeart toggleQuestionHeart(Member member, Long questionId) { + Question question = findQuestion(questionId); + + Boolean isLiked = questionHeartRepository.toggleBoardHeart(member.getId(), question.getId()); + return new QuestionResponse.QuestionToggleHeart(questionId, isLiked); + } } From f5fe9ba90ae5a70ca0143d3afe3a54c92de26ce9 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 04:26:41 +0900 Subject: [PATCH 07/16] =?UTF-8?q?fix:=20#137=20AnswerCommentInfo=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/answerComment/dto/AnswerCommentResponse.java | 2 +- .../domain/answerComment/mapper/AnswerCommentMapper.java | 2 +- .../domain/answer/controller/AnswerControllerTest.java | 2 +- .../controller/AnswerCommentControllerTest.java | 2 +- .../answerComment/service/AnswerCommentServiceTest.java | 2 +- .../boardComment/controller/BoardCommentControllerTest.java | 2 +- .../boardComment/service/BoardCommentServiceTest.java | 2 +- .../com/server/capple/support/ControllerTestConfig.java | 6 +++--- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/server/capple/domain/answerComment/dto/AnswerCommentResponse.java b/src/main/java/com/server/capple/domain/answerComment/dto/AnswerCommentResponse.java index 6f77d4a1..f877645b 100644 --- a/src/main/java/com/server/capple/domain/answerComment/dto/AnswerCommentResponse.java +++ b/src/main/java/com/server/capple/domain/answerComment/dto/AnswerCommentResponse.java @@ -25,7 +25,7 @@ public static class AnswerCommentHeart { @Builder public static class AnswerCommentInfo { private Long answerCommentId; - private String writer; + private Long writerId; private String content; private Long heartCount; private LocalDateTime createdAt; diff --git a/src/main/java/com/server/capple/domain/answerComment/mapper/AnswerCommentMapper.java b/src/main/java/com/server/capple/domain/answerComment/mapper/AnswerCommentMapper.java index d8b8816f..698cf40a 100644 --- a/src/main/java/com/server/capple/domain/answerComment/mapper/AnswerCommentMapper.java +++ b/src/main/java/com/server/capple/domain/answerComment/mapper/AnswerCommentMapper.java @@ -20,7 +20,7 @@ public AnswerComment toAnswerCommentEntity(Member member, Answer answer, String public AnswerCommentInfo toAnswerCommentInfo(AnswerComment comment, Long heartCount) { return AnswerCommentInfo.builder() .answerCommentId(comment.getId()) - .writer(comment.getMember().getNickname()) + .writerId(comment.getMember().getId()) .content(comment.getContent()) .heartCount(heartCount) .createdAt(comment.getCreatedAt()) diff --git a/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java b/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java index 7bb8c0b1..347f2f74 100644 --- a/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java +++ b/src/test/java/com/server/capple/domain/answer/controller/AnswerControllerTest.java @@ -149,7 +149,7 @@ public void getMyPageMemberAnswerTest() throws Exception { .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value("COMMON200")) .andExpect(jsonPath("$.message").value("요청에 성공하였습니다.")) - .andExpect(jsonPath("$.result.memberAnswerInfos[0].nickname").value("루시")) +// .andExpect(jsonPath("$.result.memberAnswerInfos[0].nickname").value("루시")) .andExpect(jsonPath("$.result.memberAnswerInfos[0].content").value("나는 무자비한 사람이 좋아")); } diff --git a/src/test/java/com/server/capple/domain/answerComment/controller/AnswerCommentControllerTest.java b/src/test/java/com/server/capple/domain/answerComment/controller/AnswerCommentControllerTest.java index e4838564..4cfd0898 100644 --- a/src/test/java/com/server/capple/domain/answerComment/controller/AnswerCommentControllerTest.java +++ b/src/test/java/com/server/capple/domain/answerComment/controller/AnswerCommentControllerTest.java @@ -149,7 +149,7 @@ public void getAnswerCommentInfosTest() throws Exception { .andExpect(jsonPath("$.code").value("COMMON200")) .andExpect(jsonPath("$.message").value("요청에 성공하였습니다.")) .andExpect(jsonPath("$.result.answerCommentInfos[0].answerCommentId").value(1L)) - .andExpect(jsonPath("$.result.answerCommentInfos[0].writer").value("루시")) +// .andExpect(jsonPath("$.result.answerCommentInfos[0].writer").value("루시")) .andExpect(jsonPath("$.result.answerCommentInfos[0].content").value("댓글 1")) .andExpect(jsonPath("$.result.answerCommentInfos[0].heartCount").value(3L)) .andExpect(jsonPath("$.result.answerCommentInfos[0].createdAt").value("2022-11-01T12:02:00")); diff --git a/src/test/java/com/server/capple/domain/answerComment/service/AnswerCommentServiceTest.java b/src/test/java/com/server/capple/domain/answerComment/service/AnswerCommentServiceTest.java index df077628..87962724 100644 --- a/src/test/java/com/server/capple/domain/answerComment/service/AnswerCommentServiceTest.java +++ b/src/test/java/com/server/capple/domain/answerComment/service/AnswerCommentServiceTest.java @@ -101,7 +101,7 @@ public void getAnswerCommentsTest() { AnswerCommentInfos response = answerCommentService.getAnswerCommentInfos(answer.getId()); //then - assertEquals("루시", response.getAnswerCommentInfos().get(0).getWriter()); + assertEquals(member.getId(), response.getAnswerCommentInfos().get(0).getWriterId()); assertEquals("답변에 대한 댓글이어유", response.getAnswerCommentInfos().get(0).getContent()); assertEquals(0L, response.getAnswerCommentInfos().get(0).getHeartCount()); } diff --git a/src/test/java/com/server/capple/domain/boardComment/controller/BoardCommentControllerTest.java b/src/test/java/com/server/capple/domain/boardComment/controller/BoardCommentControllerTest.java index f1af70b0..b526fedf 100644 --- a/src/test/java/com/server/capple/domain/boardComment/controller/BoardCommentControllerTest.java +++ b/src/test/java/com/server/capple/domain/boardComment/controller/BoardCommentControllerTest.java @@ -149,7 +149,7 @@ public void getBoardCommentInfosTest() throws Exception { .andExpect(jsonPath("$.code").value("COMMON200")) .andExpect(jsonPath("$.message").value("요청에 성공하였습니다.")) .andExpect(jsonPath("$.result.boardCommentInfos[0].boardCommentId").value(1L)) - .andExpect(jsonPath("$.result.boardCommentInfos[0].writer").value("루시")) +// .andExpect(jsonPath("$.result.boardCommentInfos[0].writer").value("루시")) .andExpect(jsonPath("$.result.boardCommentInfos[0].content").value("댓글")) .andExpect(jsonPath("$.result.boardCommentInfos[0].heartCount").value(2L)) .andExpect(jsonPath("$.result.boardCommentInfos[0].isLiked").value(true)); diff --git a/src/test/java/com/server/capple/domain/boardComment/service/BoardCommentServiceTest.java b/src/test/java/com/server/capple/domain/boardComment/service/BoardCommentServiceTest.java index d1accde2..21286fd8 100644 --- a/src/test/java/com/server/capple/domain/boardComment/service/BoardCommentServiceTest.java +++ b/src/test/java/com/server/capple/domain/boardComment/service/BoardCommentServiceTest.java @@ -103,7 +103,7 @@ public void getBoardCommentsTest() { BoardCommentInfos response = boardCommentService.getBoardCommentInfos(member, board.getId()); //then - assertEquals("루시", response.getBoardCommentInfos().get(0).getWriter()); + assertEquals(member.getId(), response.getBoardCommentInfos().get(0).getWriterId()); assertEquals("게시글 댓글", response.getBoardCommentInfos().get(0).getContent()); assertEquals(0L, response.getBoardCommentInfos().get(0).getHeartCount()); assertEquals(false, response.getBoardCommentInfos().get(0).getIsLiked()); diff --git a/src/test/java/com/server/capple/support/ControllerTestConfig.java b/src/test/java/com/server/capple/support/ControllerTestConfig.java index 5144dca7..aafdc453 100644 --- a/src/test/java/com/server/capple/support/ControllerTestConfig.java +++ b/src/test/java/com/server/capple/support/ControllerTestConfig.java @@ -100,7 +100,7 @@ protected MemberAnswerList getMemberAnswerList () { List memberAnswerInfos = List.of(AnswerResponse.MemberAnswerInfo.builder() .questionId(answer.getQuestion().getId()) .answerId(answer.getId()) - .nickname(answer.getMember().getNickname()) + .writerId(member.getId()) .profileImage(answer.getMember().getProfileImage()) .content(answer.getContent()) .heartCount(1) @@ -117,7 +117,7 @@ protected BoardCommentInfos getBoardCommentInfos() { List commentInfos = List.of(BoardCommentInfo.builder() .boardCommentId(1L) - .writer(member.getNickname()) + .writerId(member.getId()) .content("댓글") .createdAt(LocalDateTime.now()) .heartCount(2L) @@ -136,7 +136,7 @@ protected AnswerCommentRequest getAnswerCommentRequest() { protected AnswerCommentInfos getAnswerCommentInfos () { List answerCommentInfos = List.of(AnswerCommentInfo.builder() .answerCommentId(1L) - .writer(member.getNickname()) + .writerId(member.getId()) .content("댓글 1") .createdAt(LocalDateTime.of(2022, 11, 1, 12, 02)) .heartCount(3L) From 9de55fd34a3b135525c6bd63089652df68da67d9 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 11:57:45 +0900 Subject: [PATCH 08/16] =?UTF-8?q?fix:=20#137=20AnswerCommentInfo=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=AC=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/server/capple/domain/board/dto/BoardResponse.java | 4 ++-- .../java/com/server/capple/support/ServiceTestConfig.java | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java index 54991aec..e4286499 100644 --- a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java +++ b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java @@ -25,7 +25,7 @@ public static class BoardCreate { @AllArgsConstructor @NoArgsConstructor public static class BoardsGetByBoardType { - private List boards = new ArrayList<>(); + private List boards; } @Getter @@ -55,7 +55,7 @@ public static class BoardDelete { @AllArgsConstructor @NoArgsConstructor public static class BoardsSearchByKeyword { - private List boards = new ArrayList<>(); + private List boards; } @Getter diff --git a/src/test/java/com/server/capple/support/ServiceTestConfig.java b/src/test/java/com/server/capple/support/ServiceTestConfig.java index 61943193..8bb1698f 100644 --- a/src/test/java/com/server/capple/support/ServiceTestConfig.java +++ b/src/test/java/com/server/capple/support/ServiceTestConfig.java @@ -85,6 +85,7 @@ protected Question createLiveQuestion() { .content("아카데미 러너 중 가장 마음에 드는 유형이 있나요?") .questionStatus(QuestionStatus.LIVE) .livedAt(LocalDateTime.now()) + .commentCount(0) .build() ); } @@ -95,6 +96,7 @@ protected Question createOldQuestion() { .content("오늘 뭐 먹을 거에요?") .questionStatus(QuestionStatus.OLD) .livedAt(LocalDateTime.of(2024, 04, 01, 00, 00, 00)) + .commentCount(0) .build() ); } @@ -104,6 +106,7 @@ protected Question createPendingQuestion() { Question.builder() .content("가장 좋아하는 음식은 무엇인가요?") .questionStatus(QuestionStatus.PENDING) + .commentCount(0) .build() ); } From 6ec61b07a5ec38aa77d513382644ec16fb0116a2 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Wed, 4 Sep 2024 13:02:09 +0900 Subject: [PATCH 09/16] =?UTF-8?q?fix:=20#137=20=EC=9E=91=EC=84=B1=ED=95=9C?= =?UTF-8?q?=20=EB=8B=B5=EB=B3=80=20=EC=A1=B0=ED=9A=8C=20Response=20DTO=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/server/capple/domain/answer/dto/AnswerResponse.java | 1 + .../com/server/capple/domain/answer/mapper/AnswerMapper.java | 3 ++- .../capple/domain/answer/service/AnswerServiceImpl.java | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java b/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java index 2f397a34..5bcf2442 100644 --- a/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java +++ b/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java @@ -53,6 +53,7 @@ public static class MemberAnswerInfo { private String content; private int heartCount; private String writeAt; + private Boolean isLiked; } @Getter diff --git a/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java b/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java index 8cec8c5b..1741a2eb 100644 --- a/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java +++ b/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java @@ -43,7 +43,7 @@ public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported, .build(); } - public MemberAnswerInfo toMemberAnswerInfo(Answer answer, int heartCount) { + public MemberAnswerInfo toMemberAnswerInfo(Answer answer, int heartCount, Boolean isLiked) { return MemberAnswerInfo.builder() .questionId(answer.getQuestion().getId()) .answerId(answer.getId()) @@ -53,6 +53,7 @@ public MemberAnswerInfo toMemberAnswerInfo(Answer answer, int heartCount) { .content(answer.getContent()) .heartCount(heartCount) .writeAt(answer.getCreatedAt().toString()) + .isLiked(isLiked) .build(); } diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index 49af7d69..65d24e13 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -111,7 +111,7 @@ public MemberAnswerList getMemberAnswer(Member member) { List answers = answerRepository.findByMember(member).orElse(null); return answerMapper.toMemberAnswerList( answers.stream() - .map(answer -> answerMapper.toMemberAnswerInfo(answer, answerHeartRedisRepository.getAnswerHeartsCount(answer.getId()))) + .map(answer -> answerMapper.toMemberAnswerInfo(answer, answerHeartRedisRepository.getAnswerHeartsCount(answer.getId()), answerHeartRedisRepository.isMemberLikedAnswer(member.getId(), answer.getId()))) .toList() ); } @@ -122,7 +122,7 @@ public MemberAnswerList getMemberHeartAnswer(Member member) { return answerMapper.toMemberAnswerList( answerHeartRedisRepository.getMemberHeartsAnswer(member.getId()) .stream() - .map(answerId -> answerMapper.toMemberAnswerInfo(findAnswer((answerId)), answerHeartRedisRepository.getAnswerHeartsCount(answerId))) + .map(answerId -> answerMapper.toMemberAnswerInfo(findAnswer((answerId)), answerHeartRedisRepository.getAnswerHeartsCount(answerId), answerHeartRedisRepository.isMemberLikedAnswer(member.getId(), answerId))) .toList() ); } From b1dd2207fb1fd0d1edb251d00131d65533475fe4 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Sun, 8 Sep 2024 16:50:17 +0900 Subject: [PATCH 10/16] =?UTF-8?q?feat:=20#137=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=ED=8C=90=20DTO=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80(?= =?UTF-8?q?=EC=8B=A0=EA=B3=A0=ED=95=A8=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EC=9D=B4=ED=9B=84=20=EC=B6=94=EA=B0=80=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=ED=95=84=EC=9A=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/server/capple/domain/board/dto/BoardResponse.java | 3 +++ .../com/server/capple/domain/board/mapper/BoardMapper.java | 7 ++++++- .../capple/domain/board/service/BoardServiceImpl.java | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java index e4286499..2ab95bc9 100644 --- a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java +++ b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java @@ -39,6 +39,9 @@ public static class BoardsGetByBoardTypeBoardInfo { private Integer heartCount; private Integer commentCount; private LocalDateTime createAt; + private Boolean liLiked; + private Boolean isMine; + private Boolean isReported; private Boolean isLiked; } diff --git a/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java b/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java index cd1a2134..e53377a1 100644 --- a/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java +++ b/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java @@ -45,7 +45,9 @@ public BoardResponse.BoardsGetByBoardType toBoardsGetByBoardType( public BoardResponse.BoardsGetByBoardTypeBoardInfo toBoardsGetByBoardTypeBoardInfo( Board board, Integer boardHeartsCount, - Boolean isLiked) { + Boolean isLiked, + Boolean isMine, + Boolean isReported) { return BoardResponse.BoardsGetByBoardTypeBoardInfo.builder() .boardId(board.getId()) .writerId(board.getWriter().getId()) @@ -54,6 +56,9 @@ public BoardResponse.BoardsGetByBoardTypeBoardInfo toBoardsGetByBoardTypeBoardIn .commentCount(board.getCommentCount()) .createAt(board.getCreatedAt()) .isLiked(isLiked) + .isMine(isMine) + // TODO: BoardReport 관련 테이블 구현 후 수정 요망 + .isReported(isReported) .build(); } diff --git a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java index b7876968..b9289921 100644 --- a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java +++ b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java @@ -50,7 +50,8 @@ public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(Member member, Bo throw new RestApiException(BoardErrorCode.BOARD_BAD_REQUEST); } return boardMapper.toBoardsGetByBoardType(boards.stream() - .map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId()), boardHeartRedisRepository.isMemberLikedBoard(member.getId(), board.getId()))) + // TODO: BoardReport 관련 테이블 구현 후 수정 요망 + .map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId()), boardHeartRedisRepository.isMemberLikedBoard(member.getId(), board.getId()), board.getWriter().getId().equals(member.getId()), false)) .toList() ); } From 6f8a6a224c802f944f0b167c6808735ce6c15ce2 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Mon, 9 Sep 2024 23:26:04 +0900 Subject: [PATCH 11/16] =?UTF-8?q?feat:=20#137=20=EC=9D=B4=EB=AF=B8=20?= =?UTF-8?q?=EB=8B=B5=EB=B3=80=ED=95=9C=20=EC=A7=88=EB=AC=B8=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../capple/domain/answer/service/AnswerServiceImpl.java | 4 ++++ .../capple/global/exception/errorCode/AnswerErrorCode.java | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index 65d24e13..b76d4bd0 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -40,6 +40,10 @@ public AnswerResponse.AnswerId createAnswer(Member loginMember, Long questionId, Member member = memberService.findMember(loginMember.getId()); Question question = questionService.findQuestion(questionId); + if (answerRepository.existsByQuestionAndMember(question, loginMember)) { + throw new RestApiException(AnswerErrorCode.ANSWER_ALREADY_EXIST); + } + //답변 저장 Answer answer = answerRepository.save(answerMapper.toAnswerEntity(request, member, question)); answer.getQuestion().increaseCommentCount(); diff --git a/src/main/java/com/server/capple/global/exception/errorCode/AnswerErrorCode.java b/src/main/java/com/server/capple/global/exception/errorCode/AnswerErrorCode.java index 56b0da4d..f42150ac 100644 --- a/src/main/java/com/server/capple/global/exception/errorCode/AnswerErrorCode.java +++ b/src/main/java/com/server/capple/global/exception/errorCode/AnswerErrorCode.java @@ -10,7 +10,9 @@ @AllArgsConstructor public enum AnswerErrorCode implements ErrorCodeInterface { ANSWER_NOT_FOUND("ANSWER001", "답변을 찾을 수 없습니다.", HttpStatus.NOT_FOUND), - ANSWER_UNAUTHORIZED("ANSWER002", "답변에 대한 권한이 업습니다.", HttpStatus.FORBIDDEN); + ANSWER_UNAUTHORIZED("ANSWER002", "답변에 대한 권한이 업습니다.", HttpStatus.FORBIDDEN), + ANSWER_ALREADY_EXIST("ANSWER003", "이미 답변한 질문입니다.", HttpStatus.BAD_REQUEST), + ; private final String code; private final String message; From 3942015338675a516f9b6bfa42e59b694cc02ecb Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Mon, 9 Sep 2024 23:43:31 +0900 Subject: [PATCH 12/16] =?UTF-8?q?feat:=20#137=20=EC=9D=B4=EB=AF=B8=20?= =?UTF-8?q?=EB=8B=B5=EB=B3=80=ED=95=9C=20=EC=A7=88=EB=AC=B8=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../answer/service/AnswerServiceTest.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java b/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java index 78ae87ba..564c01d5 100644 --- a/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java +++ b/src/test/java/com/server/capple/domain/answer/service/AnswerServiceTest.java @@ -4,6 +4,7 @@ import com.server.capple.domain.answer.dto.AnswerResponse; import com.server.capple.domain.answer.entity.Answer; import com.server.capple.domain.tag.service.TagService; +import com.server.capple.global.exception.RestApiException; import com.server.capple.support.ServiceTestConfig; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -13,8 +14,7 @@ import java.util.List; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; @DisplayName("Answer 서비스의 ") @SpringBootTest @@ -25,9 +25,25 @@ public class AnswerServiceTest extends ServiceTestConfig { private TagService tagService; @Test - @DisplayName("Answer 생성 테스트") + @DisplayName("Answer 중복 생성 시 예외 발생 테스트") + @Transactional + public void createDuplicateAnswerTest() { + // given + AnswerRequest request = getAnswerRequest(); + + // 이미 `setUp()`에서 답변이 생성된 상태이므로, 다시 답변을 생성하면 예외 발생 + // then + assertThrows(RestApiException.class, () -> { + answerService.createAnswer(member, liveQuestion.getId(), request); + }); + } + + @Test + @DisplayName("Answer 중복 생성 방지 후 새로운 답변 생성 테스트") @Transactional public void createAnswerTest() { + // 기존 답변 삭제 + answerService.deleteAnswer(member, answer.getId()); //given AnswerRequest request = getAnswerRequest(); @@ -41,9 +57,11 @@ public void createAnswerTest() { } @Test - @DisplayName("Answer 수정 테스트") + @DisplayName("Answer 중복 생성 방지 후 새로운 답변 수정 테스트") @Transactional public void updateAnswerTest() { + // 기존 답변 삭제 + answerService.deleteAnswer(member, answer.getId()); //given AnswerRequest request = getAnswerRequest(); Long answerId = answerService.createAnswer(member, liveQuestion.getId(), request).getAnswerId(); @@ -61,9 +79,11 @@ public void updateAnswerTest() { } @Test - @DisplayName("Answer 삭제 테스트") + @DisplayName("Answer 중복 생성 방지 후 새로운 답변 삭제 테스트") @Transactional public void deleteAnswerTest() { + // 기존 답변 삭제 + answerService.deleteAnswer(member, answer.getId()); //given AnswerRequest request = getAnswerRequest(); Long answerId = answerService.createAnswer(member, liveQuestion.getId(), request).getAnswerId(); From f48cb144ce3f51c4bbf5dc1c97cfda6919694ae5 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Mon, 9 Sep 2024 23:53:31 +0900 Subject: [PATCH 13/16] =?UTF-8?q?fix:=20#137=20BoardHeart=20createdAt=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?&=20=EC=A4=91=EB=B3=B5=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20DISTINCT=20=EA=B5=AC?= =?UTF-8?q?=EB=AC=B8=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../question/repository/QuestionHeartRedisRepository.java | 5 ----- .../domain/question/repository/QuestionRepository.java | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java b/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java index db14a4ff..f647e948 100644 --- a/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java +++ b/src/main/java/com/server/capple/domain/question/repository/QuestionHeartRedisRepository.java @@ -26,20 +26,15 @@ public class QuestionHeartRedisRepository implements Serializable { public Boolean toggleBoardHeart(Long memberId, Long boardId) { String key = QUESTION_HEART_KEY_PREFIX + boardId.toString(); String member = MEMBER_KEY_PREFIX + memberId.toString(); - String createAtKey = key + ":" + member + ":createAt"; // member ID를 포함한 createAtKey SetOperations setOperations = redisTemplate.opsForSet(); - ValueOperations valueOperations = redisTemplate.opsForValue(); //해당 key에 member가 존재하지 않으면 추가, 존재하면 삭제 if (FALSE.equals(setOperations.isMember(key, member))) { setOperations.add(key, member); - // 현재 시간을 createAtKey로 저장 - valueOperations.set(createAtKey, LocalDateTime.now().toString()); return TRUE; } else { setOperations.remove(key, member); // 좋아요 취소 시 생성 시간도 삭제할 수 있음 - redisTemplate.delete(createAtKey); return FALSE; } } diff --git a/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java b/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java index b9685036..ffe97da0 100644 --- a/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java +++ b/src/main/java/com/server/capple/domain/question/repository/QuestionRepository.java @@ -26,7 +26,7 @@ public interface QuestionRepository extends JpaRepository { Optional findByQuestionStatusIsLiveAndOldOrderByLivedAt(); - @Query("SELECT DISTINCT q AS question, (a IS NOT NULL) AS isAnsweredByMember " + + @Query("SELECT q AS question, (a IS NOT NULL) AS isAnsweredByMember " + "FROM Question q LEFT JOIN Answer a ON q = a.question AND a.deletedAt is NULL AND a.member = :member " + "WHERE q.questionStatus = 'OLD' OR q.questionStatus = 'LIVE' " + "ORDER BY q.livedAt DESC") From 08026359c73b718d96df7e6ca7330f7881ec6760 Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Tue, 10 Sep 2024 00:38:30 +0900 Subject: [PATCH 14/16] =?UTF-8?q?fix:=20#137=20Question=20DTO=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../answer/service/AnswerServiceImpl.java | 4 ++-- .../dto/response/QuestionResponse.java | 8 ++++---- .../domain/question/entity/Question.java | 18 +++++++++--------- .../domain/question/mapper/QuestionMapper.java | 14 +++++++------- .../question/service/QuestionServiceImpl.java | 6 +++--- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index b76d4bd0..4d0326cb 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -46,7 +46,7 @@ public AnswerResponse.AnswerId createAnswer(Member loginMember, Long questionId, //답변 저장 Answer answer = answerRepository.save(answerMapper.toAnswerEntity(request, member, question)); - answer.getQuestion().increaseCommentCount(); +// answer.getQuestion().increaseCommentCount(); return new AnswerResponse.AnswerId(answer.getId()); } @@ -71,7 +71,7 @@ public AnswerResponse.AnswerId deleteAnswer(Member loginMember, Long answerId) { Answer answer = findAnswer(answerId); checkPermission(loginMember, answer); - answer.getQuestion().decreaseCommentCount(); +// answer.getQuestion().decreaseCommentCount(); answer.delete(); diff --git a/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java b/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java index 4de8f6ca..d792f632 100644 --- a/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java +++ b/src/main/java/com/server/capple/domain/question/dto/response/QuestionResponse.java @@ -19,8 +19,8 @@ public static class QuestionSummary { private Long questionId; private QuestionStatus questionStatus; private String content; - private Integer likeCount; - private Integer commentCount; +// private Integer likeCount; +// private Integer commentCount; private Boolean isAnswered; } @@ -34,8 +34,8 @@ public static class QuestionInfo { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") private LocalDateTime livedAt; private String content; - private Integer likeCount; - private Integer commentCount; +// private Integer likeCount; +// private Integer commentCount; private Boolean isAnswered; } diff --git a/src/main/java/com/server/capple/domain/question/entity/Question.java b/src/main/java/com/server/capple/domain/question/entity/Question.java index 4e39a1cb..27a82495 100644 --- a/src/main/java/com/server/capple/domain/question/entity/Question.java +++ b/src/main/java/com/server/capple/domain/question/entity/Question.java @@ -36,8 +36,8 @@ public class Question extends BaseEntity { private LocalDateTime livedAt; - @Column(nullable = false) - private Integer commentCount; +// @Column(nullable = false) +// private Integer commentCount; //question Status를 바꾸는 함수 public void setQuestionStatus(QuestionStatus questionStatus) { @@ -47,12 +47,12 @@ public void setQuestionStatus(QuestionStatus questionStatus) { this.livedAt = LocalDateTime.now(); } - public void increaseCommentCount() { - this.commentCount += 1; - } - - public void decreaseCommentCount() { - this.commentCount -= 1; - } +// public void increaseCommentCount() { +// this.commentCount += 1; +// } +// +// public void decreaseCommentCount() { +// this.commentCount -= 1; +// } } diff --git a/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java b/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java index 3825fee9..ee9e4360 100644 --- a/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java +++ b/src/main/java/com/server/capple/domain/question/mapper/QuestionMapper.java @@ -16,30 +16,30 @@ public Question toQuestion(QuestionCreate request) { return Question.builder() .questionStatus(request.getQuestionStatus()) .content(request.getContent()) - .commentCount(0) +// .commentCount(0) .build(); } - public QuestionSummary toQuestionSummary(Question question, boolean isAnswered, Integer likeCount) { + public QuestionSummary toQuestionSummary(Question question, boolean isAnswered/*, Integer likeCount*/) { return QuestionSummary.builder() .questionId(question.getId()) .questionStatus(question.getQuestionStatus()) .content(question.getContent()) .isAnswered(isAnswered) - .likeCount(likeCount) - .commentCount(question.getCommentCount()) +// .likeCount(likeCount) +// .commentCount(question.getCommentCount()) .build(); } - public QuestionInfo toQuestionInfo(Question question, boolean isAnswered, Integer likeCount) { + public QuestionInfo toQuestionInfo(Question question, boolean isAnswered/*, Integer likeCount*/) { return QuestionInfo.builder() .questionId(question.getId()) .questionStatus(question.getQuestionStatus()) .livedAt(question.getLivedAt()) .content(question.getContent()) - .likeCount(likeCount) - .commentCount(question.getCommentCount()) +// .likeCount(likeCount) +// .commentCount(question.getCommentCount()) .isAnswered(isAnswered) .build(); } diff --git a/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java b/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java index 33d2f2ca..aeb5af35 100644 --- a/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java +++ b/src/main/java/com/server/capple/domain/question/service/QuestionServiceImpl.java @@ -44,7 +44,7 @@ public QuestionSummary getMainQuestion(Member member) { boolean isAnswered = answerRepository.existsByQuestionAndMember(mainQuestion, member); - return questionMapper.toQuestionSummary(mainQuestion, isAnswered, questionHeartRepository.getQuestionHeartsCount(mainQuestion.getId())); + return questionMapper.toQuestionSummary(mainQuestion, isAnswered/*, questionHeartRepository.getQuestionHeartsCount(mainQuestion.getId())*/); } @Override @@ -54,8 +54,8 @@ public QuestionInfos getQuestions(Member member) { return questionMapper.toQuestionInfos(questions.stream() .map(questionInfo -> questionMapper.toQuestionInfo(questionInfo.getQuestion(), - questionInfo.getIsAnsweredByMember(), - questionHeartRepository.getQuestionHeartsCount(questionInfo.getQuestion().getId())) + questionInfo.getIsAnsweredByMember()/*, + questionHeartRepository.getQuestionHeartsCount(questionInfo.getQuestion().getId())*/) ).toList()); } From e0b7c0609cf5343b114864eb3dd1e7414dfeeeed Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Tue, 10 Sep 2024 00:54:14 +0900 Subject: [PATCH 15/16] =?UTF-8?q?feat:=20#137=20CommentCount=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/server/capple/support/ServiceTestConfig.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/java/com/server/capple/support/ServiceTestConfig.java b/src/test/java/com/server/capple/support/ServiceTestConfig.java index 8bb1698f..bc99047a 100644 --- a/src/test/java/com/server/capple/support/ServiceTestConfig.java +++ b/src/test/java/com/server/capple/support/ServiceTestConfig.java @@ -85,7 +85,6 @@ protected Question createLiveQuestion() { .content("아카데미 러너 중 가장 마음에 드는 유형이 있나요?") .questionStatus(QuestionStatus.LIVE) .livedAt(LocalDateTime.now()) - .commentCount(0) .build() ); } @@ -96,7 +95,6 @@ protected Question createOldQuestion() { .content("오늘 뭐 먹을 거에요?") .questionStatus(QuestionStatus.OLD) .livedAt(LocalDateTime.of(2024, 04, 01, 00, 00, 00)) - .commentCount(0) .build() ); } @@ -106,7 +104,6 @@ protected Question createPendingQuestion() { Question.builder() .content("가장 좋아하는 음식은 무엇인가요?") .questionStatus(QuestionStatus.PENDING) - .commentCount(0) .build() ); } @@ -132,7 +129,7 @@ protected Board createBoard() { .boardType(BoardType.FREEBOARD) .writer(member) .content("오늘 밥먹을 사람!") - .commentCount(2) + .commentCount(0) .build()); } protected BoardComment createBoardComment() { From 7f85a0af4e514c50c3f120399d564c58d85e5bcd Mon Sep 17 00:00:00 2001 From: KyungsooLee Date: Tue, 10 Sep 2024 01:00:33 +0900 Subject: [PATCH 16/16] =?UTF-8?q?feat:=20#137=20=EB=B9=84=EA=B5=90=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A1=9C=EC=A7=81=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/server/capple/domain/answer/dto/AnswerResponse.java | 2 +- .../com/server/capple/domain/answer/mapper/AnswerMapper.java | 4 ++-- .../capple/domain/answer/service/AnswerServiceImpl.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java b/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java index 5bcf2442..7f35b5d3 100644 --- a/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java +++ b/src/main/java/com/server/capple/domain/answer/dto/AnswerResponse.java @@ -20,7 +20,7 @@ public static class AnswerInfo { private String profileImage; private String nickname; private String content; - private Boolean isMyAnswer; + private Boolean isMine; private Boolean isReported; private Boolean isLiked; private String writeAt; diff --git a/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java b/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java index 1741a2eb..10264ec0 100644 --- a/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java +++ b/src/main/java/com/server/capple/domain/answer/mapper/AnswerMapper.java @@ -29,14 +29,14 @@ public AnswerList toAnswerList(List answerInfoList) { .build(); } - public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported, Boolean isLiked) { + public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported, Boolean isLiked, Boolean isMine) { return AnswerInfo.builder() .answerId(answer.getId()) .writerId(answer.getMember().getId()) .profileImage(answer.getMember().getProfileImage()) .nickname(answer.getMember().getNickname()) .content(answer.getContent()) - .isMyAnswer(answer.getMember().getId() == memberId) + .isMine(isMine) .isReported(isReported) .isLiked(isLiked) .writeAt(answer.getCreatedAt().toString()) diff --git a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java index 4d0326cb..a134d04c 100644 --- a/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java +++ b/src/main/java/com/server/capple/domain/answer/service/AnswerServiceImpl.java @@ -96,14 +96,14 @@ public AnswerList getAnswerList(Long memberId, Long questionId, String keyword, answerRepository.findByQuestion(questionId, pageable).orElseThrow(() -> new RestApiException(AnswerErrorCode.ANSWER_NOT_FOUND)) .stream() - .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()))) + .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()), answer.getMember().getId().equals(memberId))) .toList()); } else { return answerMapper.toAnswerList( answerRepository.findByQuestionAndKeyword(questionId, keyword, pageable).orElseThrow(() -> new RestApiException(AnswerErrorCode.ANSWER_NOT_FOUND)) .stream() - .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()))) + .map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()), answer.getMember().getId().equals(memberId))) .toList()); }