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

[FIX] 배지 획득까지 남은 횟수 반환 로직 수정 #291

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.smeem.domain.badge.adapter.BadgeFinder;
import com.smeem.domain.badge.model.Badge;
import com.smeem.domain.badge.model.BadgeType;
import com.smeem.domain.member.adapter.memberbadge.MemberBadgeCounter;
import com.smeem.domain.member.adapter.member.MemberFinder;
import com.smeem.domain.member.adapter.memberbadge.MemberBadgeFinder;
import com.smeem.domain.member.adapter.memberbadge.MemberBadgeSaver;
import com.smeem.domain.member.model.Member;
Expand All @@ -26,7 +26,7 @@ public class BadgeService {
private final BadgeFinder badgeFinder;
private final MemberBadgeSaver memberBadgeSaver;
private final MemberBadgeFinder memberBadgeFinder;
private final MemberBadgeCounter memberBadgeCounter;
private final MemberFinder memberFinder;

public BadgeListServiceResponseV3 getBadgesV3(BadgeListServiceRequestV3 request) {
val response = badgeFinder.findAllOrderById()
Expand Down Expand Up @@ -71,20 +71,21 @@ private void putBadgeMap(Map<BadgeType, List<Badge>> badgeMap, Badge badge) {
badgeMap.computeIfAbsent(badge.getType(), k -> new ArrayList<>()).add(badge);
}

private Integer calculateRemainingNumber(Long badgeId, Long count) {
private Integer calculateRemainingNumber(Long badgeId, int count) {
return switch (badgeId.intValue()) {
case 2 -> 1 - count.intValue() > 0 ? 1 - count.intValue() : null;
case 3 -> 10 - count.intValue() > 0 ? 10 - count.intValue() : null;
case 4 -> 30 - count.intValue() > 0 ? 30 - count.intValue() : null;
case 5 -> 50 - count.intValue() > 0 ? 50 - count.intValue() : null;
case 2 -> 1 - count;
case 3 -> 10 - count;
case 4 -> 30 - count;
case 5 -> 50 - count;
default -> null;
};
}

private BadgeGetServiceResponseV3 convertToBadgeGetServiceResponseV3(Badge badge, final long memberId) {
val count = memberBadgeCounter.countByBadgeIdAndMemberId(badge.getId(), memberId);
val hasBadge = count > 0;
val remainingNumber = calculateRemainingNumber(badge.getId(), count);
val member = memberFinder.findById(memberId);
val hasBadge = memberBadgeFinder.isExistByMemberAndBadge(member, badge);
val memberDiaryCount = member.getDiaries().size();
val remainingNumber = calculateRemainingNumber(badge.getId(), memberDiaryCount);
return BadgeGetServiceResponseV3.of(badge, hasBadge, remainingNumber);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.smeem.domain.member.adapter.memberbadge;


import com.smeem.domain.badge.model.Badge;
import com.smeem.domain.member.repository.MemberBadgeRepository;
import com.smeem.domain.support.RepositoryAdapter;
Expand All @@ -15,8 +14,4 @@ public class MemberBadgeCounter {
public long countByBadge(final Badge badge) {
return memberBadgeRepository.countByBadge(badge);
}

public long countByBadgeIdAndMemberId(final long badgeId, final long memberId) {
return memberBadgeRepository.countByBadgeIdAndMemberId(badgeId, memberId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.smeem.domain.member.adapter.memberbadge;

import com.smeem.common.code.failure.MemberFailureCode;
import com.smeem.domain.badge.model.Badge;
import com.smeem.domain.member.exception.MemberException;
import com.smeem.domain.member.model.Member;
import com.smeem.domain.member.model.MemberBadge;
import com.smeem.domain.member.repository.MemberBadgeRepository;
import com.smeem.domain.support.RepositoryAdapter;
Expand All @@ -24,4 +26,8 @@ public MemberBadge findFirstByMemberIdOrderByCreatedAtDesc(final long id) {
.orElseThrow(() -> new MemberException(MemberFailureCode.EMPTY_MEMBER));
}

public boolean isExistByMemberAndBadge(Member member, Badge badge) {
return memberBadgeRepository.existsByMemberAndBadge(member, badge);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.smeem.domain.member.repository;

import com.smeem.domain.badge.model.Badge;
import com.smeem.domain.member.model.Member;
import com.smeem.domain.member.model.MemberBadge;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -11,8 +12,6 @@ public interface MemberBadgeRepository extends JpaRepository<MemberBadge, Long>

List<MemberBadge> findAllByMemberId(final long memberId);
Optional<MemberBadge> findFirstByMemberIdOrderByCreatedAtDesc(final long memberId);

long countByBadge(Badge badge);

long countByBadgeIdAndMemberId(final long badgeId, final long memberId);
boolean existsByMemberAndBadge(Member member, Badge badge);
}
Loading