Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response도 전부 record로 변경 및 Converter에서 변경하도록 변경 #197

Merged
merged 5 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gamemoonchul.application;

import com.gamemoonchul.application.converter.MatchGameConverter;
import com.gamemoonchul.application.ports.output.RiotApiPort;
import com.gamemoonchul.domain.entity.riot.MatchGame;
import com.gamemoonchul.domain.model.vo.riot.MatchRecord;
Expand Down Expand Up @@ -29,9 +30,9 @@ public MatchGameResponse searchMatch(String gameId) {
MatchRecord vo = riotApi.searchMatch(gameId);
matchGame = matchGameService.save(vo);
matchUserService.saveAll(vo.info()
.participants(), matchGame);
.participants(), matchGame);
}
MatchGameResponse response = MatchGameResponse.toResponse(matchGame);
MatchGameResponse response = MatchGameConverter.toResponse(matchGame);
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gamemoonchul.application.converter;

import com.gamemoonchul.common.util.StringUtils;
import com.gamemoonchul.domain.entity.Comment;
import com.gamemoonchul.infrastructure.web.dto.response.CommentResponse;

public class CommentConverter {
public static CommentResponse toResponse(Comment entity) {
return CommentResponse.builder()
.author(MemberConverter.toResponseDto(entity.getMember()))
.content(entity.getContent())
.timesAgo(StringUtils.getTimeAgo(entity.getCreatedAt()))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.gamemoonchul.application.converter;

import com.gamemoonchul.domain.entity.riot.MatchGame;
import com.gamemoonchul.infrastructure.web.dto.response.MatchGameResponse;

public class MatchGameConverter {
public static MatchGameResponse toResponse(MatchGame matchGame) {
return MatchGameResponse.builder()
.gameId(matchGame.getGameId())
.gameCreation(matchGame.getGameCreation())
.gameDuration(matchGame.getGameDuration())
.gameMode(matchGame.getGameMode())
.matchUsers(matchGame.getMatchUsers()
.stream()
.map(MatchUserConverter::toResponse)
.toList())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.gamemoonchul.application.converter;

import com.gamemoonchul.common.util.PropertyUtil;
import com.gamemoonchul.domain.entity.riot.MatchUser;
import com.gamemoonchul.infrastructure.web.dto.response.MatchUserResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

@Slf4j
public class MatchUserConverter {
public static MatchUserResponse toResponse(MatchUser matchUser) {
String koChampName = getKoChampName(PropertyUtil.loadProperties(), matchUser);

return MatchUserResponse.builder()
.id(matchUser.getId())
.nickname(matchUser.getNickname())
.championName(koChampName)
.championThumbnail(getChampThumbnail(matchUser.getChampionName()))
.win(matchUser.isWin())
.build();
}

/**
* @param matchUser "VoteOption"의 "MatchUser"
* @param voId "VoteOption의 ID"
*/
public static MatchUserResponse toResponseVoId(MatchUser matchUser, Long voId) {
String koChampName = getKoChampName(PropertyUtil.loadProperties(), matchUser);

return MatchUserResponse.builder()
.id(voId)
.nickname(matchUser.getNickname())
.championName(koChampName)
.championThumbnail(getChampThumbnail(matchUser.getChampionName()))
.win(matchUser.isWin())
.build();
}

public static String getChampThumbnail(String championName) {
return "https://ddragon.leagueoflegends.com/cdn/img/champion/splash/" + championName + "_0.jpg";
}

private static String getKoChampName(Properties properties, MatchUser matchUser) {
String engChampName = matchUser.getChampionName();
String koChampName;
try {
// 이후 properties를 사용하여 작업을 수행합니다.
koChampName = (String) properties.get(engChampName);
} catch (Exception e) {
log.error("properties 파일을 읽어오는데 실패했습니다.\n" + e.getMessage() + "\n" + e.getStackTrace());
koChampName = engChampName;
}
return koChampName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
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.CommentResponse;
import com.gamemoonchul.infrastructure.web.dto.response.MatchUserResponse;
import com.gamemoonchul.infrastructure.web.dto.response.PostDetailResponse;
import com.gamemoonchul.infrastructure.web.dto.response.PostMainPageResponse;
import com.gamemoonchul.infrastructure.web.dto.response.VoteRatioResponse;

import java.util.HashMap;
Expand Down Expand Up @@ -40,7 +43,24 @@ public static RedisPostDetail toCache(Post post) {
.build();
}

private static List<VoteRatioResponse> getVoteDetail(Post post) {
public static PostDetailResponse toResponse(Post post, List<CommentResponse> comments) {
return PostDetailResponse.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())
.comments(comments)
.voteDetail(getVoteDetail(post))
.build();
}


public static List<VoteRatioResponse> getVoteDetail(Post post) {
HashMap<Long, Double> voteRatioMap = new HashMap<>();
post.getVoteOptions()
.forEach(vo -> {
Expand Down Expand Up @@ -68,12 +88,26 @@ private static List<VoteRatioResponse> getVoteDetail(Post post) {
.stream()
.map(vo -> {
Double voteRatio = voteRatioMap.get(vo.getId());
MatchGameResponse.MatchUserResponse matchUserResponse = MatchGameResponse.MatchUserResponse.toResponseVoId(vo.getMatchUser(), vo.getId());
MatchUserResponse matchUserResponse = MatchUserConverter.toResponseVoId(vo.getMatchUser(), vo.getId());
return new VoteRatioResponse(matchUserResponse, voteRatio);
})
.toList();

return result;
}

public static PostMainPageResponse entityToResponse(Post entity) {
List<Double> voteRatio = List.of(100 - entity.getVoteRatio(), entity.getVoteRatio());
return PostMainPageResponse.builder()
.id(entity.getId())
.member(MemberConverter.toResponseDto(entity.getMember()))
.videoUrl(entity.getVideoUrl())
.thumbnailUrl(entity.getThumbnailUrl())
.title(entity.getTitle())
.content(entity.getContent())
.viewCount(entity.getViewCount())
.timesAgo(StringUtils.getTimeAgo(entity.getCreatedAt()))
.voteRatio(voteRatio)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gamemoonchul.application.converter;

import com.gamemoonchul.domain.entity.Vote;
import com.gamemoonchul.infrastructure.web.dto.response.VoteResponse;

public class VoteConverter {
public static VoteResponse entityToResponse(Vote vote) {
return VoteResponse.builder()
.id(vote.getId())
.postId(vote.getPost().getId())
.voteOptionId(vote.getVoteOption().getId())
.build();
}
}
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.CommentConverter;
import com.gamemoonchul.application.converter.PostConverter;
import com.gamemoonchul.common.exception.BadRequestException;
import com.gamemoonchul.domain.entity.Post;
Expand Down Expand Up @@ -44,7 +45,7 @@ public RedisPostDetail getPostDetails(Long postId, Long requestMemberId) {
}

List<CommentResponse> comments = commentService.searchByPostId(redisPostDetail.getId(), requestMemberId).stream()
.map(CommentResponse::entityToResponse).toList(); // 변경 자주 일어남, 캐싱 X
.map(CommentConverter::toResponse).toList(); // 변경 자주 일어남, 캐싱 X
redisPostDetail.setComments(comments);

return redisPostDetail;
Expand All @@ -54,7 +55,7 @@ public Pagination<PostMainPageResponse> getLatestPosts(Long requestMemberId, int
Page<Post> savedPage = postRepository.searchNewPostsWithoutBanPosts(requestMemberId, PageRequest.of(page, size));
List<PostMainPageResponse> responses = savedPage.getContent()
.stream()
.map(PostMainPageResponse::entityToResponse)
.map(PostConverter::entityToResponse)
.collect(Collectors.toList());

return new Pagination<>(savedPage, responses);
Expand All @@ -64,7 +65,7 @@ public Pagination<PostMainPageResponse> getGrillPosts(Long requestMemberId, int
Page<Post> savedPage = postRepository.searchGrillPostsWithoutBanPosts(requestMemberId, PageRequest.of(page, size));
List<PostMainPageResponse> responses = savedPage.getContent()
.stream()
.map(PostMainPageResponse::entityToResponse)
.map(PostConverter::entityToResponse)
.toList();
return new Pagination<PostMainPageResponse>(savedPage, responses);
}
Expand All @@ -74,7 +75,7 @@ public Pagination<PostMainPageResponse> getHotPosts(int page, int size, Long req

List<PostMainPageResponse> responses = savedPage.getContent()
.stream()
.map(PostMainPageResponse::entityToResponse)
.map(PostConverter::entityToResponse)
.toList();
return new Pagination<>(savedPage, responses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PostDetailResponse upload(PostUploadRequest request, Member member) {

saveVoteOptions(request.matchUserIds(), savedPost);

return PostDetailResponse.toResponse(savedPost, Collections.emptyList());
return PostConverter.toResponse(savedPost, Collections.emptyList());
}

private void saveVoteOptions(List<Long> matchUserIds, Post post) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/gamemoonchul/common/util/PropertyUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gamemoonchul.common.util;

import com.gamemoonchul.infrastructure.web.dto.response.MatchUserResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@Slf4j
public class PropertyUtil {
public static Properties loadProperties() {
Properties properties = new Properties();
try {
InputStream inputStream = MatchUserResponse.class.getClassLoader()
.getResourceAsStream("lolchampion.properties");
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
properties = new Properties();
properties.load(reader);
} catch (IOException e) {
log.error("properties 파일을 읽어오는데 실패했습니다.\n" + e.getMessage() + "\n" + e.getStackTrace());
}
return properties;
}

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

import com.gamemoonchul.application.PostBanService;
import com.gamemoonchul.application.converter.PostConverter;
import com.gamemoonchul.common.annotation.MemberSession;
import com.gamemoonchul.domain.entity.Member;
import com.gamemoonchul.domain.entity.PostBan;
Expand All @@ -22,18 +23,18 @@ public class PostBanApiController {

@GetMapping("/ban")
public List<PostMainPageResponse> getBannedPosts(
@MemberSession Member member
@MemberSession Member member
) {
return postBanService.bannedPost(member.getId()).stream()
.map(PostBan::getBanPost)
.map(PostMainPageResponse::entityToResponse)
.toList();
.map(PostBan::getBanPost)
.map(PostConverter::entityToResponse)
.toList();
}

@PostMapping("/ban/{postId}")
public void ban(
@MemberSession Member member,
@PathVariable(name = "postId") Long postId
@MemberSession Member member,
@PathVariable(name = "postId") Long postId
) {
postBanService.ban(member, postId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gamemoonchul.infrastructure.web;

import com.gamemoonchul.application.VoteService;
import com.gamemoonchul.application.converter.VoteConverter;
import com.gamemoonchul.common.annotation.MemberSession;
import com.gamemoonchul.domain.entity.Member;
import com.gamemoonchul.domain.entity.Vote;
Expand All @@ -23,10 +24,10 @@ public class VoteController {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public VoteResponse save(
@RequestBody VoteCreateRequest voteCreateRequest,
@MemberSession Member member
@RequestBody VoteCreateRequest voteCreateRequest,
@MemberSession Member member
) {
Vote vote = voteService.createVote(voteCreateRequest, member);
return VoteResponse.entityToResponse(vote);
return VoteConverter.entityToResponse(vote);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
package com.gamemoonchul.infrastructure.web.dto.response;

import com.gamemoonchul.application.converter.MemberConverter;
import com.gamemoonchul.common.util.StringUtils;
import com.gamemoonchul.domain.entity.Comment;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommentResponse {
private MemberResponse author;
private String content;
private String timesAgo;
public record CommentResponse(
MemberResponse author,
String content,
String timesAgo
) {

public static CommentResponse entityToResponse(Comment entity) {
return CommentResponse.builder()
.author(MemberConverter.toResponseDto(entity.getMember()))
.content(entity.getContent())
.timesAgo(StringUtils.getTimeAgo(entity.getCreatedAt()))
.build();
}
}
Loading
Loading