Skip to content

Commit

Permalink
[Fix]: 챌린지 그룹 업데이트 코드 엔티티 책임으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonssshyeon committed Sep 17, 2024
1 parent 9afc209 commit b8aedbd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -82,20 +81,25 @@ 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);
public void updateChallenges(List<ChallengeGroupCommand.UpdateChallenge> command) {
List<Challenge> removeChallenges = new ArrayList<>();
for (Challenge existingChallenge : this.challenges) {
for (ChallengeGroupCommand.UpdateChallenge updateCommand : command) {
if (existingChallenge.getId().equals(updateCommand.getId())) {
existingChallenge.update(updateCommand);
} else {
removeChallenges.add(existingChallenge);
}
}
}
this.challenges.removeAll(removeChallenges);
}

public void removeChallenges(List<Challenge> challenges) {
this.challenges.removeAll(challenges);
}

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

0 comments on commit b8aedbd

Please sign in to comment.