diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/Challenge.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/Challenge.java index 8033f82..c18d0d2 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/Challenge.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/Challenge.java @@ -9,6 +9,8 @@ import lombok.NoArgsConstructor; import org.haedal.zzansuni.common.domain.BaseTimeEntity; +import java.util.List; + @Entity @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -52,13 +54,22 @@ public static Challenge create(ChallengeGroupCommand.CreateChallenge command, Ch .build(); } - public Challenge update(ChallengeGroupCommand.UpdateChallenge command) { + public void update(ChallengeGroupCommand.UpdateChallenge command) { this.onceExp = command.getOnceExp(); this.successExp = command.getSuccessExp(); this.difficulty = command.getDifficulty(); this.requiredCount = command.getRequiredCount(); this.activePeriod = command.getActivePeriod(); - return this; + } + + protected boolean updateChallengeIfPresent(List command) { + for (ChallengeGroupCommand.UpdateChallenge updateCommand : command) { + if (this.id.equals(updateCommand.getId())) { + update(updateCommand); + return true; + } + } + return false; } /** diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroup.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroup.java index 0547439..8002dee 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroup.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroup.java @@ -11,7 +11,8 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.List; -import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -82,20 +83,22 @@ public ChallengeGroup update(ChallengeGroupCommand.Update command) { this.guide = command.getGuide(); this.joinStartDate = command.getJoinStartDate(); this.joinEndDate = command.getJoinEndDate(); + updateChallenges(command.getUpdateChallenges()); + command.getCreateChallenges().stream().map(challenge -> Challenge.create(challenge,this)) + .forEach(this.challenges::add); return this; } - public void addChallenges(List challenges) { - this.challenges.addAll(challenges); - } + private void updateChallenges(List command) { + List removeChallenges = new ArrayList<>(); - public void removeChallenges(List challenges) { - this.challenges.removeAll(challenges); + for (Challenge existingChallenge : this.challenges) { + if (!existingChallenge.updateChallengeIfPresent(command)) { + removeChallenges.add(existingChallenge); + } + } + this.challenges.removeAll(removeChallenges); } - public Optional getChallengeById(Long challengeId) { - return this.challenges.stream() - .filter(challenge -> challenge.getId().equals(challengeId)) - .findFirst(); - } + } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupCommand.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupCommand.java index 50dfbac..195112f 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupCommand.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupCommand.java @@ -126,14 +126,5 @@ public UpdateChallenge(Long id, Integer requiredCount, Integer onceExp, Integer this.validateSelf(); } - public ChallengeGroupCommand.CreateChallenge convertCreate() { - return ChallengeGroupCommand.CreateChallenge.builder() - .requiredCount(requiredCount) - .onceExp(onceExp) - .successExp(successExp) - .difficulty(difficulty) - .activePeriod(activePeriod) - .build(); - } } } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/application/ChallengeGroupService.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/application/ChallengeGroupService.java index d5ef31f..fbf0715 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/application/ChallengeGroupService.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/challengegroup/domain/application/ChallengeGroupService.java @@ -1,7 +1,6 @@ package org.haedal.zzansuni.challengegroup.domain.application; import lombok.RequiredArgsConstructor; -import org.haedal.zzansuni.challengegroup.domain.Challenge; import org.haedal.zzansuni.challengegroup.domain.ChallengeGroup; import org.haedal.zzansuni.challengegroup.domain.ChallengeGroupCommand; import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupReader; @@ -9,9 +8,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.HashSet; -import java.util.List; -import java.util.Set; @Service @RequiredArgsConstructor @@ -36,32 +32,5 @@ public void deleteChallengeGroup(Long challengeGroupId) { public void updateChallengeGroup(ChallengeGroupCommand.Update command) { ChallengeGroup challengeGroup = challengeGroupReader.getById(command.getId()); challengeGroup.update(command); - - Set existingChallenges = new HashSet<>(); - createChallenges(challengeGroup, command.getCreateChallenges()); - updateChallenges(challengeGroup, command.getUpdateChallenges(), existingChallenges); - removeChallenges(challengeGroup, existingChallenges); - } - - private void createChallenges(ChallengeGroup challengeGroup, List createChallenges) { - List newChallenges = createChallenges.stream() - .map(challenge -> Challenge.create(challenge, challengeGroup)) - .toList(); - challengeGroup.addChallenges(newChallenges); - } - - private void updateChallenges(ChallengeGroup challengeGroup, List challenges, Set existingChallenges) { - for (ChallengeGroupCommand.UpdateChallenge challengeCommand : challenges) { - Challenge updateChallenge = challengeGroup.getChallengeById(challengeCommand.getId()).orElseThrow(); - updateChallenge.update(challengeCommand); - existingChallenges.add(updateChallenge); - } - } - - private void removeChallenges(ChallengeGroup challengeGroup, Set existingChallenges) { - List removeChallenges = challengeGroup.getChallenges().stream() - .filter(challenge -> !existingChallenges.contains(challenge)) - .toList(); - challengeGroup.removeChallenges(removeChallenges); } } diff --git a/zzansuni-api-server/app/src/test/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupTest.java b/zzansuni-api-server/app/src/test/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupTest.java new file mode 100644 index 0000000..a05dbe4 --- /dev/null +++ b/zzansuni-api-server/app/src/test/java/org/haedal/zzansuni/challengegroup/domain/ChallengeGroupTest.java @@ -0,0 +1,90 @@ +package org.haedal.zzansuni.challengegroup.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class ChallengeGroupTest { + + @Test + @DisplayName("챌린지 그룹 업데이트가 정상적으로 동작한다.") + void update() { + // given + // 챌린지 그룹1, 챌린지1,2,3 생성 + List challenges = new ArrayList<>(); + ChallengeGroup challengeGroup = ChallengeGroup.builder() + .id(1L) + .challenges(challenges) + .build(); + + Challenge challenge1 = Challenge.builder() + .id(1L) + .challengeGroup(challengeGroup) + .build(); + + Challenge challenge2 = Challenge.builder() + .id(2L) + .challengeGroup(challengeGroup) + .build(); + Challenge challenge3 = Challenge.builder() + .id(3L) + .challengeGroup(challengeGroup) + .build(); + + challenges.add(challenge1); + challenges.add(challenge2); + challenges.add(challenge3); + + + // 챌린지 update 1,2,3 생성 + ChallengeGroupCommand.UpdateChallenge updateChallenge1 = ChallengeGroupCommand.UpdateChallenge.builder() + .id(1L) + .requiredCount(1) + .onceExp(1) + .successExp(1) + .difficulty(1) + .activePeriod(1) + .build(); + + ChallengeGroupCommand.UpdateChallenge updateChallenge2 = ChallengeGroupCommand.UpdateChallenge.builder() + .id(2L) + .requiredCount(2) + .onceExp(2) + .successExp(2) + .difficulty(2) + .activePeriod(2) + .build(); + + ChallengeGroupCommand.UpdateChallenge updateChallenge3 = ChallengeGroupCommand.UpdateChallenge.builder() + .id(3L) + .requiredCount(3) + .onceExp(3) + .successExp(3) + .difficulty(3) + .activePeriod(3) + .build(); + + ChallengeGroupCommand.Update command = ChallengeGroupCommand.Update.builder() + .id(1L) + .title("title") + .content("content") + .guide("guide") + .joinStartDate(LocalDate.now()) + .joinEndDate(LocalDate.now().plusDays(1)) + .category(ChallengeCategory.ETC) + .createChallenges(List.of()) + .updateChallenges(List.of(updateChallenge1, updateChallenge2, updateChallenge3)) + .build(); + + // when + challengeGroup.update(command); + + // then + assertEquals(3, challengeGroup.getChallenges().size()); + } +} \ No newline at end of file