diff --git a/src/main/java/itstime/reflog/postlike/domain/PopularPost.java b/src/main/java/itstime/reflog/postlike/domain/PopularPost.java deleted file mode 100644 index b02ab0a..0000000 --- a/src/main/java/itstime/reflog/postlike/domain/PopularPost.java +++ /dev/null @@ -1,24 +0,0 @@ -package itstime.reflog.postlike.domain; - -import itstime.reflog.postlike.domain.enums.PostType; -import jakarta.persistence.*; -import lombok.*; - -import java.util.List; - -@Entity -@Getter -@Builder -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -public class PopularPost { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(nullable = false) - private Long postId; - - @Enumerated(EnumType.STRING) - private PostType postType; -} diff --git a/src/main/java/itstime/reflog/postlike/domain/PostLike.java b/src/main/java/itstime/reflog/postlike/domain/PostLike.java index 158aa3b..250a3f1 100644 --- a/src/main/java/itstime/reflog/postlike/domain/PostLike.java +++ b/src/main/java/itstime/reflog/postlike/domain/PostLike.java @@ -9,6 +9,8 @@ import jakarta.persistence.*; import lombok.*; +import java.time.LocalDate; + @Entity @Getter @Builder @@ -35,6 +37,9 @@ public class PostLike { @Enumerated(EnumType.STRING) private PostType postType; + @Column(name = "created_date") + private LocalDate createdDate; + @Enumerated(EnumType.STRING) private LikeType likeType; //북마크인지 좋아요인지 구분하기 위해 diff --git a/src/main/java/itstime/reflog/postlike/repository/PopularPostRepository.java b/src/main/java/itstime/reflog/postlike/repository/PopularPostRepository.java deleted file mode 100644 index cdf5f80..0000000 --- a/src/main/java/itstime/reflog/postlike/repository/PopularPostRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package itstime.reflog.postlike.repository; - -import itstime.reflog.postlike.domain.PopularPost; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface PopularPostRepository extends JpaRepository { -} diff --git a/src/main/java/itstime/reflog/postlike/repository/PostLikeRepository.java b/src/main/java/itstime/reflog/postlike/repository/PostLikeRepository.java index 0856d61..f526f8d 100644 --- a/src/main/java/itstime/reflog/postlike/repository/PostLikeRepository.java +++ b/src/main/java/itstime/reflog/postlike/repository/PostLikeRepository.java @@ -42,15 +42,17 @@ public interface PostLikeRepository extends JpaRepository { //community 게시물 좋아요 많은 순으로 정렬하고 id, 좋아요 수 반환 : Object[0] = id, Object[1] = 좋아요 수 //타입을 구분하기 위해 앞에 타입도 반환 - @Query("SELECT 'COMMUNITY', pl.community.id, COUNT(pl) as likeCount FROM PostLike pl WHERE pl.postType = 'COMMUNITY' AND pl.likeType = 'LIKE' GROUP BY pl.community.id " + + @Query("SELECT 'COMMUNITY', pl.community.id, COUNT(pl) as likeCount FROM PostLike pl WHERE pl.postType = 'COMMUNITY' AND pl.likeType = 'LIKE' AND pl.createdDate < CURRENT_DATE GROUP BY pl.community.id " + "ORDER BY likeCount DESC") List findCommunityByPostLikeTop(); - //retrospect 게시물 좋아요 많은 순으로 정렬 - @Query("SELECT 'RETROSPECT', pl.retrospect.id, COUNT(pl) as likeCount FROM PostLike pl WHERE pl.postType = 'RETROSPECT' AND pl.likeType = 'LIKE' GROUP BY pl.retrospect.id " + + //retrospect 게시물 좋아요 많은 순으로 정렬 + 어제까지의 데이터 중에서만 + @Query("SELECT 'RETROSPECT', pl.retrospect.id, COUNT(pl) as likeCount FROM PostLike pl WHERE pl.postType = 'RETROSPECT' AND pl.likeType = 'LIKE' AND pl.createdDate < CURRENT_DATE GROUP BY pl.retrospect.id " + "ORDER BY likeCount DESC") List findARetrospectPostLikesTop(); @Query("SELECT pl from PostLike pl WHERE pl.likeType = 'BOOKMARK' AND pl.member = :member") List findPostLikesByMember(@Param("member") Member member); + + } diff --git a/src/main/java/itstime/reflog/postlike/service/PopularScheduler.java b/src/main/java/itstime/reflog/postlike/service/PopularScheduler.java deleted file mode 100644 index eaec33e..0000000 --- a/src/main/java/itstime/reflog/postlike/service/PopularScheduler.java +++ /dev/null @@ -1,48 +0,0 @@ -package itstime.reflog.postlike.service; - -import itstime.reflog.postlike.domain.PopularPost; -import itstime.reflog.postlike.domain.PostLike; -import itstime.reflog.postlike.domain.enums.PostType; -import itstime.reflog.postlike.repository.PopularPostRepository; -import itstime.reflog.postlike.repository.PostLikeRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; - -@Service -@RequiredArgsConstructor -public class PopularScheduler { - - private final PostLikeRepository postLikeRepository; - private final PopularPostRepository popularPostRepository; - - @Scheduled(cron = "0 0 0 * * *") - public void scheduledPopularPosts() { - - //1. 이전에 있던 인기글 목록 지우기 - popularPostRepository.deleteAll(); - - //2. 커뮤니티, 회고일지 좋아요 순으로 각각 가져와서 하나의 배열에 저장 - List postLikesTop = new ArrayList<>(postLikeRepository.findCommunityByPostLikeTop()); - postLikesTop.addAll(postLikeRepository.findARetrospectPostLikesTop()); - - //좋아요 수인 object[1]이 Object 타입이기 떄문에 Long 타입으로 바꿔 준 후 비교 정렬 - postLikesTop.sort((o1, o2) -> Long.compare((Long) o2[2], (Long) o1[2])); - - //이 중 상위 세개만 가져옴 - List postLikesTopThree = postLikesTop.stream().limit(3).toList(); - - //가져온 데이터 저장 - for (Object[] postLike : postLikesTopThree) { - - PopularPost popularPost = PopularPost.builder() - .postId((Long) postLike[1]) - .postType(PostType.valueOf((String) postLike[0])) - .build(); - popularPostRepository.save(popularPost); - } - } -} diff --git a/src/main/java/itstime/reflog/postlike/service/PostLikeService.java b/src/main/java/itstime/reflog/postlike/service/PostLikeService.java index 55522a9..1299add 100644 --- a/src/main/java/itstime/reflog/postlike/service/PostLikeService.java +++ b/src/main/java/itstime/reflog/postlike/service/PostLikeService.java @@ -12,27 +12,28 @@ import itstime.reflog.mypage.repository.MyPageRepository; import itstime.reflog.notification.domain.NotificationType; import itstime.reflog.notification.service.NotificationService; -import itstime.reflog.postlike.domain.PopularPost; import itstime.reflog.member.service.MemberServiceHelper; import itstime.reflog.mission.service.MissionService; import itstime.reflog.postlike.domain.PostLike; import itstime.reflog.postlike.domain.enums.LikeType; import itstime.reflog.postlike.domain.enums.PostType; import itstime.reflog.postlike.dto.PostLikeDto; -import itstime.reflog.postlike.repository.PopularPostRepository; 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 static itstime.reflog.mission.domain.Badge.POWER_OF_HEART; +import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +@Slf4j @Service @RequiredArgsConstructor public class PostLikeService { @@ -40,7 +41,6 @@ public class PostLikeService { private final CommunityRepository communityRepository; private final RetrospectRepository retrospectRepository; private final MyPageRepository myPageRepository; - private final PopularPostRepository popularPostRepository; private final MemberServiceHelper memberServiceHelper; private final MissionService missionService; private final CommentRepository commentRepository; @@ -70,13 +70,14 @@ public void togglePostLike(String memberId, Long postId, PostLikeDto.PostLikeSav //2.좋아요 존재 여부 확인 if (postLike != null && postLike.getLikeType().equals(LikeType.LIKE)) { - //좋아요가 이미 있다면 테이블 삭제 + //좋아요가 이미 있다면 엔티티 삭제 postLikeRepository.delete(postLike); } else { //좋아요가 없다면 테이블 생성 PostLike newPostLike = PostLike.builder() .member(member) .community(community) + .createdDate(LocalDate.now()) .postType(PostType.COMMUNITY) .likeType(LikeType.LIKE) //좋아요 .build(); @@ -103,6 +104,7 @@ public void togglePostLike(String memberId, Long postId, PostLikeDto.PostLikeSav PostLike newPostLike = PostLike.builder() .member(member) .community(community) + .createdDate(LocalDate.now()) .postType(PostType.COMMUNITY) .likeType(LikeType.BOOKMARK) //북마크 .build(); @@ -134,6 +136,7 @@ else if (PostType.RETROSPECT == PostType.valueOf(dto.getPostType())){ .member(member) .retrospect(retrospect) .postType(PostType.RETROSPECT) + .createdDate(LocalDate.now()) .likeType(LikeType.LIKE) .build(); @@ -145,7 +148,6 @@ else if (PostType.RETROSPECT == PostType.valueOf(dto.getPostType())){ // 알림 sendRetrospectLikeNotification(retrospect, member); - } } else if (LikeType.BOOKMARK == LikeType.valueOf(dto.getLikeType())) { //북마크한경우 //커뮤니티, 멤버 id와 일치하는 북마크 가져오기 @@ -155,12 +157,14 @@ else if (PostType.RETROSPECT == PostType.valueOf(dto.getPostType())){ if (postLike != null && postLike.getLikeType().equals(LikeType.BOOKMARK)) { //북마크가 이미 있다면 테이블 삭제 postLikeRepository.delete(postLike); + } else { //좋아요가 없다면 테이블 생성 PostLike newPostLike = PostLike.builder() .member(member) .retrospect(retrospect) .postType(PostType.RETROSPECT) + .createdDate(LocalDate.now()) .likeType(LikeType.BOOKMARK) //북마크 .build(); @@ -193,12 +197,20 @@ public int getSumRetrospectPostLike(Retrospect retrospect) { public List getTopLikeCommunityPosts(String memberId) { Member member = memberServiceHelper.findMemberByUuid(memberId); - List postLikesTopThree = popularPostRepository.findAll(); + //1. 커뮤니티, 회고일지 좋아요 순으로 각각 가져와서 하나의 배열에 저장 + List postLikesTop = new ArrayList<>(postLikeRepository.findCommunityByPostLikeTop()); + postLikesTop.addAll(postLikeRepository.findARetrospectPostLikesTop()); + + //2. 좋아요 수인 object[1]이 Object 타입이기 떄문에 Long 타입으로 바꿔 준 후 비교 정렬 + postLikesTop.sort((o1, o2) -> Long.compare((Long) o2[2], (Long) o1[2])); + + //3. 이 중 상위 세개만 가져옴 + List postLikesTopThree = postLikesTop.stream().limit(3).toList(); return postLikesTopThree.stream() .map(popularPost -> { - if (popularPost.getPostType() == PostType.COMMUNITY) { - Community community = communityRepository.findById(popularPost.getPostId()) + 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) @@ -211,8 +223,8 @@ public List getTopLikeCommunityPosts(Stri return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment); - } else if (popularPost.getPostType() == PostType.RETROSPECT) { - Retrospect retrospect = retrospectRepository.findById(popularPost.getPostId()) + } else if (popularPost[0] == PostType.RETROSPECT) { + Retrospect retrospect = retrospectRepository.findById((Long)popularPost[1]) .orElseThrow(() -> new GeneralException(ErrorStatus._POST_NOT_FOUND)); String nickname = myPageRepository.findByMember(retrospect.getMember()) .map(MyPage::getNickname)