Skip to content

Commit

Permalink
[Fix]: 챌린지 그룹 업데이트 중복 체크 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonssshyeon committed Sep 19, 2024
1 parent dd4d145 commit bf32bef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
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 @@ -61,6 +63,16 @@ public Challenge update(ChallengeGroupCommand.UpdateChallenge command) {
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;
}

/**
* 챌린지 그룹 아이디 반환
* FK는 LAZY 여도 이미 영속성 컨텍스트에 존재하므로 추가 조회 없이 바로 접근 가능
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -87,15 +89,12 @@ public ChallengeGroup update(ChallengeGroupCommand.Update command) {
return this;
}

public void updateChallenges(List<ChallengeGroupCommand.UpdateChallenge> command) {
private 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);
}
if (!existingChallenge.updateChallengeIfPresent(command)) {
removeChallenges.add(existingChallenge);
}
}
this.challenges.removeAll(removeChallenges);
Expand Down

0 comments on commit bf32bef

Please sign in to comment.