Skip to content

Commit

Permalink
refactor: balance game type을 enum으로 변경한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Jan 13, 2024
1 parent 9bf9b8f commit 2462660
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
34 changes: 34 additions & 0 deletions src/main/java/net/teumteum/user/domain/BalanceGameType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.teumteum.user.domain;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import net.teumteum.user.domain.response.InterestQuestionResponse;

public enum BalanceGameType {

BALANCE("balance", (users, interestQuestion) -> interestQuestion.getBalanceGame(users)),
STORY("story", (users, interestQuestion) -> interestQuestion.getStoryGame(users)),
;

private final String value;
private final BiFunction<List<User>, InterestQuestion, InterestQuestionResponse> behavior;

BalanceGameType(String value, BiFunction<List<User>, InterestQuestion, InterestQuestionResponse> behavior) {
this.value = value;
this.behavior = behavior;
}

public static BalanceGameType of(String value) {
return Arrays.stream(BalanceGameType.values())
.filter(type -> type.value.equals(value))
.findAny()
.orElseThrow(
() -> new IllegalArgumentException("\"" + value + "\" 에 해당하는 enum값을 찾을 수 없습니다.")
);
}

public InterestQuestionResponse getInterestQuestionResponse(List<User> users, InterestQuestion interestQuestion) {
return behavior.apply(users, interestQuestion);
}
}
6 changes: 2 additions & 4 deletions src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import net.teumteum.user.domain.BalanceGameType;
import net.teumteum.user.domain.InterestQuestion;
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserRepository;
Expand Down Expand Up @@ -76,9 +77,6 @@ public InterestQuestionResponse getInterestQuestionByUserIds(List<Long> userIds,
}
);

if (type.equals("balance")) {
return interestQuestion.getBalanceGame(users);
}
return interestQuestion.getStoryGame(users);
return BalanceGameType.of(type).getInterestQuestionResponse(users, interestQuestion);
}
}

0 comments on commit 2462660

Please sign in to comment.