Skip to content

Commit

Permalink
Merge pull request #79 from Itstime-replog/refactor/#76
Browse files Browse the repository at this point in the history
Refactor : 인기글 조회 API 리팩토링
jeong724 authored Jan 14, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents b2c5d23 + 9c23c6f commit 34093c8
Showing 6 changed files with 32 additions and 92 deletions.
24 changes: 0 additions & 24 deletions src/main/java/itstime/reflog/postlike/domain/PopularPost.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/itstime/reflog/postlike/domain/PostLike.java
Original file line number Diff line number Diff line change
@@ -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; //북마크인지 좋아요인지 구분하기 위해

This file was deleted.

Original file line number Diff line number Diff line change
@@ -42,15 +42,17 @@ public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

//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<Object[]> 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<Object[]> findARetrospectPostLikesTop();

@Query("SELECT pl from PostLike pl WHERE pl.likeType = 'BOOKMARK' AND pl.member = :member")
List<PostLike> findPostLikesByMember(@Param("member") Member member);


}

This file was deleted.

32 changes: 22 additions & 10 deletions src/main/java/itstime/reflog/postlike/service/PostLikeService.java
Original file line number Diff line number Diff line change
@@ -12,35 +12,35 @@
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 {
private final PostLikeRepository postLikeRepository;
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<CommunityDto.CombinedCategoryResponse> getTopLikeCommunityPosts(String memberId) {
Member member = memberServiceHelper.findMemberByUuid(memberId);

List<PopularPost> postLikesTopThree = popularPostRepository.findAll();
//1. 커뮤니티, 회고일지 좋아요 순으로 각각 가져와서 하나의 배열에 저장
List<Object[]> 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<Object[]> 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<CommunityDto.CombinedCategoryResponse> 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)

0 comments on commit 34093c8

Please sign in to comment.