Skip to content

Commit 7882754

Browse files
authored
Merge pull request #72 from BackEndSchoolPlus3th/SMS-98-feat-community
Sms 98 feat community
2 parents d1d50fe + d93b8b0 commit 7882754

7 files changed

Lines changed: 60 additions & 25 deletions

File tree

src/main/java/org/com/stocknote/domain/comment/entity/Comment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Comment extends BaseEntity {
2222
private Post post;
2323

2424
@Setter
25-
@Column(nullable = false)
25+
@Column(nullable = false, length = 500) // 최대 길이 제한
2626
private String body;
2727

2828
@ManyToOne(fetch = FetchType.LAZY)

src/main/java/org/com/stocknote/domain/comment/repository/CommentRepository.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
import org.springframework.data.domain.Page;
66
import org.springframework.data.domain.Pageable;
77
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
9+
import org.springframework.data.repository.query.Param;
810
import org.springframework.stereotype.Repository;
911

1012
@Repository
1113
public interface CommentRepository extends JpaRepository<Comment, Long> {
12-
Page<Comment> findByPostId(Pageable pageable, Long postId);
14+
// Page<Comment> findByPostId(Pageable pageable, Long postId);
1315
Page<Comment> findByMember(Member member, Pageable pageable);
1416
boolean existsByPostIdAndMember(Long postId, Member member);
17+
@Query(value = "SELECT DISTINCT c FROM Comment c " +
18+
"JOIN FETCH c.member " +
19+
"WHERE c.post.id = :postId",
20+
countQuery = "SELECT COUNT(c) FROM Comment c WHERE c.post.id = :postId")
21+
Page<Comment> findByPostId(@Param("postId") Long postId, Pageable pageable);
1522
}

src/main/java/org/com/stocknote/domain/comment/service/CommentService.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ public CommentDetailResponse getCommentDetail(Long commentId) {
4141
}
4242

4343
public Page<CommentDetailResponse> getComments(Long postId, Pageable pageable) {
44-
Page<CommentDetailResponse> comments = commentRepository.findByPostId(pageable, postId)
45-
.map(comment -> {
46-
Member member = memberRepository.findById(comment.getMember().getId()).orElseThrow(() -> new IllegalArgumentException("user not found"));
47-
return new CommentDetailResponse(comment.getId(), comment.getBody(), comment.getCreatedAt(), member.getId(), member.getName(),member.getProfile());
48-
}
49-
);
50-
return comments;
44+
return commentRepository.findByPostId(postId, pageable)
45+
.map(comment -> new CommentDetailResponse(
46+
comment.getId(),
47+
comment.getBody(),
48+
comment.getCreatedAt(),
49+
comment.getMember().getId(), //이미 로딩된 member 사용
50+
comment.getMember().getName(),
51+
comment.getMember().getProfile()
52+
));
5153
}
5254

5355
@Transactional

src/main/java/org/com/stocknote/domain/post/controller/PostController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import lombok.RequiredArgsConstructor;
88
import org.com.stocknote.domain.member.entity.Member;
99
import org.com.stocknote.domain.notification.service.KeywordNotificationElasticService;
10-
import org.com.stocknote.domain.notification.service.KeywordNotificationService;
1110
import org.com.stocknote.domain.post.dto.PostModifyDto;
1211
import org.com.stocknote.domain.post.dto.PostResponseDto;
13-
import org.com.stocknote.domain.post.entity.Post;
1412
import org.com.stocknote.domain.post.dto.PostSearchConditionDto;
13+
import org.com.stocknote.domain.post.entity.Post;
1514
import org.com.stocknote.domain.post.entity.PostCategory;
1615
import org.com.stocknote.domain.post.service.PostService;
1716
import org.com.stocknote.domain.searchDoc.document.PostDoc;
@@ -34,7 +33,7 @@ public class PostController {
3433

3534
private final PostService postService;
3635
private final SearchDocService searchDocService;
37-
private final KeywordNotificationService keywordNotificationService;
36+
// private final KeywordNotificationService keywordNotificationService;
3837
private final KeywordNotificationElasticService keywordNotificationElasticService;
3938

4039
@Transactional

src/main/java/org/com/stocknote/domain/post/dto/PostResponseDto.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.time.LocalDateTime;
1010
import java.util.ArrayList;
11+
import java.util.Collections;
1112
import java.util.List;
1213

1314
public record PostResponseDto(
@@ -44,6 +45,15 @@ public static PostResponseDto fromPost(Post post, List<String> hashtags) {
4445
);
4546
}
4647

48+
// 지연 로딩용 메서드 추가
49+
public List<CommentDetailResponse> getComments() {
50+
if (comments == null) {
51+
// 실제 댓글 로딩 로직 (서비스 레이어에서 처리)
52+
return Collections.emptyList();
53+
}
54+
return comments;
55+
}
56+
4757
public static PostResponseDto fromPost(PostDoc postDoc) {
4858
return new PostResponseDto(
4959
Long.valueOf(postDoc.getId()),

src/main/java/org/com/stocknote/domain/post/entity/Post.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class Post extends BaseEntity {
4545

4646
private LocalDateTime deletedAt;
4747

48-
public void softDelete() {
49-
this.deletedAt = LocalDateTime.now();
50-
}
48+
// public void softDelete() {
49+
// this.deletedAt = LocalDateTime.now();
50+
// }
5151

5252
public void titleUpdate(String title) {
5353
this.title = title;

src/main/java/org/com/stocknote/domain/post/repository/PostRepository.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,28 @@
1414

1515
@Repository
1616
public interface PostRepository extends JpaRepository<Post, Long> {
17-
Page<Post> findByCategory(PostCategory category, Pageable pageable);
17+
@Query("SELECT DISTINCT p FROM Post p " +
18+
"LEFT JOIN FETCH p.member m " +
19+
"LEFT JOIN FETCH p.comments c " +
20+
"WHERE p.category = :category " +
21+
"AND p.deletedAt IS NULL " +
22+
"ORDER BY p.createdAt DESC")
1823

19-
Long id(Long id);
24+
Page<Post> findByCategory(
25+
@Param("category") PostCategory category,
26+
Pageable pageable
27+
);
2028

21-
Page<Post> findByMember(Member member, Pageable pageable);
29+
@Query("SELECT DISTINCT p FROM Post p " +
30+
"LEFT JOIN FETCH p.member m " +
31+
"LEFT JOIN FETCH p.comments c " +
32+
"WHERE p.member = :member " +
33+
"AND p.deletedAt IS NULL " +
34+
"ORDER BY p.createdAt DESC")
35+
Page<Post> findByMember(
36+
@Param("member") Member member,
37+
Pageable pageable
38+
);
2239

2340
//인기 순으로 정렬(댓글순 + 좋아요순 3일 이내)
2441
@Query("""
@@ -48,13 +65,13 @@ public interface PostRepository extends JpaRepository<Post, Long> {
4865
Page<Post> findPopularPosts(Pageable pageable);
4966

5067

51-
// 좋아요 순으로 정렬
52-
@Query("SELECT p FROM Post p WHERE p.deletedAt IS NULL ORDER BY size(p.likes) DESC")
53-
Page<Post> findPopularPostsByLikes(Pageable pageable);
54-
55-
// 댓글 순으로 정렬
56-
@Query("SELECT p FROM Post p WHERE p.deletedAt IS NULL ORDER BY size(p.comments) DESC")
57-
Page<Post> findPopularPostsByComments(Pageable pageable);
68+
// // 좋아요 순으로 정렬
69+
// @Query("SELECT p FROM Post p WHERE p.deletedAt IS NULL ORDER BY size(p.likes) DESC")
70+
// Page<Post> findPopularPostsByLikes(Pageable pageable);
71+
//
72+
// // 댓글 순으로 정렬
73+
// @Query("SELECT p FROM Post p WHERE p.deletedAt IS NULL ORDER BY size(p.comments) DESC")
74+
// Page<Post> findPopularPostsByComments(Pageable pageable);
5875

5976

6077
@Query("SELECT DISTINCT p FROM Post p " +

0 commit comments

Comments
 (0)