Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b6037ab
refactor: community 도메인 제외 연관관계 삭제
lsy1307 Jul 1, 2025
df10149
fix: application, university 테스트 코드 오류 수정
lsy1307 Jul 1, 2025
32cb72f
refactor: 불필요한 siteUser 재조회 수정
lsy1307 Jul 1, 2025
3ca8f4e
fix: conflict해결
lsy1307 Jul 1, 2025
2938a79
refactor: 수정사항 반영
lsy1307 Jul 2, 2025
9c59985
fix: conflict 해결
lsy1307 Jul 2, 2025
541fed7
fix: conflict merge 과정에서 생긴 더미 파일 삭제
lsy1307 Jul 2, 2025
5cf0660
refactor: 불필요한 import문 삭제
lsy1307 Jul 2, 2025
b739ceb
refactor: Community 연관관계 매핑 수정/삭제
lsy1307 Jul 2, 2025
805ae3d
refactor: 사용하지 않는 import문 삭제
lsy1307 Jul 2, 2025
41afcad
fix: likedPostCount 테스트 코드 주석처리 및 todo 추가
lsy1307 Jul 4, 2025
f24495e
fix: 서브모듈 참조 오류 수정
lsy1307 Jul 8, 2025
2bb6e95
refactor: 코드리뷰 수정사항 반영
lsy1307 Jul 8, 2025
d3c9b0e
refactor: 코드리뷰 수정사항 반영
lsy1307 Jul 9, 2025
8257dfc
Merge branch 'refactor/358-refactor-delete-relationship' of https://g…
lsy1307 Jul 9, 2025
cfa18eb
refactor: Parameter 명칭 변경
lsy1307 Jul 9, 2025
13a4673
Merge branch 'refactor/358-refactor-delete-relationship' of https://g…
lsy1307 Jul 9, 2025
68f4bbd
refactor: 코드리뷰 수정사항 반영
lsy1307 Jul 9, 2025
af4ec96
refactor: 코드리뷰 수정사항 반영
lsy1307 Jul 9, 2025
6fcc903
fix: SiteUser 결함 수정
lsy1307 Jul 9, 2025
5f6bcaf
fix: 직전 커밋 revert
lsy1307 Jul 10, 2025
786e0ae
fix: conflict 해결
lsy1307 Jul 10, 2025
5b58a39
refactor: 사용하지 않는 import문 제거
lsy1307 Jul 10, 2025
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package com.example.solidconnection.community.board.domain;

import com.example.solidconnection.community.post.domain.Post;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

