-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converter 전부 static으로 수정 및 변환로직 Converter로 통일 (#196)
* delete: 나쁜 주석 제거 * refactor: MatchGameConverter static으로 변경 * delete: 불필요한 setter 삭제 * refactor: Post Redis Cache로 변환하는 로직 PostConverter로 이동 * delete: 불필요한 DTO 삭제 * refactor: CommentSaveRequest web 폴더로 이동 * refactor: DTO 접미사 삭제 * config: querydsl build 옵션 변경
- Loading branch information
1 parent
3dc827d
commit 4011735
Showing
26 changed files
with
240 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 67 additions & 7 deletions
74
src/main/java/com/gamemoonchul/application/converter/PostConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,79 @@ | ||
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) { | ||
Post entity = Post.builder() | ||
.title(request.title()) | ||
.videoUrl(request.videoUrl()) | ||
.thumbnailUrl(request.thumbnailUrl()) | ||
.content(request.content()) | ||
.tags(request.tags()) | ||
.build(); | ||
entity.setMember(member); | ||
.title(request.title()) | ||
.videoUrl(request.videoUrl()) | ||
.thumbnailUrl(request.thumbnailUrl()) | ||
.content(request.content()) | ||
.tags(request.tags()) | ||
.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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.