diff --git a/src/main/java/balancetalk/member/application/MyPageService.java b/src/main/java/balancetalk/member/application/MyPageService.java index 0ab558c2a..f0937cc57 100644 --- a/src/main/java/balancetalk/member/application/MyPageService.java +++ b/src/main/java/balancetalk/member/application/MyPageService.java @@ -28,6 +28,10 @@ import balancetalk.vote.domain.TalkPickVoteRepository; import balancetalk.vote.domain.GameVote; import balancetalk.vote.domain.VoteRepository; +import java.util.function.Function; +import java.util.stream.Stream; +import lombok.AllArgsConstructor; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; @@ -107,12 +111,7 @@ public Page findAllBookmarkedGames(ApiMember apiMember, Page Game game = gameRepository.findById(bookmark.getGameId()) // 사용자가 북마크한 위치의 밸런스게임을 찾음 .orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_BALANCE_GAME)); - List resourceIds = getResourceIds(game); - List files = fileRepository.findAllByResourceIdsAndFileType(resourceIds, FileType.GAME_OPTION); - String imgA = game.getImgA(files); - String imgB = game.getImgB(files); - - return GameMyPageResponse.from(game, bookmark, imgA, imgB); + return createGameMyPageResponse(game, bookmark); }) .toList(); @@ -127,31 +126,66 @@ private List getResourceIds(Game game) { } -// public Page findAllVotedGames(ApiMember apiMember, Pageable pageable) { -// Member member = apiMember.toMember(memberRepository); -// -// Page votes = voteRepository.findAllByMemberIdAndGameDesc(member.getId(), pageable); -// -// List responses = votes.stream() -// .map(vote -> GameMyPageResponse.from(vote.getGameOption().getGame(), vote)) -// .toList(); -// -// return new PageImpl<>(responses, pageable, votes.getTotalElements()); -// } - -// public Page findAllGamesByMember(ApiMember apiMember, Pageable pageable) { -// Member member = apiMember.toMember(memberRepository); -// Page gameSets = gameSetRepository.findAllByMemberIdOrderByEditedAtDesc(member.getId(), pageable); -// -// List responses = gameSets.stream() -// .map(GameMyPageResponse::from) -// .toList(); -// -// return new PageImpl<>(responses, pageable, gameSets.getTotalElements()); -// } + public Page findAllVotedGames(ApiMember apiMember, Pageable pageable) { + Member member = apiMember.toMember(memberRepository); + + Page votes = voteRepository.findAllByMemberIdAndGameDesc(member.getId(), pageable); + + List responses = votes.stream() + .map(vote -> { + Game game = gameRepository.findById(vote.getGameOption().getGame().getId()) + .orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_BALANCE_GAME)); + return createGameMyPageResponse(game, vote); + }) + .toList(); + + return new PageImpl<>(responses, pageable, votes.getTotalElements()); + } + + public Page findAllGamesByMember(ApiMember apiMember, Pageable pageable) { + Member member = apiMember.toMember(memberRepository); + Page gameSets = gameSetRepository.findAllByMemberIdOrderByEditedAtDesc(member.getId(), pageable); + + List responses = gameSets.stream() + .map(gameSet -> { + Game game = gameSet.getGames().get(0); + return createGameMyPageResponse(game, game); + }) + .toList(); + + return new PageImpl<>(responses, pageable, gameSets.getTotalElements()); + } + + private GameMyPageResponse createGameMyPageResponse(Game game, Object source) { + List resourceIds = getResourceIds(game); + List files = fileRepository.findAllByResourceIdsAndFileType(resourceIds, FileType.GAME_OPTION); + String imgA = game.getImgA(files); + String imgB = game.getImgB(files); + + return Stream.of( + new SourceHandler<>(GameBookmark.class, bookmark -> GameMyPageResponse.from(game, bookmark, imgA, imgB)), + new SourceHandler<>(GameVote.class, vote -> GameMyPageResponse.from(game, vote, imgA, imgB)), + new SourceHandler<>(Game.class, myGame -> GameMyPageResponse.from(myGame.getGameSet(), imgA, imgB)) + ) + .filter(handler -> handler.getType().isInstance(source)) + .findFirst() + .map(handler -> handler.handle(source)) + .orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_BALANCE_GAME)); + } public MemberActivityResponse getMemberActivity(ApiMember apiMember) { Member member = apiMember.toMember(memberRepository); return MemberActivityResponse.fromEntity(member); } -} \ No newline at end of file + + @Getter + @AllArgsConstructor + private static class SourceHandler { + private final Class type; + private final Function handler; + + public GameMyPageResponse handle(Object source) { + return handler.apply(type.cast(source)); + } + } +} diff --git a/src/main/java/balancetalk/member/presentation/MyPageController.java b/src/main/java/balancetalk/member/presentation/MyPageController.java index c4198017a..dd9849676 100644 --- a/src/main/java/balancetalk/member/presentation/MyPageController.java +++ b/src/main/java/balancetalk/member/presentation/MyPageController.java @@ -78,25 +78,25 @@ public Page findAllBookmarkedGames( return myPageService.findAllBookmarkedGames(apiMember, pageable); } -// @GetMapping("/game-sets/votes") -// @Operation(summary = "투표한 밸런스 게임 목록 조회", description = "로그인한 회원이 투표한 밸런스 게임 목록을 조회한다.") -// public Page findAllVotedGames( -// @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6", required = false) int size, -// @Parameter(hidden = true) @AuthPrincipal ApiMember apiMember) { -// -// Pageable pageable = PageRequest.of(page, size); -// return myPageService.findAllVotedGames(apiMember, pageable); -// } -// -// @GetMapping("/game-sets/written") -// @Operation(summary = "내가 작성한 밸런스 게임 목록 조회", description = "로그인한 회원이 작성한 밸런스 게임 목록을 조회한다.") -// public Page findAllMyGames( -// @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6", required = false) int size, -// @Parameter(hidden = true) @AuthPrincipal ApiMember apiMember) { -// -// Pageable pageable = PageRequest.of(page, size); -// return myPageService.findAllGamesByMember(apiMember, pageable); -// } + @GetMapping("/game-sets/votes") + @Operation(summary = "투표한 밸런스 게임 목록 조회", description = "로그인한 회원이 투표한 밸런스 게임 목록을 조회한다.") + public Page findAllVotedGames( + @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6", required = false) int size, + @Parameter(hidden = true) @AuthPrincipal ApiMember apiMember) { + + Pageable pageable = PageRequest.of(page, size); + return myPageService.findAllVotedGames(apiMember, pageable); + } + + @GetMapping("/game-sets/written") + @Operation(summary = "내가 작성한 밸런스 게임 목록 조회", description = "로그인한 회원이 작성한 밸런스 게임 목록을 조회한다.") + public Page findAllMyGames( + @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6", required = false) int size, + @Parameter(hidden = true) @AuthPrincipal ApiMember apiMember) { + + Pageable pageable = PageRequest.of(page, size); + return myPageService.findAllGamesByMember(apiMember, pageable); + } @ResponseStatus(HttpStatus.OK) @GetMapping("/activity")