diff --git a/src/main/java/itstime/reflog/community/domain/Community.java b/src/main/java/itstime/reflog/community/domain/Community.java index 1c23034..a1f2cee 100644 --- a/src/main/java/itstime/reflog/community/domain/Community.java +++ b/src/main/java/itstime/reflog/community/domain/Community.java @@ -26,6 +26,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.BatchSize; @Entity @Getter @@ -42,12 +43,12 @@ public class Community { private String content; // 게시글 내용 - @ElementCollection(fetch = FetchType.EAGER) + @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "community_post_types", joinColumns = @JoinColumn(name = "community_id")) @Column(name = "post_type") private List postTypes; // 글 유형 (최대 2개) - @ElementCollection(fetch = FetchType.EAGER) + @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "community_learning_types", joinColumns = @JoinColumn(name = "community_id")) @Column(name = "learning_type") private List learningTypes; // 학습 유형 (최대 2개) diff --git a/src/main/java/itstime/reflog/community/dto/CommunityDto.java b/src/main/java/itstime/reflog/community/dto/CommunityDto.java index 2b806b5..4b89f4c 100644 --- a/src/main/java/itstime/reflog/community/dto/CommunityDto.java +++ b/src/main/java/itstime/reflog/community/dto/CommunityDto.java @@ -9,7 +9,9 @@ import itstime.reflog.postlike.domain.enums.PostType; import itstime.reflog.retrospect.domain.Retrospect; import lombok.*; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class CommunityDto { @Getter @@ -49,13 +51,15 @@ public static class CombinedCategoryResponse { private Long postId; private PostType postType; - public static CombinedCategoryResponse fromCommunity(Community community, String writer, Boolean isLike, Integer totalLike, Long totalComment) { + public static CombinedCategoryResponse fromCommunity(Community community, List postTypes,List learningTypes, String writer, Boolean isLike, Integer totalLike, Long totalComment) { + + return CombinedCategoryResponse.builder() .title(community.getTitle()) .content(community.getContent()) .createdDate(community.getCreatedAt()) - .postTypes(community.getPostTypes()) - .learningTypes(community.getLearningTypes()) + .postTypes(postTypes) + .learningTypes(learningTypes) .isLike(isLike) .totalComment(totalComment) .totalLike(totalLike) diff --git a/src/main/java/itstime/reflog/community/repository/CommunityRepository.java b/src/main/java/itstime/reflog/community/repository/CommunityRepository.java index 6e0464f..53935f1 100644 --- a/src/main/java/itstime/reflog/community/repository/CommunityRepository.java +++ b/src/main/java/itstime/reflog/community/repository/CommunityRepository.java @@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.Query; import java.util.List; +import java.util.Optional; public interface CommunityRepository extends JpaRepository { @@ -30,4 +31,5 @@ List findByLearningTypesAndPostTypes( //내가 작성한 글 모두 찾기 List findAllByMemberOrderByIdDesc(Member member); + } diff --git a/src/main/java/itstime/reflog/community/service/CommunityConverter.java b/src/main/java/itstime/reflog/community/service/CommunityConverter.java new file mode 100644 index 0000000..ac3b8e0 --- /dev/null +++ b/src/main/java/itstime/reflog/community/service/CommunityConverter.java @@ -0,0 +1,82 @@ +package itstime.reflog.community.service; + +import itstime.reflog.comment.repository.CommentRepository; +import itstime.reflog.community.domain.Community; +import itstime.reflog.community.dto.CommunityDto; +import itstime.reflog.community.repository.CommunityRepository; +import itstime.reflog.member.domain.Member; +import itstime.reflog.mypage.domain.MyPage; +import itstime.reflog.mypage.repository.MyPageRepository; +import itstime.reflog.postlike.repository.PostLikeRepository; +import itstime.reflog.postlike.service.PostLikeService; +import itstime.reflog.retrospect.domain.Retrospect; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor //final로 설정한 것만 의존성 주임 +public class CommunityConverter { + + private final MyPageRepository myPageRepository; + private final PostLikeRepository postLikeRepository; + private final PostLikeService postLikeService; + private final CommentRepository commentRepository; + + @Transactional(readOnly = true) + public List communityResponseConverter(List communities, Member member) { + + List responses = communities.stream() + .map(community -> { + //마이페이지 레포지토리에서 닉네임 반환 + String nickname = myPageRepository.findByMember(community.getMember()) + .map(MyPage::getNickname) + .orElse("닉네임 없음"); + + List communityPostTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화 + List communityLearningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화 + + //좋아요 있는지 없는지 플래그 + Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent(); + + //게시물마다 좋아요 총 갯수 반환 + int totalLike = postLikeService.getSumCommunityPostLike(community); + + //게시물마다 댓글 수 반환 + Long totalComment = commentRepository.countByCommunity(community); + return CommunityDto.CombinedCategoryResponse.fromCommunity(community,communityPostTypes, communityLearningTypes, nickname, isLike, totalLike, totalComment); + }) + .collect(Collectors.toList()); + + return responses; + } + + @Transactional(readOnly = true) + public List retrospectResponseConverter(List retrospects, Member member) { + + List responses = retrospects.stream().map( + retrospect -> { + String nickname = myPageRepository.findByMember(retrospect.getMember()) + .map(MyPage::getNickname) + .orElse("닉네임 없음"); + //좋아요 있는지 없는지 플래그 + Boolean isLike = postLikeRepository.findBookmarkByMemberAndRetrospect(member, retrospect).isPresent(); + + //게시물마다 좋아요 총 갯수 반환 + int totalLike = postLikeService.getSumRetrospectPostLike(retrospect); + + //게시물마다 댓글 수 반환 + Long totalComment = commentRepository.countByRetrospect(retrospect); + + return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment); + }) + .collect(Collectors.toList()); + + return responses; + } + +} diff --git a/src/main/java/itstime/reflog/community/service/CommunityService.java b/src/main/java/itstime/reflog/community/service/CommunityService.java index fc6ee8a..4d7d098 100644 --- a/src/main/java/itstime/reflog/community/service/CommunityService.java +++ b/src/main/java/itstime/reflog/community/service/CommunityService.java @@ -48,9 +48,9 @@ public class CommunityService { private final RetrospectRepository retrospectRepository; private final CommentRepository commentRepository; private final PostLikeService postLikeService; - private final PostLikeRepository postLikeRepository; private final MissionService missionService; private final MemberServiceHelper memberServiceHelper; + private final CommunityConverter communityConverter; @Transactional @@ -222,49 +222,14 @@ public List getFilteredCommunity(String m } //커뮤니티 response형태로 반환 닉네임 추가 - List responses = communities.stream() - .map(community -> { - //마이페이지 레포지토리에서 닉네임 반환 - String nickname = myPageRepository.findByMember(community.getMember()) - .map(MyPage::getNickname) - .orElse("닉네임 없음"); - - //좋아요 있는지 없는지 플래그 - Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent(); - - //게시물마다 좋아요 총 갯수 반환 - int totalLike = postLikeService.getSumCommunityPostLike(community); - - //게시물마다 댓글 수 반환 - Long totalComment = commentRepository.countByCommunity(community); - return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment); - }) - .collect(Collectors.toList()); + List responses = communityConverter.communityResponseConverter(communities, member); //글 유형에 회고일지가 있는 경우 if (postTypes != null && postTypes.contains("회고일지")) { List retrospects = retrospectRepository.findByVisibilityIsTrue(); - List retrospectResponses = retrospects.stream() - .map(retrospect -> { - String nickname = myPageRepository.findByMember(retrospect.getMember()) - .map(MyPage::getNickname) - .orElse("닉네임 없음"); - - //좋아요 있는지 없는지 플래그 - Boolean isLike = postLikeRepository.findLikeByMemberAndRetrospect(member, retrospect).isPresent(); - - //게시물마다 좋아요 총 갯수 반환 - int totalLike = postLikeService.getSumRetrospectPostLike(retrospect); - - //게시물마다 댓글 수 반환 - Long totalComment = commentRepository.countByRetrospect(retrospect); - - return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment); - }) - .collect(Collectors.toList()); + List retrospectResponses = communityConverter.retrospectResponseConverter(retrospects, member); responses.addAll(retrospectResponses); // 두 리스트 합치기(회고일지, 커뮤니티) } - return responses; } @@ -277,44 +242,11 @@ public List getSearchedCommunity(String m //커뮤니티 게시물 중 키워드가 일치하는 게시물 찾기 List communities = communityRepository.searchCommunitiesByTitleContaining(title); - List responses = communities.stream() - .map(community -> { - String nickname = myPageRepository.findByMember(community.getMember()) - .map(MyPage::getNickname) - .orElse("닉네임 없음"); - - //좋아요 있는지 없는지 플래그 - Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent(); - - //게시물마다 좋아요 총 갯수 반환 - int totalLike = postLikeService.getSumCommunityPostLike(community); - - //게시물마다 댓글 수 반환 - Long totalComment = commentRepository.countByCommunity(community); - return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment); - }) - .collect(Collectors.toList()); - + List responses = communityConverter.communityResponseConverter(communities, member); //회고일지 게시물 중 키워드가 일치하는 게시물 찾기 List retrospects = retrospectRepository.findByTitleContainingAndVisibilityIsTrue(title); - List retrospectResponses = retrospects.stream() - .map(retrospect -> { - String nickname = myPageRepository.findByMember(retrospect.getMember()) - .map(MyPage::getNickname) - .orElse("닉네임 없음"); - //좋아요 있는지 없는지 플래그 - Boolean isLike = postLikeRepository.findLikeByMemberAndRetrospect(member, retrospect).isPresent(); - - //게시물마다 좋아요 총 갯수 반환 - int totalLike = postLikeService.getSumRetrospectPostLike(retrospect); - - //게시물마다 댓글 수 반환 - Long totalComment = commentRepository.countByRetrospect(retrospect); - - return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment); - }) - .collect(Collectors.toList()); + List retrospectResponses = communityConverter.retrospectResponseConverter(retrospects, member); responses.addAll(retrospectResponses); // 두 리스트 합치기(회고일지, 커뮤니티) return responses; @@ -328,42 +260,9 @@ public List getAllCommunity(String member List retrospects = retrospectRepository.findAllByVisibilityTrueOrderByCreatedDateDesc(); //회고일지, 커뮤니티 리스트를 합쳐 하나의 리스트로 - List responses = retrospects.stream().map( - retrospect -> { - String nickname = myPageRepository.findByMember(retrospect.getMember()) - .map(MyPage::getNickname) - .orElse("닉네임 없음"); - //좋아요 있는지 없는지 플래그 - Boolean isLike = postLikeRepository.findBookmarkByMemberAndRetrospect(member, retrospect).isPresent(); - - //게시물마다 좋아요 총 갯수 반환 - int totalLike = postLikeService.getSumRetrospectPostLike(retrospect); - - //게시물마다 댓글 수 반환 - Long totalComment = commentRepository.countByRetrospect(retrospect); - - return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment); - }) - .collect(Collectors.toList()); + List responses = communityConverter.retrospectResponseConverter(retrospects, member); - List communityResponses = communities.stream() - .map(community -> { - String nickname = myPageRepository.findByMember(community.getMember()) - .map(MyPage::getNickname) - .orElse("닉네임 없음"); - - //좋아요 있는지 없는지 플래그 - Boolean isLike = postLikeRepository.findBookmarkByMemberAndCommunity(member, community).isPresent(); - - //게시물마다 좋아요 총 갯수 반환 - int totalLike = postLikeService.getSumCommunityPostLike(community); - - //게시물마다 댓글 수 반환 - Long totalComment = commentRepository.countByCommunity(community); - - return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment); - }) - .collect(Collectors.toList()); + List communityResponses = communityConverter.communityResponseConverter(communities, member); responses.addAll(communityResponses); @@ -388,12 +287,15 @@ public List getPostsByProfile(String nickname) { List responses = communityList.stream() .map(community -> { + List postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화 + List learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화 + // 좋아요 총 개수 int totalLike = postLikeService.getSumCommunityPostLike(community); // 댓글 총 개수 long commentCount = commentRepository.countByCommunity(community); - return MyPageDto.MyPagePostResponse.fromCommunity(community, totalLike, commentCount); + return MyPageDto.MyPagePostResponse.fromCommunity(community, postTypes, learningTypes,totalLike, commentCount); }) .collect(Collectors.toList()); diff --git a/src/main/java/itstime/reflog/mypage/dto/MyPageDto.java b/src/main/java/itstime/reflog/mypage/dto/MyPageDto.java index 45516f6..7a958b9 100644 --- a/src/main/java/itstime/reflog/mypage/dto/MyPageDto.java +++ b/src/main/java/itstime/reflog/mypage/dto/MyPageDto.java @@ -60,7 +60,7 @@ public static class MyPagePostResponse { private int totalLike; private long commentCount; - public static MyPagePostResponse fromCommunity(Community community, int totalLike, long commentCount){ + public static MyPagePostResponse fromCommunity(Community community, List postTypes,List learningTypes, int totalLike, long commentCount){ return MyPagePostResponse.builder() .id(community.getId()) .title(community.getTitle()) diff --git a/src/main/java/itstime/reflog/mypage/service/MyPageService.java b/src/main/java/itstime/reflog/mypage/service/MyPageService.java index ed6b591..afc7bee 100644 --- a/src/main/java/itstime/reflog/mypage/service/MyPageService.java +++ b/src/main/java/itstime/reflog/mypage/service/MyPageService.java @@ -23,6 +23,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -142,12 +143,15 @@ public List getMyPost(String memberId) { List responses = communityList.stream() .map(community -> { + List postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화 + List learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화 + // 좋아요 총 개수 int totalLike = postLikeService.getSumCommunityPostLike(community); // 댓글 총 개수 long commentCount = commentRepository.countByCommunity(community); - return MyPageDto.MyPagePostResponse.fromCommunity(community, totalLike, commentCount); + return MyPageDto.MyPagePostResponse.fromCommunity(community, postTypes, learningTypes, totalLike, commentCount); }) .collect(Collectors.toList()); @@ -181,13 +185,17 @@ public List getMyLikePost(String memberId) { // 3. 내가 좋아요 한 커뮤니티 글 정리 List responses = postLikeList.stream() .map(postLike -> { + Community community = postLike.getCommunity(); + + List postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화 + List learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화 // 좋아요 총 개수 int totalLike = postLikeService.getSumCommunityPostLike(postLike.getCommunity()); // 댓글 총 개수 long commentCount = commentRepository.countByCommunity(postLike.getCommunity()); - return MyPageDto.MyPagePostResponse.fromCommunity(postLike.getCommunity(), totalLike, commentCount); + return MyPageDto.MyPagePostResponse.fromCommunity(community, postTypes, learningTypes, totalLike, commentCount); }) .collect(Collectors.toList()); diff --git a/src/main/java/itstime/reflog/postlike/service/PostLikeService.java b/src/main/java/itstime/reflog/postlike/service/PostLikeService.java index 1299add..8d153dc 100644 --- a/src/main/java/itstime/reflog/postlike/service/PostLikeService.java +++ b/src/main/java/itstime/reflog/postlike/service/PostLikeService.java @@ -21,10 +21,10 @@ import itstime.reflog.postlike.repository.PostLikeRepository; import itstime.reflog.retrospect.domain.Retrospect; import itstime.reflog.retrospect.repository.RetrospectRepository; -import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import static itstime.reflog.mission.domain.Badge.POWER_OF_HEART; @@ -46,8 +46,6 @@ public class PostLikeService { private final CommentRepository commentRepository; private final NotificationService notificationService; - private final MemberRepository memberRepository; - @Transactional public void togglePostLike(String memberId, Long postId, PostLikeDto.PostLikeSaveRequest dto){ Member member = memberServiceHelper.findMemberByUuid(memberId); @@ -183,17 +181,17 @@ else if (PostType.RETROSPECT == PostType.valueOf(dto.getPostType())){ } //게시물마다 좋아요 갯수 항상 0이상이므로 int로 - @Transactional + @Transactional(readOnly = true) public int getSumCommunityPostLike(Community community) { return postLikeRepository.countByCommunity(community); } - @Transactional + @Transactional(readOnly = true) public int getSumRetrospectPostLike(Retrospect retrospect) { return postLikeRepository.countByRetrospect(retrospect); } - @Transactional + @Transactional(readOnly = true) public List getTopLikeCommunityPosts(String memberId) { Member member = memberServiceHelper.findMemberByUuid(memberId); @@ -212,16 +210,21 @@ public List getTopLikeCommunityPosts(Stri if (PostType.valueOf((String) popularPost[0]) == PostType.COMMUNITY) { Community community = communityRepository.findById((Long)popularPost[1]) .orElseThrow(() -> new GeneralException(ErrorStatus._POST_NOT_FOUND)); + String nickname = myPageRepository.findByMember(community.getMember()) .map(MyPage::getNickname) .orElse("닉네임 없음"); + Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent(); int totalLike = getSumCommunityPostLike(community); + List postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화 + List learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화 + //게시물마다 댓글 수 반환 Long totalComment = commentRepository.countByCommunity(community); - return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment); + return CommunityDto.CombinedCategoryResponse.fromCommunity(community,postTypes, learningTypes, nickname, isLike, totalLike, totalComment); } else if (popularPost[0] == PostType.RETROSPECT) { Retrospect retrospect = retrospectRepository.findById((Long)popularPost[1]) @@ -244,7 +247,7 @@ public List getTopLikeCommunityPosts(Stri .collect(Collectors.toList()); } - @Transactional + @Transactional(readOnly = true) public List getBookmarks(String memberId){ Member member = memberServiceHelper.findMemberByUuid(memberId);