Skip to content

Commit

Permalink
refactor: 밸런스게임 검색 imgURL 필드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
gywns0417 committed Dec 6, 2024
1 parent e40d6c2 commit 6e3c6e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import static balancetalk.global.exception.ErrorCode.BALANCE_GAME_SEARCH_BLANK;
import static balancetalk.global.exception.ErrorCode.BALANCE_GAME_SEARCH_LENGTH;

import balancetalk.file.domain.File;
import balancetalk.file.domain.FileType;
import balancetalk.file.domain.repository.FileRepository;
import balancetalk.game.domain.Game;
import balancetalk.game.domain.GameOption;
import balancetalk.game.domain.repository.GameRepository;
import balancetalk.game.dto.SearchGameResponse;
import balancetalk.global.exception.BalanceTalkException;
Expand All @@ -23,6 +27,7 @@
public class SearchGameService {

private final GameRepository gameRepository;
private final FileRepository fileRepository;

private static final int MINIMUM_SEARCH_LENGTH = 2;

Expand Down Expand Up @@ -84,11 +89,24 @@ private String removeSpaces(String query) {

private List<SearchGameResponse> convertToResponse(List<Game> games) {
return games.stream()
.map(SearchGameResponse::from)
.map(game -> {
List<Long> resourceIds = getResourceIds(game);
List<File> files = fileRepository.findAllByResourceIdsAndFileType(resourceIds, FileType.GAME_OPTION);
String imgA = files.isEmpty() ? null : game.getImgA(files);
String imgB = files.isEmpty() ? null : game.getImgB(files);
return SearchGameResponse.from(game, imgA, imgB);
})
.distinct() // 중복 제거
.toList();
}

private List<Long> getResourceIds(Game game) {
return game.getGameOptions().stream()
.filter(option -> option.getImgId() != null)
.map(GameOption::getImgId)
.toList();
}

private void sortByViews(List<Game> resultList) {
resultList.sort(Comparator
.comparingLong((Game game) -> game.getGameSet().getViews())
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/balancetalk/game/dto/SearchGameResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public class SearchGameResponse {
@Schema(description = "밸런스 게임 메인 태그", example = "인기")
private String mainTag;

public static SearchGameResponse from(Game game) {
public static SearchGameResponse from(Game game, String imgA, String imgB) {
return SearchGameResponse.builder()
.gameSetId(game.getGameSet().getId())
.id(game.getId())
.title(game.getGameSet().getTitle())
// .optionAImg(game.getGameOptions().get(0).getImgUrl())
// .optionBImg(game.getGameOptions().get(1).getImgUrl())
.optionAImg(imgA)
.optionBImg(imgB)
.subTag(game.getGameSet().getSubTag())
.mainTag(game.getGameSet().getMainTag().getName())
.build();
Expand Down

0 comments on commit 6e3c6e6

Please sign in to comment.