diff --git a/src/main/java/balancetalk/game/application/SearchGameService.java b/src/main/java/balancetalk/game/application/SearchGameService.java index b74549eb2..b8193cae2 100644 --- a/src/main/java/balancetalk/game/application/SearchGameService.java +++ b/src/main/java/balancetalk/game/application/SearchGameService.java @@ -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; @@ -23,6 +27,7 @@ public class SearchGameService { private final GameRepository gameRepository; + private final FileRepository fileRepository; private static final int MINIMUM_SEARCH_LENGTH = 2; @@ -84,11 +89,24 @@ private String removeSpaces(String query) { private List convertToResponse(List games) { return games.stream() - .map(SearchGameResponse::from) + .map(game -> { + List resourceIds = getResourceIds(game); + List 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 getResourceIds(Game game) { + return game.getGameOptions().stream() + .filter(option -> option.getImgId() != null) + .map(GameOption::getImgId) + .toList(); + } + private void sortByViews(List resultList) { resultList.sort(Comparator .comparingLong((Game game) -> game.getGameSet().getViews()) diff --git a/src/main/java/balancetalk/game/dto/SearchGameResponse.java b/src/main/java/balancetalk/game/dto/SearchGameResponse.java index a1cd67cce..7da739251 100644 --- a/src/main/java/balancetalk/game/dto/SearchGameResponse.java +++ b/src/main/java/balancetalk/game/dto/SearchGameResponse.java @@ -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();