From e18875cd3889c7b4b4d0fef094dfdbefca582edb Mon Sep 17 00:00:00 2001 From: ybchar Date: Sun, 7 Apr 2024 04:28:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20CUD=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../daybook/domain/board/entity/Board.java | 8 ++- .../comment/controller/CommentController.java | 54 ++++++++++++++ .../dto/request/CommentCreateRequest.java | 7 ++ .../dto/request/CommentModifyRequest.java | 6 ++ .../dto/response/CommentCreateResponse.java | 19 +++++ .../dto/response/CommentModifyResponse.java | 12 ++++ .../domain/comment/entity/Comment.java | 72 ++++++++++++++----- .../comment/repository/CommentRepository.java | 8 +++ .../repository/CommentRepositoryCustom.java | 4 ++ .../repository/CommentRepositoryImpl.java | 16 +++++ .../comment/service/CommentService.java | 53 ++++++++++++++ .../global/error/exception/ErrorCode.java | 7 +- 12 files changed, 247 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/unit/daybook/domain/comment/controller/CommentController.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/dto/request/CommentCreateRequest.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/dto/request/CommentModifyRequest.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/dto/response/CommentCreateResponse.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/dto/response/CommentModifyResponse.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/repository/CommentRepository.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java create mode 100644 src/main/java/com/unit/daybook/domain/comment/service/CommentService.java diff --git a/src/main/java/com/unit/daybook/domain/board/entity/Board.java b/src/main/java/com/unit/daybook/domain/board/entity/Board.java index 724c720..f83fa4a 100644 --- a/src/main/java/com/unit/daybook/domain/board/entity/Board.java +++ b/src/main/java/com/unit/daybook/domain/board/entity/Board.java @@ -1,6 +1,7 @@ package com.unit.daybook.domain.board.entity; import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto; +import com.unit.daybook.domain.comment.entity.Comment; import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import com.unit.daybook.domain.reaction.entity.Reaction; @@ -42,12 +43,15 @@ public class Board extends BaseTimeEntity { @Column private Long hearts; - @OneToMany(mappedBy = "board", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true) private List hashtags = new ArrayList<>(); - @OneToMany(mappedBy = "board", cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true) private List reactions = new ArrayList<>(); + @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true) + private List comments = new ArrayList<>(); + @Builder(access = AccessLevel.PRIVATE) public Board(Long boardId, String content, Long respectBoardId, Member member, String category, Long hearts, String paperType) { this.boardId = boardId; diff --git a/src/main/java/com/unit/daybook/domain/comment/controller/CommentController.java b/src/main/java/com/unit/daybook/domain/comment/controller/CommentController.java new file mode 100644 index 0000000..dc53ba6 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/controller/CommentController.java @@ -0,0 +1,54 @@ +package com.unit.daybook.domain.comment.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.unit.daybook.domain.comment.dto.request.CommentCreateRequest; +import com.unit.daybook.domain.comment.dto.request.CommentModifyRequest; +import com.unit.daybook.domain.comment.dto.response.CommentCreateResponse; +import com.unit.daybook.domain.comment.dto.response.CommentModifyResponse; +import com.unit.daybook.domain.comment.service.CommentService; +import com.unit.daybook.domain.common.annotation.LoginUsers; +import com.unit.daybook.global.config.security.CustomUserDetails; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; + +@RestController +@RequestMapping("/comments") +@RequiredArgsConstructor +public class CommentController { + + private final CommentService commentService; + + @PostMapping + public ResponseEntity commentCreate( + @Valid @RequestBody CommentCreateRequest request, + @LoginUsers CustomUserDetails userDetails + ) { + CommentCreateResponse comment = commentService.createComment(request, userDetails.getMemberId()); + return ResponseEntity.status(HttpStatus.CREATED).body(comment); + } + + @DeleteMapping("/{commentId}") + public void commentDelete( + @PathVariable Long commentId + ) { + commentService.deleteComment(commentId); + } + + @PutMapping("/{commentId}") + public CommentModifyResponse commentModify( + @Valid @RequestBody CommentModifyRequest request, + @PathVariable Long commentId + ) { + return commentService.updateComment(request, commentId); + } +} diff --git a/src/main/java/com/unit/daybook/domain/comment/dto/request/CommentCreateRequest.java b/src/main/java/com/unit/daybook/domain/comment/dto/request/CommentCreateRequest.java new file mode 100644 index 0000000..46069f3 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/dto/request/CommentCreateRequest.java @@ -0,0 +1,7 @@ +package com.unit.daybook.domain.comment.dto.request; + +public record CommentCreateRequest ( + String content, + Long boardId +) { +} diff --git a/src/main/java/com/unit/daybook/domain/comment/dto/request/CommentModifyRequest.java b/src/main/java/com/unit/daybook/domain/comment/dto/request/CommentModifyRequest.java new file mode 100644 index 0000000..d55035f --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/dto/request/CommentModifyRequest.java @@ -0,0 +1,6 @@ +package com.unit.daybook.domain.comment.dto.request; + +public record CommentModifyRequest( + String content +) { +} diff --git a/src/main/java/com/unit/daybook/domain/comment/dto/response/CommentCreateResponse.java b/src/main/java/com/unit/daybook/domain/comment/dto/response/CommentCreateResponse.java new file mode 100644 index 0000000..e37847a --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/dto/response/CommentCreateResponse.java @@ -0,0 +1,19 @@ +package com.unit.daybook.domain.comment.dto.response; + +import com.unit.daybook.domain.comment.entity.Comment; + +public record CommentCreateResponse( + Long commentId, + String content, + Long boardId, + Long memberId +) { + public static CommentCreateResponse from(Comment comment) { + return new CommentCreateResponse( + comment.getCommentId(), + comment.getContent(), + comment.getBoard().getBoardId(), + comment.getMember().getId() + ); + } +} diff --git a/src/main/java/com/unit/daybook/domain/comment/dto/response/CommentModifyResponse.java b/src/main/java/com/unit/daybook/domain/comment/dto/response/CommentModifyResponse.java new file mode 100644 index 0000000..dba5ab6 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/dto/response/CommentModifyResponse.java @@ -0,0 +1,12 @@ +package com.unit.daybook.domain.comment.dto.response; + +import com.unit.daybook.domain.comment.entity.Comment; + +public record CommentModifyResponse( + String content, + Long commentId +) { + public static CommentModifyResponse from(Comment comment) { + return new CommentModifyResponse(comment.getContent(), comment.getCommentId()); + } +} diff --git a/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java b/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java index 19c3b79..ac41cd1 100644 --- a/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java +++ b/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java @@ -1,22 +1,62 @@ package com.unit.daybook.domain.comment.entity; -import jakarta.persistence.*; +import com.unit.daybook.domain.board.entity.Board; +import com.unit.daybook.domain.member.domain.Member; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -//@Getter -//@Entity -//@Table(name = "comment") -//@NoArgsConstructor(access = AccessLevel.PROTECTED) -//public class Comment { -// -// @Id -// @GeneratedValue(strategy = GenerationType.IDENTITY) -// @Column(name = "comment_id") -// private Long commentId; -// -// @Column -// private String content; -// -//} +@Getter +@Entity +@Table(name = "comment") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Comment { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "comment_id") + private Long commentId; + + @Column(nullable = false, length = 25) + private String content; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "board_id") + private Board board; + + @Builder(access = AccessLevel.PRIVATE) + private Comment(String content, Member member, Board board) { + this.content = content; + this.member = member; + this.board = board; + } + + public static Comment createComment( + String content, Member member, Board board + ) { + return Comment.builder() + .content(content) + .member(member) + .board(board) + .build(); + } + + public void updateContent(String content) { + this.content = content; + } +} diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepository.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepository.java new file mode 100644 index 0000000..09fa7ff --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepository.java @@ -0,0 +1,8 @@ +package com.unit.daybook.domain.comment.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.unit.daybook.domain.comment.entity.Comment; + +public interface CommentRepository extends JpaRepository, CommentRepositoryCustom { +} diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java new file mode 100644 index 0000000..4aeac96 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java @@ -0,0 +1,4 @@ +package com.unit.daybook.domain.comment.repository; + +public interface CommentRepositoryCustom { +} diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java new file mode 100644 index 0000000..277bdea --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java @@ -0,0 +1,16 @@ +package com.unit.daybook.domain.comment.repository; + +import org.springframework.stereotype.Repository; + +import com.querydsl.jpa.impl.JPAQueryFactory; + +import lombok.RequiredArgsConstructor; + +@Repository +@RequiredArgsConstructor +public class CommentRepositoryImpl { + + private final JPAQueryFactory jpaQueryFactory; + + +} diff --git a/src/main/java/com/unit/daybook/domain/comment/service/CommentService.java b/src/main/java/com/unit/daybook/domain/comment/service/CommentService.java new file mode 100644 index 0000000..7ec84c6 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/service/CommentService.java @@ -0,0 +1,53 @@ +package com.unit.daybook.domain.comment.service; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.unit.daybook.domain.board.entity.Board; +import com.unit.daybook.domain.board.repository.BoardRepository; +import com.unit.daybook.domain.comment.dto.request.CommentCreateRequest; +import com.unit.daybook.domain.comment.dto.request.CommentModifyRequest; +import com.unit.daybook.domain.comment.dto.response.CommentCreateResponse; +import com.unit.daybook.domain.comment.dto.response.CommentModifyResponse; +import com.unit.daybook.domain.comment.entity.Comment; +import com.unit.daybook.domain.comment.repository.CommentRepository; +import com.unit.daybook.domain.member.domain.Member; +import com.unit.daybook.domain.member.repository.MemberRepository; +import com.unit.daybook.global.error.exception.CustomException; +import com.unit.daybook.global.error.exception.ErrorCode; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +@Transactional +public class CommentService { + + private final CommentRepository commentRepository; + private final MemberRepository memberRepository; + private final BoardRepository boardRepository; + + public CommentCreateResponse createComment(CommentCreateRequest request, Long memberId) { + final Member member = findMemberById(memberId); + Board board = boardRepository.findById(request.boardId()) + .orElseThrow(() -> new CustomException(ErrorCode.BOARD_NOT_FOUND)); + Comment comment = Comment.createComment(request.content(), member, board); + return CommentCreateResponse.from(commentRepository.save(comment)); + } + + private Member findMemberById(Long memberId) { + return memberRepository.findById(memberId) + .orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); + } + + public void deleteComment(Long commentId) { + commentRepository.deleteById(commentId); + } + + public CommentModifyResponse updateComment(CommentModifyRequest request, Long commentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + comment.updateContent(request.content()); + return CommentModifyResponse.from(comment); + } +} diff --git a/src/main/java/com/unit/daybook/global/error/exception/ErrorCode.java b/src/main/java/com/unit/daybook/global/error/exception/ErrorCode.java index cf3432e..9475b1f 100644 --- a/src/main/java/com/unit/daybook/global/error/exception/ErrorCode.java +++ b/src/main/java/com/unit/daybook/global/error/exception/ErrorCode.java @@ -28,7 +28,12 @@ public enum ErrorCode { BOARD_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 일지입니다."), // Reaction - REACTION_EXISTS(HttpStatus.CONFLICT, "이미 리액션을 하였습니다."); + REACTION_EXISTS(HttpStatus.CONFLICT, "이미 리액션을 하였습니다."), + + // 댓글 + COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 댓글입니다."), + + ; private final HttpStatus status; private final String message; } From 6c4f701aa0e484127fd538bd91063acc3660fa06 Mon Sep 17 00:00:00 2001 From: ybchar Date: Sun, 7 Apr 2024 04:45:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=9D=BC=EC=A7=80=20comments=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/FindOneBoardResponse.java | 5 ++++- .../daybook/domain/board/entity/Hashtag.java | 3 ++- .../domain/board/service/BoardService.java | 11 +++++++++-- .../dto/response/FindOneCommentResponse.java | 12 ++++++++++++ .../daybook/domain/comment/entity/Comment.java | 3 ++- .../repository/CommentRepositoryCustom.java | 6 ++++++ .../repository/CommentRepositoryImpl.java | 18 ++++++++++++++++-- .../domain/reaction/entity/Reaction.java | 3 ++- 8 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java diff --git a/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java b/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java index 9f8db83..29eab8b 100644 --- a/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java +++ b/src/main/java/com/unit/daybook/domain/board/dto/response/FindOneBoardResponse.java @@ -5,6 +5,7 @@ import com.unit.daybook.domain.board.entity.Board; import com.unit.daybook.domain.board.entity.Hashtag; +import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; import com.unit.daybook.domain.reaction.dto.response.ReactionTypeAndCount; import com.unit.daybook.domain.reaction.entity.Reaction; @@ -19,9 +20,10 @@ public record FindOneBoardResponse( List hashtags, String paperType, List reactions, + List comments, LocalDateTime createdAt ) { - public static FindOneBoardResponse of(Board board, List reactions) { + public static FindOneBoardResponse of(Board board, List reactions, List comments) { List hashContents = board.getHashtags().stream().map(Hashtag::getContent).toList(); Long heartsFromReactions = reactions.stream().mapToLong(ReactionTypeAndCount::count).sum(); return new FindOneBoardResponse( @@ -34,6 +36,7 @@ public static FindOneBoardResponse of(Board board, List re hashContents, board.getPaperType(), reactions, + comments, board.getCreatedAt() ); } diff --git a/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java b/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java index bccbfd8..49795ec 100644 --- a/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java +++ b/src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java @@ -2,6 +2,7 @@ import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto; +import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import jakarta.persistence.*; import lombok.AccessLevel; @@ -15,7 +16,7 @@ @Entity @Table(name = "hashtag") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Hashtag { +public class Hashtag extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "hashtag_id") diff --git a/src/main/java/com/unit/daybook/domain/board/service/BoardService.java b/src/main/java/com/unit/daybook/domain/board/service/BoardService.java index 52007ae..0fe67b1 100644 --- a/src/main/java/com/unit/daybook/domain/board/service/BoardService.java +++ b/src/main/java/com/unit/daybook/domain/board/service/BoardService.java @@ -10,6 +10,9 @@ import com.unit.daybook.domain.board.entity.ReadBoard; import com.unit.daybook.domain.board.repository.*; +import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; +import com.unit.daybook.domain.comment.entity.Comment; +import com.unit.daybook.domain.comment.repository.CommentRepository; import com.unit.daybook.domain.member.domain.Member; import com.unit.daybook.domain.member.repository.MemberRepository; import com.unit.daybook.domain.reaction.dto.response.ReactionTypeAndCount; @@ -33,6 +36,7 @@ public class BoardService { private final ReadBoardRepository readBoardRepository; private final HashtagRepository hashtagRepository; private final ReactionRepository reactionRepository; + private final CommentRepository commentRepository; public BoardResponseDto addBoard(AddBoardRequestDto addBoardRequestDto, Long memberId) { Member member = memberRepository @@ -60,8 +64,11 @@ public FindOneBoardResponse getBoard(Long boardId) { Board board = boardRepository.findById(boardId) .orElseThrow(() -> new CustomException(ErrorCode.BOARD_NOT_FOUND)); List reactions = reactionRepository.findAllByBoardGroupByReactionType(board); - - return FindOneBoardResponse.of(board, reactions); + List comments = commentRepository.findCommentByBoard(boardId) + .stream() + .map(FindOneCommentResponse::from) + .toList(); + return FindOneBoardResponse.of(board, reactions, comments); } @Transactional(readOnly = true) diff --git a/src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java b/src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java new file mode 100644 index 0000000..3a168c5 --- /dev/null +++ b/src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java @@ -0,0 +1,12 @@ +package com.unit.daybook.domain.comment.dto.response; + +import com.unit.daybook.domain.comment.entity.Comment; + +public record FindOneCommentResponse( + Long commentId, + String content +) { + public static FindOneCommentResponse from(Comment comment) { + return new FindOneCommentResponse(comment.getCommentId(), comment.getContent()); + } +} diff --git a/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java b/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java index ac41cd1..4ba1237 100644 --- a/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java +++ b/src/main/java/com/unit/daybook/domain/comment/entity/Comment.java @@ -1,6 +1,7 @@ package com.unit.daybook.domain.comment.entity; import com.unit.daybook.domain.board.entity.Board; +import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import jakarta.persistence.Column; @@ -21,7 +22,7 @@ @Entity @Table(name = "comment") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Comment { +public class Comment extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java index 4aeac96..3350b82 100644 --- a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java @@ -1,4 +1,10 @@ package com.unit.daybook.domain.comment.repository; +import java.util.List; + +import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; +import com.unit.daybook.domain.comment.entity.Comment; + public interface CommentRepositoryCustom { + List findCommentByBoard(Long boardId); } diff --git a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java index 277bdea..a7baf09 100644 --- a/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java +++ b/src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java @@ -1,16 +1,30 @@ package com.unit.daybook.domain.comment.repository; +import static com.unit.daybook.domain.board.entity.QBoard.*; +import static com.unit.daybook.domain.comment.entity.QComment.*; + +import java.util.List; + import org.springframework.stereotype.Repository; import com.querydsl.jpa.impl.JPAQueryFactory; +import com.unit.daybook.domain.comment.entity.Comment; import lombok.RequiredArgsConstructor; @Repository @RequiredArgsConstructor -public class CommentRepositoryImpl { +public class CommentRepositoryImpl implements CommentRepositoryCustom{ private final JPAQueryFactory jpaQueryFactory; - + @Override + public List findCommentByBoard(Long boardId) { + return jpaQueryFactory.selectFrom(comment) + .leftJoin(comment.board, board) + .fetchJoin() + .where(board.boardId.eq(boardId)) + .orderBy(comment.createdAt.desc()) + .fetch(); + } } diff --git a/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java b/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java index 6c78e71..e9391f4 100644 --- a/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java +++ b/src/main/java/com/unit/daybook/domain/reaction/entity/Reaction.java @@ -1,6 +1,7 @@ package com.unit.daybook.domain.reaction.entity; import com.unit.daybook.domain.board.entity.Board; +import com.unit.daybook.domain.common.model.BaseTimeEntity; import com.unit.daybook.domain.member.domain.Member; import jakarta.persistence.*; @@ -14,7 +15,7 @@ @Entity @Table(name = "reaction") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Reaction { +public class Reaction extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)