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 5a5a3f6 commit e40d6c2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 48 deletions.
92 changes: 63 additions & 29 deletions src/main/java/balancetalk/member/application/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,12 +111,7 @@ public Page<GameMyPageResponse> findAllBookmarkedGames(ApiMember apiMember, Page
Game game = gameRepository.findById(bookmark.getGameId()) // 사용자가 북마크한 위치의 밸런스게임을 찾음
.orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_BALANCE_GAME));

List<Long> resourceIds = getResourceIds(game);
List<File> 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();

Expand All @@ -127,31 +126,66 @@ private List<Long> getResourceIds(Game game) {
}


// public Page<GameMyPageResponse> findAllVotedGames(ApiMember apiMember, Pageable pageable) {
// Member member = apiMember.toMember(memberRepository);
//
// Page<GameVote> votes = voteRepository.findAllByMemberIdAndGameDesc(member.getId(), pageable);
//
// List<GameMyPageResponse> responses = votes.stream()
// .map(vote -> GameMyPageResponse.from(vote.getGameOption().getGame(), vote))
// .toList();
//
// return new PageImpl<>(responses, pageable, votes.getTotalElements());
// }

// public Page<GameMyPageResponse> findAllGamesByMember(ApiMember apiMember, Pageable pageable) {
// Member member = apiMember.toMember(memberRepository);
// Page<GameSet> gameSets = gameSetRepository.findAllByMemberIdOrderByEditedAtDesc(member.getId(), pageable);
//
// List<GameMyPageResponse> responses = gameSets.stream()
// .map(GameMyPageResponse::from)
// .toList();
//
// return new PageImpl<>(responses, pageable, gameSets.getTotalElements());
// }
public Page<GameMyPageResponse> findAllVotedGames(ApiMember apiMember, Pageable pageable) {
Member member = apiMember.toMember(memberRepository);

Page<GameVote> votes = voteRepository.findAllByMemberIdAndGameDesc(member.getId(), pageable);

List<GameMyPageResponse> 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<GameMyPageResponse> findAllGamesByMember(ApiMember apiMember, Pageable pageable) {
Member member = apiMember.toMember(memberRepository);
Page<GameSet> gameSets = gameSetRepository.findAllByMemberIdOrderByEditedAtDesc(member.getId(), pageable);

List<GameMyPageResponse> 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<Long> resourceIds = getResourceIds(game);
List<File> 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);
}
}

@Getter
@AllArgsConstructor
private static class SourceHandler<T> {
private final Class<T> type;
private final Function<T, GameMyPageResponse> handler;

public GameMyPageResponse handle(Object source) {
return handler.apply(type.cast(source));
}
}
}
38 changes: 19 additions & 19 deletions src/main/java/balancetalk/member/presentation/MyPageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,25 @@ public Page<GameMyPageResponse> findAllBookmarkedGames(
return myPageService.findAllBookmarkedGames(apiMember, pageable);
}

// @GetMapping("/game-sets/votes")
// @Operation(summary = "투표한 밸런스 게임 목록 조회", description = "로그인한 회원이 투표한 밸런스 게임 목록을 조회한다.")
// public Page<GameMyPageResponse> 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<GameMyPageResponse> 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<GameMyPageResponse> 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<GameMyPageResponse> 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")
Expand Down

0 comments on commit e40d6c2

Please sign in to comment.