Skip to content

Commit cfe8489

Browse files
authored
refactor: Community 연관관계 수정/삭제 (#372)
* refactor: community 도메인 제외 연관관계 삭제 - Application, Country, GpaScore, InterestedCountry/Region, LanguageTestScore, LikedUniversity. SiteUser, UnivApplyInfo 도메인 수정 - 연관관계 삭제 및 생성자 parameter 수정으로 인한 쿼리 수정 다수 - test코드 수정 * fix: application, university 테스트 코드 오류 수정 - 연관관계 변경에 따른 transaction 문제 해결 - 쿼리 일부 수정, 테스트 코드 일부 수정 - GeneralUniversityRecommendService 로직 수정 * refactor: 불필요한 siteUser 재조회 수정 - ScoreService에서 siteUser정보를 다시 불러오는 로직 삭제 * refactor: 수정사항 반영 - 컨벤션, 타입수정 다수 * fix: conflict merge 과정에서 생긴 더미 파일 삭제 * refactor: 불필요한 import문 삭제 * refactor: Community 연관관계 매핑 수정/삭제 - Board, Comment, Post, PostLike 연관관계 수정/삭제 - 연관관계 수정에 따른 Repository및 Service 레이어 수정 - 연관관계 수정에 따른 테스트 코드 수정 * refactor: 사용하지 않는 import문 삭제 * fix: likedPostCount 테스트 코드 주석처리 및 todo 추가 * fix: 서브모듈 참조 오류 수정 * refactor: 코드리뷰 수정사항 반영 - Long타입 long으로 수정 - collect -> toList단독으로 변경 - 컨벤션 수정 - Recomment 변경사항 원복 - getGeneralRecommendsExcludingSelected함수 OutOfRange 방지 추가 * refactor: 코드리뷰 수정사항 반영 - UnivApplyInfo University fetchType LAZY로 변경 - 컨벤션 수정 - findRandomByTerm함수 nativeQuery제거 및 Pageable로 LIMIT 구현 * refactor: Parameter 명칭 변경 - siteUser -> siteUserId로 변경 * refactor: 코드리뷰 수정사항 반영 - 메서드명 각자 기능에 맞게 수정 * refactor: 코드리뷰 수정사항 반영 - 사용하지 않는 import문 제거 - 코드 컨벤션 수정 - postLikeList에서 의미없는 BatchSize 삭제 * fix: SiteUser 결함 수정 - password nullable true -> false로 수정 * fix: 직전 커밋 revert - password nullable 변경사항 취소 This reverts commit 6fcc903. * refactor: 사용하지 않는 import문 제거
1 parent bfa7ba7 commit cfe8489

File tree

24 files changed

+83
-173
lines changed

24 files changed

+83
-173
lines changed

src/main/java/com/example/solidconnection/community/board/domain/Board.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package com.example.solidconnection.community.board.domain;
22

3-
import com.example.solidconnection.community.post.domain.Post;
4-
import jakarta.persistence.CascadeType;
53
import jakarta.persistence.Column;
64
import jakarta.persistence.Entity;
75
import jakarta.persistence.Id;
8-
import jakarta.persistence.OneToMany;
96
import lombok.Getter;
107
import lombok.NoArgsConstructor;
118

12-
import java.util.ArrayList;
13-
import java.util.List;
14-
159
@Entity
1610
@Getter
1711
@NoArgsConstructor
@@ -24,9 +18,6 @@ public class Board {
2418
@Column(nullable = false, length = 20)
2519
private String koreanName;
2620

27-
@OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true)
28-
private List<Post> postList = new ArrayList<>();
29-
3021
public Board(String code, String koreanName) {
3122
this.code = code;
3223
this.koreanName = koreanName;

src/main/java/com/example/solidconnection/community/board/repository/BoardRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.example.solidconnection.common.exception.CustomException;
44
import com.example.solidconnection.common.exception.ErrorCode;
55
import com.example.solidconnection.community.board.domain.Board;
6-
import org.springframework.data.jpa.repository.EntityGraph;
76
import org.springframework.data.jpa.repository.JpaRepository;
87
import org.springframework.data.repository.query.Param;
98

@@ -13,7 +12,6 @@
1312

1413
public interface BoardRepository extends JpaRepository<Board, String> {
1514

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

1917
default Board getByCodeUsingEntityGraph(String code) {

src/main/java/com/example/solidconnection/community/comment/domain/Comment.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.example.solidconnection.common.BaseEntity;
44
import com.example.solidconnection.community.post.domain.Post;
5-
import com.example.solidconnection.siteuser.domain.SiteUser;
65
import jakarta.persistence.CascadeType;
76
import jakarta.persistence.Column;
87
import jakarta.persistence.Entity;
@@ -45,9 +44,8 @@ public class Comment extends BaseEntity {
4544
@JoinColumn(name = "post_id")
4645
private Post post;
4746

48-
@ManyToOne(fetch = FetchType.LAZY)
49-
@JoinColumn(name = "site_user_id")
50-
private SiteUser siteUser;
47+
@Column
48+
private long siteUserId;
5149

5250
@ManyToOne(fetch = FetchType.LAZY)
5351
@JoinColumn(name = "parent_id")
@@ -60,7 +58,7 @@ public Comment(String content) {
6058
this.content = content;
6159
}
6260

63-
public void setParentCommentAndPostAndSiteUser(Comment parentComment, Post post, SiteUser siteUser) {
61+
public void setParentCommentAndPostAndSiteUserId(Comment parentComment, Post post, long siteUserId) {
6462

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

77-
if (this.siteUser != null) {
78-
this.siteUser.getCommentList().remove(this);
79-
}
80-
this.siteUser = siteUser;
81-
siteUser.getCommentList().add(this);
75+
this.siteUserId = siteUserId;
8276
}
8377

84-
public void setPostAndSiteUser(Post post, SiteUser siteUser) {
78+
public void setPostAndSiteUserId(Post post, long siteUserId) {
8579

8680
if (this.post != null) {
8781
this.post.getCommentList().remove(this);
8882
}
8983
this.post = post;
9084
post.getCommentList().add(this);
9185

92-
if (this.siteUser != null) {
93-
this.siteUser.getCommentList().remove(this);
94-
}
95-
this.siteUser = siteUser;
96-
siteUser.getCommentList().add(this);
86+
this.siteUserId = siteUserId;
9787
}
9888

99-
public void resetPostAndSiteUserAndParentComment() {
89+
public void resetPostAndParentComment() {
10090
if (this.post != null) {
10191
this.post.getCommentList().remove(this);
10292
this.post = null;
10393
}
104-
if (this.siteUser != null) {
105-
this.siteUser.getCommentList().remove(this);
106-
this.siteUser = null;
107-
}
10894
if (this.parentComment != null) {
10995
this.parentComment.getCommentList().remove(this);
11096
this.parentComment = null;

src/main/java/com/example/solidconnection/community/comment/dto/CommentCreateRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public Comment toEntity(SiteUser siteUser, Post post, Comment parentComment) {
2424
);
2525

2626
if (parentComment == null) {
27-
comment.setPostAndSiteUser(post, siteUser);
27+
comment.setPostAndSiteUserId(post, siteUser.getId());
2828
} else {
29-
comment.setParentCommentAndPostAndSiteUser(parentComment, post, siteUser);
29+
comment.setParentCommentAndPostAndSiteUserId(parentComment, post, siteUser.getId());
3030
}
3131
return comment;
3232
}

src/main/java/com/example/solidconnection/community/comment/dto/PostFindCommentResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.solidconnection.community.comment.dto;
22

33
import com.example.solidconnection.community.comment.domain.Comment;
4+
import com.example.solidconnection.siteuser.domain.SiteUser;
45
import com.example.solidconnection.siteuser.dto.PostFindSiteUserResponse;
56

67
import java.time.ZonedDateTime;
@@ -15,15 +16,15 @@ public record PostFindCommentResponse(
1516
PostFindSiteUserResponse postFindSiteUserResponse
1617
) {
1718

18-
public static PostFindCommentResponse from(Boolean isOwner, Comment comment) {
19+
public static PostFindCommentResponse from(Boolean isOwner, Comment comment, SiteUser siteUser) {
1920
return new PostFindCommentResponse(
2021
comment.getId(),
2122
getParentCommentId(comment),
2223
comment.getContent(),
2324
isOwner,
2425
comment.getCreatedAt(),
2526
comment.getUpdatedAt(),
26-
PostFindSiteUserResponse.from(comment.getSiteUser())
27+
PostFindSiteUserResponse.from(siteUser)
2728
);
2829
}
2930

src/main/java/com/example/solidconnection/community/comment/service/CommentService.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.transaction.annotation.Transactional;
1919

2020
import java.util.List;
21+
import java.util.Objects;
2122
import java.util.stream.Collectors;
2223

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

3637
@Transactional(readOnly = true)
3738
public List<PostFindCommentResponse> findCommentsByPostId(SiteUser siteUser, Long postId) {
39+
SiteUser commentOwner = siteUserRepository.findById(siteUser.getId())
40+
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
3841
return commentRepository.findCommentTreeByPostId(postId)
3942
.stream()
40-
.map(comment -> PostFindCommentResponse.from(isOwner(comment, siteUser), comment))
43+
.map(comment -> PostFindCommentResponse.from(isOwner(comment, siteUser), comment, siteUser))
4144
.collect(Collectors.toList());
4245
}
4346

4447
private Boolean isOwner(Comment comment, SiteUser siteUser) {
45-
return comment.getSiteUser().getId().equals(siteUser.getId());
48+
return Objects.equals(comment.getSiteUserId(), siteUser.getId());
4649
}
4750

4851
@Transactional
@@ -54,14 +57,7 @@ public CommentCreateResponse createComment(SiteUser siteUser, CommentCreateReque
5457
parentComment = commentRepository.getById(commentCreateRequest.parentId());
5558
validateCommentDepth(parentComment);
5659
}
57-
58-
/*
59-
* todo: siteUser를 영속 상태로 만들 수 있도록 컨트롤러에서 siteUserId 를 넘겨줄 것인지,
60-
* siteUser 에 postList 를 FetchType.EAGER 로 설정할 것인지,
61-
* post 와 siteUser 사이의 양방향을 끊을 것인지 생각해봐야한다.
62-
*/
63-
SiteUser siteUser1 = siteUserRepository.findById(siteUser.getId()).orElseThrow(() -> new CustomException(USER_NOT_FOUND));
64-
Comment comment = commentCreateRequest.toEntity(siteUser1, post, parentComment);
60+
Comment comment = commentCreateRequest.toEntity(siteUser, post, parentComment);
6561
Comment createdComment = commentRepository.save(comment);
6662

6763
return CommentCreateResponse.from(createdComment);
@@ -100,18 +96,18 @@ public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId
10096
// 대댓글인 경우
10197
Comment parentComment = comment.getParentComment();
10298
// 대댓글을 삭제합니다.
103-
comment.resetPostAndSiteUserAndParentComment();
99+
comment.resetPostAndParentComment();
104100
commentRepository.deleteById(commentId);
105101
// 대댓글 삭제 이후, 부모댓글이 무의미하다면 이역시 삭제합니다.
106102
if (parentComment.getCommentList().isEmpty() && parentComment.getContent() == null) {
107-
parentComment.resetPostAndSiteUserAndParentComment();
103+
parentComment.resetPostAndParentComment();
108104
commentRepository.deleteById(parentComment.getId());
109105
}
110106
} else {
111107
// 댓글인 경우
112108
if (comment.getCommentList().isEmpty()) {
113109
// 대댓글이 없는 경우
114-
comment.resetPostAndSiteUserAndParentComment();
110+
comment.resetPostAndParentComment();
115111
commentRepository.deleteById(commentId);
116112
} else {
117113
// 대댓글이 있는 경우
@@ -122,7 +118,7 @@ public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId
122118
}
123119

124120
private void validateOwnership(Comment comment, SiteUser siteUser) {
125-
if (!comment.getSiteUser().getId().equals(siteUser.getId())) {
121+
if (!Objects.equals(comment.getSiteUserId(), siteUser.getId())) {
126122
throw new CustomException(INVALID_POST_ACCESS);
127123
}
128124
}

src/main/java/com/example/solidconnection/community/post/domain/Post.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package com.example.solidconnection.community.post.domain;
22

33
import com.example.solidconnection.common.BaseEntity;
4-
import com.example.solidconnection.community.board.domain.Board;
54
import com.example.solidconnection.community.comment.domain.Comment;
65
import com.example.solidconnection.community.post.dto.PostUpdateRequest;
7-
import com.example.solidconnection.siteuser.domain.SiteUser;
86
import jakarta.persistence.CascadeType;
97
import jakarta.persistence.Column;
108
import jakarta.persistence.Entity;
119
import jakarta.persistence.EnumType;
1210
import jakarta.persistence.Enumerated;
13-
import jakarta.persistence.FetchType;
1411
import jakarta.persistence.GeneratedValue;
1512
import jakarta.persistence.GenerationType;
1613
import jakarta.persistence.Id;
17-
import jakarta.persistence.JoinColumn;
18-
import jakarta.persistence.ManyToOne;
1914
import jakarta.persistence.OneToMany;
2015
import lombok.EqualsAndHashCode;
2116
import lombok.Getter;
@@ -50,13 +45,12 @@ public class Post extends BaseEntity {
5045
@Enumerated(EnumType.STRING)
5146
private PostCategory category;
5247

53-
@ManyToOne(fetch = FetchType.LAZY)
54-
@JoinColumn(name = "board_code")
55-
private Board board;
48+
@Column
49+
private String boardCode;
50+
51+
@Column
52+
private long siteUserId;
5653

57-
@ManyToOne(fetch = FetchType.LAZY)
58-
@JoinColumn(name = "site_user_id")
59-
private SiteUser siteUser;
6054

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

81-
public void setBoardAndSiteUser(Board board, SiteUser siteUser) {
82-
if (this.board != null) {
83-
this.board.getPostList().remove(this);
84-
}
85-
this.board = board;
86-
board.getPostList().add(this);
87-
88-
if (this.siteUser != null) {
89-
this.siteUser.getPostList().remove(this);
90-
}
91-
this.siteUser = siteUser;
92-
siteUser.getPostList().add(this);
93-
}
94-
95-
public void resetBoardAndSiteUser() {
96-
if (this.board != null) {
97-
this.board.getPostList().remove(this);
98-
this.board = null;
99-
}
100-
if (this.siteUser != null) {
101-
this.siteUser.getPostList().remove(this);
102-
this.siteUser = null;
103-
}
75+
public void setBoardAndSiteUserId(String boardCode, long siteUserId) {
76+
this.boardCode = boardCode;
77+
this.siteUserId = siteUserId;
10478
}
10579

10680
public void update(PostUpdateRequest postUpdateRequest) {

src/main/java/com/example/solidconnection/community/post/domain/PostLike.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.solidconnection.community.post.domain;
22

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

29-
@ManyToOne(fetch = FetchType.LAZY)
30-
@JoinColumn(name = "site_user_id")
31-
private SiteUser siteUser;
28+
private long siteUserId;
3229

33-
public void setPostAndSiteUser(Post post, SiteUser siteUser) {
30+
public void setPostAndSiteUserId(Post post, long siteUserId) {
3431
if (this.post != null) {
3532
this.post.getPostLikeList().remove(this);
3633
}
3734
this.post = post;
3835
post.getPostLikeList().add(this);
39-
40-
if (this.siteUser != null) {
41-
this.siteUser.getPostLikeList().remove(this);
42-
}
43-
this.siteUser = siteUser;
44-
siteUser.getPostLikeList().add(this);
36+
this.siteUserId = siteUserId;
4537
}
4638

47-
public void resetPostAndSiteUser() {
39+
public void resetPost() {
4840
if (this.post != null) {
4941
this.post.getPostLikeList().remove(this);
5042
}
5143
this.post = null;
52-
53-
if (this.siteUser != null) {
54-
this.siteUser.getPostLikeList().remove(this);
55-
}
56-
this.siteUser = null;
5744
}
5845
}

src/main/java/com/example/solidconnection/community/post/dto/PostCreateRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Post toEntity(SiteUser siteUser, Board board) {
3636
0L,
3737
PostCategory.valueOf(this.postCategory)
3838
);
39-
post.setBoardAndSiteUser(board, siteUser);
39+
post.setBoardAndSiteUserId(board.getCode(), siteUser.getId());
4040
return post;
4141
}
4242
}

src/main/java/com/example/solidconnection/community/post/repository/PostLikeRepository.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.example.solidconnection.common.exception.CustomException;
44
import com.example.solidconnection.community.post.domain.Post;
55
import com.example.solidconnection.community.post.domain.PostLike;
6-
import com.example.solidconnection.siteuser.domain.SiteUser;
76
import org.springframework.data.jpa.repository.JpaRepository;
87

98
import java.util.Optional;
@@ -12,10 +11,10 @@
1211

1312
public interface PostLikeRepository extends JpaRepository<PostLike, Long> {
1413

15-
Optional<PostLike> findPostLikeByPostAndSiteUser(Post post, SiteUser siteUser);
14+
Optional<PostLike> findPostLikeByPostAndSiteUserId(Post post, long siteUserId);
1615

17-
default PostLike getByPostAndSiteUser(Post post, SiteUser siteUser) {
18-
return findPostLikeByPostAndSiteUser(post, siteUser)
16+
default PostLike getByPostAndSiteUserId(Post post, long siteUserId) {
17+
return findPostLikeByPostAndSiteUserId(post, siteUserId)
1918
.orElseThrow(() -> new CustomException(INVALID_POST_LIKE));
2019
}
2120
}

0 commit comments

Comments
 (0)