Skip to content

Commit

Permalink
Fix: 나의 기록 뱃지 조회를 최신 기준으로 최대 3개 리턴하도록 수정 (#288)
Browse files Browse the repository at this point in the history
* Fix: 나의 뱃지 조회를 획득한 날짜 최신 순으로 최대 3개만 가져오도록 repository 함수 수정

* Fix: 뱃지 조회 repository 함수 명을 명확하게 변경, limit 인자 추가

- findByMemberIdWithBadge에서 findByMemberIdWithBadgeOrderByAchievedAtLimit으로 변경

* Test: 나의 기록의 뱃지 조회 findByMemberIdWithBadgeOrderByAchievedAtLimit 함수 테스트 코드 추가
  • Loading branch information
hee9841 authored Oct 29, 2024
1 parent 0312d42 commit 1c4fada
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
15 changes: 8 additions & 7 deletions src/main/java/com/dnd/runus/application/badge/BadgeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ public void achieveBadge(Member member, BadgeType badgeType, int value) {
}

public AchievedBadgesResponse getAchievedBadges(long memberId) {
return new AchievedBadgesResponse(badgeAchievementRepository.findByMemberIdWithBadge(memberId).stream()
.map(badgeAchievement -> new AchievedBadgesResponse.AchievedBadge(
badgeAchievement.badge().badgeId(),
badgeAchievement.badge().name(),
badgeAchievement.badge().imageUrl(),
badgeAchievement.createdAt().toLocalDateTime()))
.toList());
return new AchievedBadgesResponse(
badgeAchievementRepository.findByMemberIdWithBadgeOrderByAchievedAtLimit(memberId, 3).stream()
.map(badgeAchievement -> new AchievedBadgesResponse.AchievedBadge(
badgeAchievement.badge().badgeId(),
badgeAchievement.badge().name(),
badgeAchievement.badge().imageUrl(),
badgeAchievement.createdAt().toLocalDateTime()))
.toList());
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface BadgeAchievementRepository {

Optional<BadgeAchievement> findById(long id);

List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadge(long memberId);
List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadgeOrderByAchievedAtLimit(long memberId, int limit);

BadgeAchievement save(BadgeAchievement badgeAchievement);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public Optional<BadgeAchievement> findById(long id) {
}

@Override
public List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadge(long memberId) {
return jooqBadgeAchievementRepository.findByMemberIdWithBadge(memberId);
public List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadgeOrderByAchievedAtLimit(long memberId, int limit) {
return jooqBadgeAchievementRepository.findByMemberIdWithBadgeOrderByAchievedAtLimit(memberId, limit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
public class JooqBadgeAchievementRepository {
private final DSLContext dsl;

public List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadge(long memberId) {
public List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadgeOrderByAchievedAtLimit(long memberId, int limit) {
return dsl.select()
.from(BADGE_ACHIEVEMENT)
.join(BADGE)
.on(BADGE_ACHIEVEMENT.BADGE_ID.eq(BADGE.ID))
.where(BADGE_ACHIEVEMENT.MEMBER_ID.eq(memberId))
.orderBy(BADGE_ACHIEVEMENT.CREATED_AT.desc())
.limit(limit)
.fetch(badge -> new BadgeAchievement.OnlyBadge(
badge.get(BADGE_ACHIEVEMENT.ID),
new JooqBadgeMapper().map(badge),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void getAchievedBadges() {
Badge badge1 = new Badge(1L, "badge1", "description", "imageUrl1", BadgeType.STREAK, 100);
Badge badge2 = new Badge(2L, "badge2", "description", "imageUrl2", BadgeType.DISTANCE_METER, 1000);

given(badgeAchievementRepository.findByMemberIdWithBadge(1L))
given(badgeAchievementRepository.findByMemberIdWithBadgeOrderByAchievedAtLimit(1L, 3))
.willReturn(List.of(
new BadgeAchievement.OnlyBadge(1, badge1, OffsetDateTime.now(), OffsetDateTime.now()),
new BadgeAchievement.OnlyBadge(2, badge2, OffsetDateTime.now(), OffsetDateTime.now())));
Expand All @@ -68,7 +68,8 @@ void getAchievedBadges() {
@DisplayName("자신이 획득한 뱃지가 없다면, 뱃지가 없는 응답을 반환한다.")
void getAchievedBadges_Empty() {
// given
given(badgeAchievementRepository.findByMemberIdWithBadge(1L)).willReturn(List.of());
given(badgeAchievementRepository.findByMemberIdWithBadgeOrderByAchievedAtLimit(1L, 3))
.willReturn(List.of());

// when
AchievedBadgesResponse achievedBadgesResponse = badgeService.getAchievedBadges(1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dnd.runus.domain.badge.Badge;
import com.dnd.runus.domain.badge.BadgeAchievement;
import com.dnd.runus.domain.badge.BadgeAchievement.OnlyBadge;
import com.dnd.runus.domain.badge.BadgeAchievementRepository;
import com.dnd.runus.domain.member.Member;
import com.dnd.runus.domain.member.MemberRepository;
Expand All @@ -14,6 +15,7 @@
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.OffsetDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -51,6 +53,72 @@ public void deleteByMember() {
.isPresent());
}

@Nested
@DisplayName("BadeAchievement 조회 테스트")
class BadgeAchievementFindTest {
@Autowired
private EntityManager entityManager;

private Badge badge1;
private Badge badge2;
private Badge badge3;
private Badge badge4;

@BeforeEach
void beforeEach() {
BadgeEntity badgeEntity1 =
BadgeEntity.from(new Badge(0L, "testBadge1", "testBadge1", "tesUrl1", BadgeType.DISTANCE_METER, 0));
BadgeEntity badgeEntity2 =
BadgeEntity.from(new Badge(0L, "testBadge2", "testBadge2", "tesUrl2", BadgeType.DISTANCE_METER, 2));

BadgeEntity badgeEntity3 =
BadgeEntity.from(new Badge(0L, "testBadge3", "testBadge3", "tesUrl3", BadgeType.DISTANCE_METER, 3));

BadgeEntity badgeEntity4 =
BadgeEntity.from(new Badge(0L, "testBadge4", "testBadge4", "tesUrl4", BadgeType.DISTANCE_METER, 4));

entityManager.persist(badgeEntity1);
entityManager.persist(badgeEntity2);
entityManager.persist(badgeEntity3);
entityManager.persist(badgeEntity4);

badge1 = badgeEntity1.toDomain();
badge2 = badgeEntity2.toDomain();
badge3 = badgeEntity3.toDomain();
badge4 = badgeEntity4.toDomain();
}

@AfterEach
void afterEach() {
entityManager.createQuery("delete from badge_achievement").executeUpdate();
entityManager.createQuery("delete from badge").executeUpdate();
}

@Test
@DisplayName("획득한 뱃지를 조회한다: 획득한 최신 순, 최대 3개의 데이터를 리턴한다.")
void findByMemberIdWithBadgeOrderByAchievedAtLimit() {
// given
badgeAchievementRepository.save(new BadgeAchievement(badge1, savedMember));
badgeAchievementRepository.save(new BadgeAchievement(badge2, savedMember));
badgeAchievementRepository.save(new BadgeAchievement(badge3, savedMember));
badgeAchievementRepository.save(new BadgeAchievement(badge4, savedMember));

// when
List<OnlyBadge> byMemberIdWithBadgeList =
badgeAchievementRepository.findByMemberIdWithBadgeOrderByAchievedAtLimit(savedMember.memberId(), 3);

// then
assertEquals(3, byMemberIdWithBadgeList.size());

OffsetDateTime achievedAt1 = byMemberIdWithBadgeList.get(0).createdAt();
OffsetDateTime achievedAt2 = byMemberIdWithBadgeList.get(1).createdAt();
OffsetDateTime achievedAt3 = byMemberIdWithBadgeList.get(2).createdAt();

assertTrue(achievedAt1.isAfter(achievedAt2));
assertTrue(achievedAt2.isAfter(achievedAt3));
}
}

@Nested
@DisplayName("BadgeAchievement 저장 테스트")
class BadgeAchievementSaveTest {
Expand Down

0 comments on commit 1c4fada

Please sign in to comment.