Skip to content

Commit

Permalink
feat: 일지 comments 프로퍼티 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
char-yb committed Apr 6, 2024
1 parent e18875c commit 6c4f701
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,9 +20,10 @@ public record FindOneBoardResponse(
List<String> hashtags,
String paperType,
List<ReactionTypeAndCount> reactions,
List<FindOneCommentResponse> comments,
LocalDateTime createdAt
) {
public static FindOneBoardResponse of(Board board, List<ReactionTypeAndCount> reactions) {
public static FindOneBoardResponse of(Board board, List<ReactionTypeAndCount> reactions, List<FindOneCommentResponse> comments) {
List<String> hashContents = board.getHashtags().stream().map(Hashtag::getContent).toList();
Long heartsFromReactions = reactions.stream().mapToLong(ReactionTypeAndCount::count).sum();
return new FindOneBoardResponse(
Expand All @@ -34,6 +36,7 @@ public static FindOneBoardResponse of(Board board, List<ReactionTypeAndCount> re
hashContents,
board.getPaperType(),
reactions,
comments,
board.getCreatedAt()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -60,8 +64,11 @@ public FindOneBoardResponse getBoard(Long boardId) {
Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new CustomException(ErrorCode.BOARD_NOT_FOUND));
List<ReactionTypeAndCount> reactions = reactionRepository.findAllByBoardGroupByReactionType(board);

return FindOneBoardResponse.of(board, reactions);
List<FindOneCommentResponse> comments = commentRepository.findCommentByBoard(boardId)
.stream()
.map(FindOneCommentResponse::from)
.toList();
return FindOneBoardResponse.of(board, reactions, comments);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Comment> findCommentByBoard(Long boardId);
}
Original file line number Diff line number Diff line change
@@ -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<Comment> findCommentByBoard(Long boardId) {
return jpaQueryFactory.selectFrom(comment)
.leftJoin(comment.board, board)
.fetchJoin()
.where(board.boardId.eq(boardId))
.orderBy(comment.createdAt.desc())
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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)
Expand Down

0 comments on commit 6c4f701

Please sign in to comment.