Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: 산책 저장 시 부여되는 뱃지 확인 로직 #368

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from

Conversation

xx10222
Copy link
Member

@xx10222 xx10222 commented Jul 11, 2023

변경한 부분

  • 누적 거리 뱃지와 누적 기록 뱃지를 enum 클래스로 관리하도록 분리
  • WalkService에 있던 뱃지 부여 로직을 BadgeService로 분리

코드리뷰 시 함께 고민해줬으면 좋겠는 부분 (?)

  • 로직 처음에 부여받을 수 있는 뱃지를 메소드를 통해 확인하는데, 받을 수 있는 뱃지가 없는 경우 어떤 동작을 해야 할지 (메소드에서 int로 뱃지 번호를 바로 반환하고 없는 경우 0이나 -1을 반환해서 service 로직에서 처리하도록 하기, exception을 던지기 근데 이건 별로인듯,, 등등 고민중)
  • 갖고 있는 뱃지와 받을 수 있는 뱃지의 간극을 계산하는 로직을 더욱 개선할 수 있을 것 같은데 당장은 아이디어가 떠오르지 않음..

완벽하진 않지만 우선 지금까지 한 부분만 올릴게용!
그리고 추가적으로 더 할 수 있으면 계속 수정하겠습니다 :)

@xx10222 xx10222 requested review from dlxortmd987 and minaver July 11, 2023 08:43
@xx10222 xx10222 self-assigned this Jul 11, 2023
@xx10222 xx10222 added the refactor refactoring code label Jul 11, 2023
Copy link
Contributor

@dlxortmd987 dlxortmd987 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 남겼습니다
두서 없이 작성해서 이해안되는 부분은 다시 알려주세요!~

public List<Integer> getAcquiredBadgeIdxList(int userIdx, Double totalDistance, Integer totalWalkCount) {
// 현재 누적 거리, 횟수로 얻을 수 있는 뱃지 계산 - 획득한 뱃지가 없을 때 어떤 동작을 해야 하지?
TotalDistanceStatus distanceStatus = TotalDistanceStatus.getDistanceBadge(totalDistance).orElseThrow();
TotalRecordStatus recordStatus = TotalRecordStatus.getWalkCountBadge(totalWalkCount).orElseThrow();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. 현재 생각나는건 enum이 현재는 2개로 분리되어 있는데(record, distance) 이것들을 하나로 합치고(뭐 DefaultBadgeStatus 등등..) 여기에 start 뱃지를 넣어서 이걸 반환하는 방법이 있을 것 같아요 (근데 이거는 좀 별로인 것 같긴 하네요.)

아니면 그냥 Optional로 놔두다가 274번째줄 이후에 둘 다 empty면 바로 반환하는 방법도 있을 것 같아요.
처음이면 1만 넣어서 반환, 아니면 빈 리스트로 반환

List<Integer> acquiredBadgeIdxList = new ArrayList<>();

// 현재 갖고 있는 뱃지 조회
List<Integer> beforeSavingWalkBadgeList = userBadgeRepository.findAllByUserIdxAndStatus(userIdx, "ACTIVE")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findAllByUserIdxAndStatus 반환 값이 Optional<List<>> 형태인데
Optional을 빼고 그냥 List<> 형태로 반환하면 어떨까요?
반환 값이 없으면 빈 리스트로 나오는게 자연스러울 것 같아서요!

}
}

return acquiredBadgeIdxList;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return acquiredBadgeIdxList;
return Collections.unmodifiableList(acquiredBadgeIdxList);

메서드 밖에서 리스트에 추가 못하게 불변 리스트로 반환하는 것도 좋을 것 같아요

int originMaxRecordBadgeIdx = 1; // 원래 갖고 있던 뱃지(6~8)의 가장 큰 값

for (Integer originBadgeIdx : beforeSavingWalkBadgeList) {
if (originBadgeIdx >= TotalDistanceStatus.minBadgeIdx() && originBadgeIdx <= TotalDistanceStatus.maxBadgeIdx()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 문 안에 있는 boolean 값을 TotalDistanceStatus 안에 넣는건 어떻게 생각하시나요?

List<Integer> beforeSavingWalkBadgeList = userBadgeRepository.findAllByUserIdxAndStatus(userIdx, "ACTIVE")
.map(userBadgeList -> userBadgeList.stream()
.map(UserBadge::getBadgeIdx)
.sorted()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정렬을 하는 이유가 뭔가요?

Comment on lines +278 to +285
for (Integer originBadgeIdx : beforeSavingWalkBadgeList) {
if (originBadgeIdx >= TotalDistanceStatus.minBadgeIdx() && originBadgeIdx <= TotalDistanceStatus.maxBadgeIdx()) {
originMaxDistanceBadgeIdx = originBadgeIdx;
}
if (originBadgeIdx >= TotalRecordStatus.minBadgeIdx() && originBadgeIdx <= TotalRecordStatus.maxBadgeIdx()) {
originMaxRecordBadgeIdx = originBadgeIdx;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞에서 beforeSavingWalkBadgeList을 만들 떄 내림차순으로 정렬하고
originMaxDistanceBadgeIdx, originMaxRecordBadgeIdx 이게 초기화 되면
바로 for 문을 나가는건 어떤가요?

Suggested change
for (Integer originBadgeIdx : beforeSavingWalkBadgeList) {
if (originBadgeIdx >= TotalDistanceStatus.minBadgeIdx() && originBadgeIdx <= TotalDistanceStatus.maxBadgeIdx()) {
originMaxDistanceBadgeIdx = originBadgeIdx;
}
if (originBadgeIdx >= TotalRecordStatus.minBadgeIdx() && originBadgeIdx <= TotalRecordStatus.maxBadgeIdx()) {
originMaxRecordBadgeIdx = originBadgeIdx;
}
}
for (Integer originBadgeIdx : beforeSavingWalkBadgeList) {
if (originBadgeIdx >= TotalDistanceStatus.minBadgeIdx() && originBadgeIdx <= TotalDistanceStatus.maxBadgeIdx()) {
originMaxDistanceBadgeIdx = originBadgeIdx;
}
else if (originBadgeIdx >= TotalRecordStatus.minBadgeIdx() && originBadgeIdx <= TotalRecordStatus.maxBadgeIdx()) {
originMaxRecordBadgeIdx = originBadgeIdx;
}
if (originMaxDistanceBadgeIdx != 1 && originMaxRecordBadgeIdx != 1) break;
}

Comment on lines +288 to +299
if(distanceStatus.getBadgeIdx() > originMaxDistanceBadgeIdx) {
for (int i = originMaxDistanceBadgeIdx + 1; i <= distanceStatus.getBadgeIdx(); i++) {
acquiredBadgeIdxList.add(i);
}
}

//거리 뱃지 확인 로직 - 2~5
if(recordStatus.getBadgeIdx() > originMaxRecordBadgeIdx) {
for (int i = originMaxRecordBadgeIdx + 1; i <= recordStatus.getBadgeIdx(); i++) {
acquiredBadgeIdxList.add(i);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안드한테 순서대로 보내려면 두 개 순서 바꿔야 할 것 같아요

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor refactoring code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants