Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ dependencies {

tasks.withType<Test> {
useJUnitPlatform()
systemProperty("spring.profiles.active", "test")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.back.domain.board.comment.controller;

import com.back.domain.board.comment.controller.docs.CommentControllerDocs;
import com.back.domain.board.comment.dto.CommentListResponse;
import com.back.domain.board.comment.dto.CommentRequest;
import com.back.domain.board.comment.dto.CommentResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.back.domain.board.comment.controller;

import com.back.domain.board.comment.controller.docs.CommentLikeControllerDocs;
import com.back.domain.board.comment.dto.CommentLikeResponse;
import com.back.domain.board.comment.service.CommentLikeService;
import com.back.global.common.dto.RsData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.comment.controller;
package com.back.domain.board.comment.controller.docs;

import com.back.domain.board.comment.dto.CommentListResponse;
import com.back.domain.board.comment.dto.CommentRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.comment.controller;
package com.back.domain.board.comment.controller.docs;

import com.back.domain.board.comment.dto.CommentLikeResponse;
import com.back.global.common.dto.RsData;
Expand Down
45 changes: 31 additions & 14 deletions src/main/java/com/back/domain/board/comment/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,80 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Entity
@Getter
@NoArgsConstructor
public class Comment extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
@JoinColumn(name = "post_id", nullable = false)
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(nullable = false, columnDefinition = "TEXT")
private String content;

// TODO: 추후 CommentRepositoryImpl#getCommentsByPostId 로직 개선 필요, ERD에도 반영할 것
@Column(nullable = false)
private Long likeCount = 0L;

// 해당 댓글의 부모 댓글
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_comment_id")
private Comment parent;

// 해당 댓글에 달린 대댓글 목록
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> children = new ArrayList<>();

@OneToMany(mappedBy = "comment", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CommentLike> commentLikes = new ArrayList<>();

// -------------------- 생성자 --------------------
public Comment(Post post, User user, String content) {
this.post = post;
this.user = user;
this.content = content;
}

public Comment(Post post, User user, String content, Comment parent) {
this.post = post;
this.user = user;
this.content = content;
this.parent = parent;
post.addComment(this);
user.addComment(this);
}

// -------------------- 정적 팩토리 메서드 --------------------
/** 루트 댓글 생성 */
public static Comment createRoot(Post post, User user, String content) {
return new Comment(post, user, content, null);
}

/** 대댓글 생성 */
public static Comment createChild(Post post, User user, String content, Comment parent) {
Comment comment = new Comment(post, user, content, parent);
parent.getChildren().add(comment);
return comment;
}

// -------------------- 연관관계 편의 메서드 --------------------
public void addLike(CommentLike like) {
this.commentLikes.add(like);
}

public void removeLike(CommentLike like) {
this.commentLikes.remove(like);
}

// -------------------- 비즈니스 메서드 --------------------
// 댓글 업데이트
/** 댓글 내용 수정 */
public void update(String content) {
this.content = content;
}

// 좋아요 수 증가
/** 좋아요 수 증가 */
public void increaseLikeCount() {
this.likeCount++;
}

// 좋아요 수 감소
/** 좋아요 수 감소 */
public void decreaseLikeCount() {
if (this.likeCount > 0) {
this.likeCount--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@

import com.back.domain.user.entity.User;
import com.back.global.entity.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(
uniqueConstraints = @UniqueConstraint(columnNames = {"comment_id", "user_id"})
)
public class CommentLike extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "comment_id")
@JoinColumn(name = "comment_id", nullable = false)
private Comment comment;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JoinColumn(name = "user_id", nullable = false)
private User user;

// -------------------- 생성자 --------------------
public CommentLike(Comment comment, User user) {
this.comment = comment;
this.user = user;
comment.addLike(this);
user.addCommentLike(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.back.domain.board.comment.repository;

import com.back.domain.board.comment.entity.CommentLike;
import com.back.domain.board.comment.repository.custom.CommentLikeRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Repository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.back.domain.board.comment.repository;

import com.back.domain.board.comment.entity.Comment;
import com.back.domain.board.comment.repository.custom.CommentRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.comment.repository;
package com.back.domain.board.comment.repository.custom;

import java.util.Collection;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.comment.repository;
package com.back.domain.board.comment.repository.custom;

import com.back.domain.board.comment.entity.QCommentLike;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.comment.repository;
package com.back.domain.board.comment.repository.custom;

import com.back.domain.board.comment.dto.CommentListResponse;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.comment.repository;
package com.back.domain.board.comment.repository.custom;

import com.back.domain.board.comment.dto.CommentListResponse;
import com.back.domain.board.comment.dto.QCommentListResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public CommentLikeResponse cancelLikeComment(Long commentId, Long userId) {
CommentLike commentLike = commentLikeRepository.findByUserIdAndCommentId(userId, commentId)
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_LIKE_NOT_FOUND));

// 연관관계 제거
comment.removeLike(commentLike);
user.removeCommentLike(commentLike);

// CommentLike 삭제
commentLikeRepository.delete(commentLike);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ public class CommentService {
private final PostRepository postRepository;
private final ApplicationEventPublisher eventPublisher;

// TODO: 연관관계 고려, 메서드 명, 중복 코드 제거, 주석 통일
// TODO: comment 끝나면 post도 해야 함.. entity > DTO > Repo > Service > Controller > Docs 순으로..
/**
* 댓글 생성 서비스
* 1. User 조회
* 2. Post 조회
* 3. Comment 생성
* 4. Comment 저장 및 CommentResponse 반환
* 3. Comment 생성 및 저장
* 4. 댓글 작성 이벤트 발행
* 5. CommentResponse 반환
*/
public CommentResponse createComment(Long postId, CommentRequest request, Long userId) {
// User 조회
Expand All @@ -54,10 +57,11 @@ public CommentResponse createComment(Long postId, CommentRequest request, Long u
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));

// Comment 생성
Comment comment = new Comment(post, user, request.content());
// CommentCount 증가
post.increaseCommentCount();

// Comment 저장 및 응답 반환
// Comment 생성 및 저장
Comment comment = Comment.createRoot(post, user, request.content());
commentRepository.save(comment);

// 댓글 작성 이벤트 발행
Expand Down Expand Up @@ -164,6 +168,10 @@ public CommentResponse updateComment(Long postId, Long commentId, CommentRequest
* 4. Comment 삭제
*/
public void deleteComment(Long postId, Long commentId, Long userId) {
// User 조회
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

// Post 조회
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
Expand All @@ -177,6 +185,11 @@ public void deleteComment(Long postId, Long commentId, Long userId) {
throw new CustomException(ErrorCode.COMMENT_NO_PERMISSION);
}

// 연관관계 제거
post.removeComment(comment);
user.removeComment(comment);

// Comment 삭제
commentRepository.delete(comment);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.back.domain.board.post.controller;

import com.back.domain.board.post.controller.docs.PostBookmarkControllerDocs;
import com.back.domain.board.post.dto.PostBookmarkResponse;
import com.back.domain.board.post.service.PostBookmarkService;
import com.back.global.common.dto.RsData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.back.domain.board.post.controller;

import com.back.domain.board.post.controller.docs.PostCategoryControllerDocs;
import com.back.domain.board.post.dto.CategoryRequest;
import com.back.domain.board.post.dto.CategoryResponse;
import com.back.domain.board.post.service.PostCategoryService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.back.domain.board.post.controller;

import com.back.domain.board.common.dto.PageResponse;
import com.back.domain.board.post.controller.docs.PostControllerDocs;
import com.back.domain.board.post.dto.PostDetailResponse;
import com.back.domain.board.post.dto.PostListResponse;
import com.back.domain.board.post.dto.PostRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.back.domain.board.post.controller;

import com.back.domain.board.post.controller.docs.PostLikeControllerDocs;
import com.back.domain.board.post.dto.PostLikeResponse;
import com.back.domain.board.post.service.PostLikeService;
import com.back.global.common.dto.RsData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.post.controller;
package com.back.domain.board.post.controller.docs;

import com.back.domain.board.post.dto.PostBookmarkResponse;
import com.back.global.common.dto.RsData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.post.controller;
package com.back.domain.board.post.controller.docs;

import com.back.domain.board.post.dto.CategoryRequest;
import com.back.domain.board.post.dto.CategoryResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.post.controller;
package com.back.domain.board.post.controller.docs;

import com.back.domain.board.common.dto.PageResponse;
import com.back.domain.board.post.dto.PostDetailResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.back.domain.board.post.controller;
package com.back.domain.board.post.controller.docs;

import com.back.domain.board.post.dto.PostLikeResponse;
import com.back.global.common.dto.RsData;
Expand Down
Loading