Skip to content

Commit

Permalink
Merge pull request #35 from T1F5/feature/#23
Browse files Browse the repository at this point in the history
feat: 댓글 기능 추가
  • Loading branch information
char-yb authored Apr 6, 2024
2 parents dd9cfaf + 6c4f701 commit f4ecc87
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 24 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
8 changes: 6 additions & 2 deletions src/main/java/com/unit/daybook/domain/board/entity/Board.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Hashtag> hashtags = new ArrayList<>();

@OneToMany(mappedBy = "board", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reaction> reactions = new ArrayList<>();

@OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> 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;
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,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<CommentCreateResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.unit.daybook.domain.comment.dto.request;

public record CommentCreateRequest (
String content,
Long boardId
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.unit.daybook.domain.comment.dto.request;

public record CommentModifyRequest(
String content
) {
}
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
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 CommentModifyResponse(
String content,
Long commentId
) {
public static CommentModifyResponse from(Comment comment) {
return new CommentModifyResponse(comment.getContent(), comment.getCommentId());
}
}
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());
}
}
73 changes: 57 additions & 16 deletions src/main/java/com/unit/daybook/domain/comment/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,63 @@
package com.unit.daybook.domain.comment.entity;

import jakarta.persistence.*;
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;
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 extends BaseTimeEntity {

@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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Comment, Long>, CommentRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +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
@@ -0,0 +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 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();
}
}
Loading

0 comments on commit f4ecc87

Please sign in to comment.