Skip to content

Commit

Permalink
Feat: badge_achievement unique 중복 무시하고 저장하는 함수 추가 (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewon-pro authored Oct 28, 2024
1 parent 3ef36da commit 5050c2c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public interface BadgeAchievementRepository {

BadgeAchievement save(BadgeAchievement badgeAchievement);

void saveAllIgnoreDuplicated(List<BadgeAchievement> badgeAchievements);

void deleteByMemberId(long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public BadgeAchievement save(BadgeAchievement badgeAchievement) {
.toDomain(badgeAchievement.badge());
}

@Override
public void saveAllIgnoreDuplicated(List<BadgeAchievement> badgeAchievements) {
jooqBadgeAchievementRepository.saveAllIgnoreDuplicated(badgeAchievements);
}

@Override
public void deleteByMemberId(long memberId) {
jpaBadgeAchievementRepository.deleteByMemberId(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.dnd.runus.domain.badge.BadgeAchievement;
import lombok.RequiredArgsConstructor;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.springframework.stereotype.Repository;

import java.time.OffsetDateTime;
Expand All @@ -24,17 +22,26 @@ public List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadge(long memberId) {
.join(BADGE)
.on(BADGE_ACHIEVEMENT.BADGE_ID.eq(BADGE.ID))
.where(BADGE_ACHIEVEMENT.MEMBER_ID.eq(memberId))
.fetch(new BadgeAchievementMapper());
.fetch(badge -> new BadgeAchievement.OnlyBadge(
badge.get(BADGE_ACHIEVEMENT.ID),
new JooqBadgeMapper().map(badge),
badge.get(BADGE_ACHIEVEMENT.CREATED_AT, OffsetDateTime.class),
badge.get(BADGE_ACHIEVEMENT.UPDATED_AT, OffsetDateTime.class)));
}

private static class BadgeAchievementMapper implements RecordMapper<Record, BadgeAchievement.OnlyBadge> {
@Override
public BadgeAchievement.OnlyBadge map(Record record) {
return new BadgeAchievement.OnlyBadge(
record.get(BADGE_ACHIEVEMENT.ID),
new JooqBadgeMapper().map(record),
record.get(BADGE_ACHIEVEMENT.CREATED_AT, OffsetDateTime.class),
record.get(BADGE_ACHIEVEMENT.UPDATED_AT, OffsetDateTime.class));
}
public void saveAllIgnoreDuplicated(List<BadgeAchievement> badgeAchievements) {
dsl.batch(badgeAchievements.stream()
.map(badgeAchievement -> dsl.insertInto(BADGE_ACHIEVEMENT)
.set(
BADGE_ACHIEVEMENT.BADGE_ID,
badgeAchievement.badge().badgeId())
.set(
BADGE_ACHIEVEMENT.MEMBER_ID,
badgeAchievement.member().memberId())
.set(BADGE_ACHIEVEMENT.CREATED_AT, badgeAchievement.createdAt())
.set(BADGE_ACHIEVEMENT.UPDATED_AT, badgeAchievement.updatedAt())
.onConflictDoNothing())
.toList())
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import com.dnd.runus.global.constant.BadgeType;
import com.dnd.runus.global.constant.MemberRole;
import com.dnd.runus.infrastructure.persistence.annotation.RepositoryTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.dnd.runus.infrastructure.persistence.jpa.badge.entity.BadgeAchievementEntity;
import com.dnd.runus.infrastructure.persistence.jpa.badge.entity.BadgeEntity;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@RepositoryTest
class BadgeAchievementRepositoryImplTest {
Expand Down Expand Up @@ -47,4 +50,93 @@ public void deleteByMember() {
.findById(badgeAchievement.badgeAchievementId())
.isPresent());
}

@Nested
@DisplayName("BadgeAchievement 저장 테스트")
class BadgeAchievementSaveTest {
@Autowired
private EntityManager entityManager;

private Badge badge1;
private Badge badge2;

private BadgeAchievement badgeAchievement1;
private BadgeAchievement badgeAchievement2;

@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));

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

badge1 = badgeEntity1.toDomain();
badge2 = badgeEntity2.toDomain();

badgeAchievement1 = new BadgeAchievement(badge1, savedMember);
badgeAchievement2 = new BadgeAchievement(badge2, savedMember);

badgeAchievementRepository.save(badgeAchievement1);
badgeAchievementRepository.save(badgeAchievement2);
}

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

@Test
@DisplayName("saveAllIgnoreDuplicated: 중복된 데이터가 없을 때 모든 데이터를 저장한다.")
void saveAllIgnoreDuplicated() {
// given
List<BadgeAchievement> badgeAchievements = List.of(badgeAchievement1, badgeAchievement2);

// when
badgeAchievementRepository.saveAllIgnoreDuplicated(badgeAchievements);

// then
List<BadgeAchievement> achievements = entityManager
.createQuery("select ba from badge_achievement ba", BadgeAchievementEntity.class)
.getResultList()
.stream()
.map(BadgeAchievementEntity::toDomain)
.toList();

assertFalse(achievements.isEmpty());

assertEquals(2, achievements.size());
assertTrue(achievements.stream().anyMatch(ba -> ba.badge().badgeId() == badge1.badgeId()));
assertTrue(achievements.stream().anyMatch(ba -> ba.badge().badgeId() == badge2.badgeId()));
}

@Test
@DisplayName("saveAllIgnoreDuplicated: 중복된 데이터가 있을 때 중복된 데이터는 무시하고 저장한다.")
void saveAllIgnoreDuplicated_case_duplicated() {
// given
List<BadgeAchievement> badgeAchievements = List.of(badgeAchievement1, badgeAchievement2);

// when
// 저장 여러번 시도
badgeAchievementRepository.saveAllIgnoreDuplicated(badgeAchievements);
badgeAchievementRepository.saveAllIgnoreDuplicated(badgeAchievements);

// then
List<BadgeAchievement> achievements = entityManager
.createQuery("select ba from badge_achievement ba", BadgeAchievementEntity.class)
.getResultList()
.stream()
.map(BadgeAchievementEntity::toDomain)
.toList();

assertFalse(achievements.isEmpty());

assertEquals(2, achievements.size());
assertTrue(achievements.stream().anyMatch(ba -> ba.badge().badgeId() == badge1.badgeId()));
assertTrue(achievements.stream().anyMatch(ba -> ba.badge().badgeId() == badge2.badgeId()));
}
}
}

0 comments on commit 5050c2c

Please sign in to comment.