Skip to content

Commit

Permalink
refactor: Post Redis Cache로 변환하는 로직 PostConverter로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
rookedsysc committed Oct 12, 2024
1 parent 8ef1811 commit 7dbe546
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.gamemoonchul.application.converter;

import com.gamemoonchul.common.util.StringUtils;
import com.gamemoonchul.domain.entity.Member;
import com.gamemoonchul.domain.entity.Post;
import com.gamemoonchul.domain.entity.redis.RedisPostDetail;
import com.gamemoonchul.infrastructure.web.dto.request.PostUploadRequest;
import com.gamemoonchul.infrastructure.web.dto.response.MatchGameResponse;
import com.gamemoonchul.infrastructure.web.dto.response.VoteRatioResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

public class PostConverter {
public static Post requestToEntity(PostUploadRequest request, Member member) {
Expand All @@ -16,4 +24,56 @@ public static Post requestToEntity(PostUploadRequest request, Member member) {
.build();
return entity;
}

public static RedisPostDetail toCache(Post post) {
return RedisPostDetail.builder()
.id(post.getId())
.author(MemberConverter.toResponseDto(post.getMember()))
.videoUrl(post.getVideoUrl())
.thumbnailUrl(post.getThumbnailUrl())
.commentCount(post.getCommentCount())
.title(post.getTitle())
.content(post.getContent())
.timesAgo(StringUtils.getTimeAgo(post.getCreatedAt()))
.viewCount(post.getViewCount())
.voteDetail(getVoteDetail(post))
.build();
}

private static List<VoteRatioResponse> getVoteDetail(Post post) {
HashMap<Long, Double> voteRatioMap = new HashMap<>();
post.getVoteOptions()
.forEach(vo -> {
voteRatioMap.put(vo.getId(), 0.0);
Optional.ofNullable(vo.getVotes())
.ifPresent(votes -> {
votes.forEach(v -> voteRatioMap.put(v.getId(), 0.0));
});
});

Double totalVoteCnt = voteRatioMap.values()
.stream()
.mapToDouble(Double::doubleValue)
.sum();

voteRatioMap.forEach((k, v) -> {
if (totalVoteCnt == 0) {
voteRatioMap.put(k, 0.0);
} else {
voteRatioMap.put(k, (v / totalVoteCnt) * 100);
}
});

List<VoteRatioResponse> result = post.getVoteOptions()
.stream()
.map(vo -> {
Double voteRatio = voteRatioMap.get(vo.getId());
MatchGameResponse.MatchUserResponse matchUserResponse = MatchGameResponse.MatchUserResponse.toResponseVoId(vo.getMatchUser(), vo.getId());
return new VoteRatioResponse(matchUserResponse, voteRatio);
})
.toList();

return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gamemoonchul.application.post;

import com.gamemoonchul.application.CommentService;
import com.gamemoonchul.application.converter.PostConverter;
import com.gamemoonchul.common.exception.BadRequestException;
import com.gamemoonchul.domain.entity.Post;
import com.gamemoonchul.domain.entity.redis.RedisPostDetail;
Expand Down Expand Up @@ -37,7 +38,7 @@ public RedisPostDetail getPostDetails(Long postId, Long requestMemberId) {
Post post = postRepository.searchByPostId(postId).orElseThrow(() -> new BadRequestException(PostStatus.POST_NOT_FOUND));
post.viewCountUp();

redisPostDetail = redisPostDetailRepository.save(RedisPostDetail.toCache(post));
redisPostDetail = redisPostDetailRepository.save(PostConverter.toCache(post));
} else {
redisPostDetail = optionalPostDetail.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.gamemoonchul.domain.entity.redis;

import com.gamemoonchul.application.converter.MemberConverter;
import com.gamemoonchul.common.util.StringUtils;
import com.gamemoonchul.domain.entity.Post;
import com.gamemoonchul.infrastructure.web.dto.response.CommentResponse;
import com.gamemoonchul.infrastructure.web.dto.response.MatchGameResponse;
import com.gamemoonchul.infrastructure.web.dto.response.MemberResponseDto;
import com.gamemoonchul.infrastructure.web.dto.response.PostDetailResponse;
import com.gamemoonchul.infrastructure.web.dto.response.VoteRatioResponse;
import jakarta.persistence.Id;
import lombok.AccessLevel;
Expand All @@ -17,9 +12,7 @@
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Getter
@Builder
Expand All @@ -41,73 +34,6 @@ public class RedisPostDetail {
private List<CommentResponse> comments;
private List<VoteRatioResponse> voteDetail;

public static RedisPostDetail fromResponse(PostDetailResponse postDetailResponse) {
return RedisPostDetail.builder()
.id(postDetailResponse.getId())
.author(postDetailResponse.getAuthor())
.videoUrl(postDetailResponse.getVideoUrl())
.thumbnailUrl(postDetailResponse.getThumbnailUrl())
.title(postDetailResponse.getTitle())
.content(postDetailResponse.getContent())
.timesAgo(postDetailResponse.getTimesAgo())
.viewCount(postDetailResponse.getViewCount())
.commentCount(postDetailResponse.getCommentCount())
.comments(postDetailResponse.getComments())
.voteDetail(postDetailResponse.getVoteDetail())
.build();
}

public static RedisPostDetail toCache(Post post) {
return RedisPostDetail.builder()
.id(post.getId())
.author(MemberConverter.toResponseDto(post.getMember()))
.videoUrl(post.getVideoUrl())
.thumbnailUrl(post.getThumbnailUrl())
.commentCount(post.getCommentCount())
.title(post.getTitle())
.content(post.getContent())
.timesAgo(StringUtils.getTimeAgo(post.getCreatedAt()))
.viewCount(post.getViewCount())
.voteDetail(getVoteDetail(post))
.build();
}

public static List<VoteRatioResponse> getVoteDetail(Post post) {
HashMap<Long, Double> voteRatioMap = new HashMap<>();
post.getVoteOptions()
.forEach(vo -> {
voteRatioMap.put(vo.getId(), 0.0);
Optional.ofNullable(vo.getVotes())
.ifPresent(votes -> {
votes.forEach(v -> voteRatioMap.put(v.getId(), 0.0));
});
});

Double totalVoteCnt = voteRatioMap.values()
.stream()
.mapToDouble(Double::doubleValue)
.sum();

voteRatioMap.forEach((k, v) -> {
if (totalVoteCnt == 0) {
voteRatioMap.put(k, 0.0);
} else {
voteRatioMap.put(k, (v / totalVoteCnt) * 100);
}
});

List<VoteRatioResponse> result = post.getVoteOptions()
.stream()
.map(vo -> {
Double voteRatio = voteRatioMap.get(vo.getId());
MatchGameResponse.MatchUserResponse matchUserResponse = MatchGameResponse.MatchUserResponse.toResponseVoId(vo.getMatchUser(), vo.getId());
return new VoteRatioResponse(matchUserResponse, voteRatio);
})
.toList();

return result;
}

public void setComments(List<CommentResponse> comments) {
this.comments = comments;
}
Expand Down

0 comments on commit 7dbe546

Please sign in to comment.