@Entity
@Getter
@NoArgsConstructor
Expand All @@ -24,9 +18,6 @@ public class Board {
@Column(nullable = false, length = 20)
private String koreanName;

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

public Board(String code, String koreanName) {
this.code = code;
this.koreanName = koreanName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.common.exception.ErrorCode;
import com.example.solidconnection.community.board.domain.Board;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

Expand All @@ -13,7 +12,6 @@

public interface BoardRepository extends JpaRepository<Board, String> {

@EntityGraph(attributePaths = {"postList"})
Optional<Board> findBoardByCode(@Param("code") String code);

default Board getByCodeUsingEntityGraph(String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.solidconnection.common.BaseEntity;
import com.example.solidconnection.community.post.domain.Post;
import com.example.solidconnection.siteuser.domain.SiteUser;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -45,9 +44,8 @@ public class Comment extends BaseEntity {
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "site_user_id")
private SiteUser siteUser;
@Column
private long siteUserId;
Comment on lines +47 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import com.example.solidconnection.siteuser.domain.SiteUser;

import 제거!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정하였습니다!


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
Expand All @@ -60,7 +58,7 @@ public Comment(String content) {
this.content = content;
}

public void setParentCommentAndPostAndSiteUser(Comment parentComment, Post post, SiteUser siteUser) {
public void setParentCommentAndPostAndSiteUserId(Comment parentComment, Post post, long siteUserId) {

if (this.parentComment != null) {
this.parentComment.getCommentList().remove(this);
Expand All @@ -74,37 +72,25 @@ public void setParentCommentAndPostAndSiteUser(Comment parentComment, Post post,
this.post = post;
post.getCommentList().add(this);

if (this.siteUser != null) {
this.siteUser.getCommentList().remove(this);
}
this.siteUser = siteUser;
siteUser.getCommentList().add(this);
this.siteUserId = siteUserId;
}

public void setPostAndSiteUser(Post post, SiteUser siteUser) {
public void setPostAndSiteUserId(Post post, long siteUserId) {

if (this.post != null) {
this.post.getCommentList().remove(this);
}
this.post = post;
post.getCommentList().add(this);

if (this.siteUser != null) {
this.siteUser.getCommentList().remove(this);
}
this.siteUser = siteUser;
siteUser.getCommentList().add(this);
this.siteUserId = siteUserId;
}

public void resetPostAndSiteUserAndParentComment() {
public void resetPostAndParentComment() {
if (this.post != null) {
this.post.getCommentList().remove(this);
this.post = null;
}
if (this.siteUser != null) {
this.siteUser.getCommentList().remove(this);
this.siteUser = null;
}
if (this.parentComment != null) {
this.parentComment.getCommentList().remove(this);
this.parentComment = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public Comment toEntity(SiteUser siteUser, Post post, Comment parentComment) {
);

if (parentComment == null) {
comment.setPostAndSiteUser(post, siteUser);
comment.setPostAndSiteUserId(post, siteUser.getId());
} else {
comment.setParentCommentAndPostAndSiteUser(parentComment, post, siteUser);
comment.setParentCommentAndPostAndSiteUserId(parentComment, post, siteUser.getId());
}
return comment;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.solidconnection.community.comment.dto;

import com.example.solidconnection.community.comment.domain.Comment;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.dto.PostFindSiteUserResponse;

import java.time.ZonedDateTime;
Expand All @@ -15,15 +16,15 @@ public record PostFindCommentResponse(
PostFindSiteUserResponse postFindSiteUserResponse
) {

public static PostFindCommentResponse from(Boolean isOwner, Comment comment) {
public static PostFindCommentResponse from(Boolean isOwner, Comment comment, SiteUser siteUser) {
return new PostFindCommentResponse(
comment.getId(),
getParentCommentId(comment),
comment.getContent(),
isOwner,
comment.getCreatedAt(),
comment.getUpdatedAt(),
PostFindSiteUserResponse.from(comment.getSiteUser())
PostFindSiteUserResponse.from(siteUser)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_UPDATE_DEPRECATED_COMMENT;
Expand All @@ -35,14 +36,16 @@ public class CommentService {

@Transactional(readOnly = true)
public List<PostFindCommentResponse> findCommentsByPostId(SiteUser siteUser, Long postId) {
SiteUser commentOwner = siteUserRepository.findById(siteUser.getId())
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
return commentRepository.findCommentTreeByPostId(postId)
.stream()
.map(comment -> PostFindCommentResponse.from(isOwner(comment, siteUser), comment))
.map(comment -> PostFindCommentResponse.from(isOwner(comment, siteUser), comment, siteUser))
.collect(Collectors.toList());
}

private Boolean isOwner(Comment comment, SiteUser siteUser) {
return comment.getSiteUser().getId().equals(siteUser.getId());
return Objects.equals(comment.getSiteUserId(), siteUser.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSiteUserId가 long으로 바뀌어서 Objects.equals를 쓰신거군요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 맞습니다!

}

@Transactional
Expand All @@ -54,14 +57,7 @@ public CommentCreateResponse createComment(SiteUser siteUser, CommentCreateReque
parentComment = commentRepository.getById(commentCreateRequest.parentId());
validateCommentDepth(parentComment);
}

/*
* todo: siteUser를 영속 상태로 만들 수 있도록 컨트롤러에서 siteUserId 를 넘겨줄 것인지,
* siteUser 에 postList 를 FetchType.EAGER 로 설정할 것인지,
* post 와 siteUser 사이의 양방향을 끊을 것인지 생각해봐야한다.
*/
SiteUser siteUser1 = siteUserRepository.findById(siteUser.getId()).orElseThrow(() -> new CustomException(USER_NOT_FOUND));
Comment comment = commentCreateRequest.toEntity(siteUser1, post, parentComment);
Comment comment = commentCreateRequest.toEntity(siteUser, post, parentComment);
Comment createdComment = commentRepository.save(comment);

return CommentCreateResponse.from(createdComment);
Expand Down Expand Up @@ -100,18 +96,18 @@ public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId
// 대댓글인 경우
Comment parentComment = comment.getParentComment();
// 대댓글을 삭제합니다.
comment.resetPostAndSiteUserAndParentComment();
comment.resetPostAndParentComment();
commentRepository.deleteById(commentId);
// 대댓글 삭제 이후, 부모댓글이 무의미하다면 이역시 삭제합니다.
if (parentComment.getCommentList().isEmpty() && parentComment.getContent() == null) {
parentComment.resetPostAndSiteUserAndParentComment();
parentComment.resetPostAndParentComment();
commentRepository.deleteById(parentComment.getId());
}
} else {
// 댓글인 경우
if (comment.getCommentList().isEmpty()) {
// 대댓글이 없는 경우
comment.resetPostAndSiteUserAndParentComment();
comment.resetPostAndParentComment();
commentRepository.deleteById(commentId);
} else {
// 대댓글이 있는 경우
Expand All @@ -122,7 +118,7 @@ public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId
}

private void validateOwnership(Comment comment, SiteUser siteUser) {
if (!comment.getSiteUser().getId().equals(siteUser.getId())) {
if (!Objects.equals(comment.getSiteUserId(), siteUser.getId())) {
throw new CustomException(INVALID_POST_ACCESS);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.example.solidconnection.community.post.domain;

import com.example.solidconnection.common.BaseEntity;
import com.example.solidconnection.community.board.domain.Board;
import com.example.solidconnection.community.comment.domain.Comment;
import com.example.solidconnection.community.post.dto.PostUpdateRequest;
import com.example.solidconnection.siteuser.domain.SiteUser;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
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.OneToMany;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -50,13 +45,12 @@ public class Post extends BaseEntity {
@Enumerated(EnumType.STRING)
private PostCategory category;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_code")
private Board board;
@Column
private String boardCode;

@Column
private long siteUserId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "site_user_id")
private SiteUser siteUser;

@BatchSize(size = 20)
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
Expand All @@ -78,29 +72,9 @@ public Post(String title, String content, Boolean isQuestion, Long likeCount, Lo
this.category = category;
}

public void setBoardAndSiteUser(Board board, SiteUser siteUser) {
if (this.board != null) {
this.board.getPostList().remove(this);
}
this.board = board;
board.getPostList().add(this);

if (this.siteUser != null) {
this.siteUser.getPostList().remove(this);
}
this.siteUser = siteUser;
siteUser.getPostList().add(this);
}

public void resetBoardAndSiteUser() {
if (this.board != null) {
this.board.getPostList().remove(this);
this.board = null;
}
if (this.siteUser != null) {
this.siteUser.getPostList().remove(this);
this.siteUser = null;
}
public void setBoardAndSiteUserId(String boardCode, long siteUserId) {
this.boardCode = boardCode;
this.siteUserId = siteUserId;
}

public void update(PostUpdateRequest postUpdateRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.solidconnection.community.post.domain;

import com.example.solidconnection.siteuser.domain.SiteUser;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -26,33 +25,21 @@ public class PostLike {
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "site_user_id")
private SiteUser siteUser;
private long siteUserId;

public void setPostAndSiteUser(Post post, SiteUser siteUser) {
public void setPostAndSiteUserId(Post post, long siteUserId) {
if (this.post != null) {
this.post.getPostLikeList().remove(this);
}
this.post = post;
post.getPostLikeList().add(this);

if (this.siteUser != null) {
this.siteUser.getPostLikeList().remove(this);
}
this.siteUser = siteUser;
siteUser.getPostLikeList().add(this);
this.siteUserId = siteUserId;
}

public void resetPostAndSiteUser() {
public void resetPost() {
if (this.post != null) {
this.post.getPostLikeList().remove(this);
}
this.post = null;

if (this.siteUser != null) {
this.siteUser.getPostLikeList().remove(this);
}
this.siteUser = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Post toEntity(SiteUser siteUser, Board board) {
0L,
PostCategory.valueOf(this.postCategory)
);
post.setBoardAndSiteUser(board, siteUser);
post.setBoardAndSiteUserId(board.getCode(), siteUser.getId());
return post;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.community.post.domain.Post;
import com.example.solidconnection.community.post.domain.PostLike;
import com.example.solidconnection.siteuser.domain.SiteUser;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
Expand All @@ -12,10 +11,10 @@

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

Optional<PostLike> findPostLikeByPostAndSiteUser(Post post, SiteUser siteUser);
Optional<PostLike> findPostLikeByPostAndSiteUserId(Post post, long siteUserId);

default PostLike getByPostAndSiteUser(Post post, SiteUser siteUser) {
return findPostLikeByPostAndSiteUser(post, siteUser)
default PostLike getByPostAndSiteUserId(Post post, long siteUserId) {
return findPostLikeByPostAndSiteUserId(post, siteUserId)
.orElseThrow(() -> new CustomException(INVALID_POST_LIKE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

import static com.example.solidconnection.common.exception.ErrorCode.INVALID_POST_ID;

public interface PostRepository extends JpaRepository<Post, Long> {

@EntityGraph(attributePaths = {"postImageList", "board", "siteUser"})
List<Post> findByBoardCode(String boardCode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개행!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정하였습니다!


@EntityGraph(attributePaths = {"postImageList"})
Optional<Post> findPostById(Long id);

@Modifying(clearAutomatically = true, flushAutomatically = true)
Expand Down
Loading
Loading