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]: 챌린지 그룹 업데이트 코드 엔티티 책임으로 수정 #97

Merged
merged 4 commits into from
Sep 20, 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 @@ -9,6 +9,8 @@
import lombok.NoArgsConstructor;
import org.haedal.zzansuni.common.domain.BaseTimeEntity;

import java.util.List;

@Entity
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -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<ChallengeGroupCommand.UpdateChallenge> command) {
for (ChallengeGroupCommand.UpdateChallenge updateCommand : command) {
if (this.id.equals(updateCommand.getId())) {
update(updateCommand);
return true;
}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Challenge> challenges) {
this.challenges.addAll(challenges);
}
private void updateChallenges(List<ChallengeGroupCommand.UpdateChallenge> command) {
List<Challenge> removeChallenges = new ArrayList<>();

public void removeChallenges(List<Challenge> challenges) {
this.challenges.removeAll(challenges);
for (Challenge existingChallenge : this.challenges) {
if (!existingChallenge.updateChallengeIfPresent(command)) {
removeChallenges.add(existingChallenge);
}
}
this.challenges.removeAll(removeChallenges);
}

public Optional<Challenge> getChallengeById(Long challengeId) {
return this.challenges.stream()
.filter(challenge -> challenge.getId().equals(challengeId))
.findFirst();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
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;
import org.haedal.zzansuni.challengegroup.domain.port.ChallengeGroupStore;
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
Expand All @@ -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<Challenge> existingChallenges = new HashSet<>();
createChallenges(challengeGroup, command.getCreateChallenges());
updateChallenges(challengeGroup, command.getUpdateChallenges(), existingChallenges);
removeChallenges(challengeGroup, existingChallenges);
}

private void createChallenges(ChallengeGroup challengeGroup, List<ChallengeGroupCommand.CreateChallenge> createChallenges) {
List<Challenge> newChallenges = createChallenges.stream()
.map(challenge -> Challenge.create(challenge, challengeGroup))
.toList();
challengeGroup.addChallenges(newChallenges);
}

private void updateChallenges(ChallengeGroup challengeGroup, List<ChallengeGroupCommand.UpdateChallenge> challenges, Set<Challenge> 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<Challenge> existingChallenges) {
List<Challenge> removeChallenges = challengeGroup.getChallenges().stream()
.filter(challenge -> !existingChallenges.contains(challenge))
.toList();
challengeGroup.removeChallenges(removeChallenges);
}
}
Original file line number Diff line number Diff line change
@@ -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<Challenge> 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());
}
}
Loading