Skip to content

Commit d32b032

Browse files
authored
Merge pull request #217 from TeamLearningFlow/develop
Develop
2 parents a3d3511 + fc4d9d2 commit d32b032

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/main/java/learningFlow/learningFlow_BE/apiPayload/code/status/ErrorStatus.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public enum ErrorStatus implements BaseErrorCode {
7575
// Resource 에러
7676
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "RESOURCE4001", "존재하지 않는 리소스 입니다."),
7777

78+
USER_COLLECTION_NOT_FOUND(HttpStatus.NOT_FOUND, "USER COLLECTION4001", "존재하지 않는 리소스 입니다."),
79+
7880
// User Progress
7981
USER_PROGRESS_NOT_FOUND(HttpStatus.NOT_FOUND, "USER-PROGRESS4001", "진도 값이 null 인 상태입니다.");
8082
private final HttpStatus httpStatus;

src/main/java/learningFlow/learningFlow_BE/domain/UserCollection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ public void updateUserCollection(Integer userCollectionStatus) {
7575
this.userCollectionStatus = userCollectionStatus;
7676
this.completedTime = LocalDate.now();
7777
}
78+
public void CompleteUserCollection(){
79+
this.status = UserCollectionStatus.COMPLETED;
80+
this.completedTime = LocalDate.now();
81+
}
7882
}
7983

src/main/java/learningFlow/learningFlow_BE/service/resource/ResourceService.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.stereotype.Service;
1111
import org.springframework.transaction.annotation.Transactional;
1212

13+
import java.util.List;
1314
import java.util.Optional;
1415

1516
@Service
@@ -71,7 +72,6 @@ public Optional<Memo> getMemoContents(Long episodeId){
7172

7273
@Transactional
7374
public void updateUserCollection(CollectionEpisode episode, String loginId) {
74-
// UserCollection 조회
7575
Collection collection = episode.getCollection();
7676
User user = userRepository.findById(loginId)
7777
.orElseThrow(() -> new ResourceHandler(ErrorStatus.USER_NOT_FOUND));
@@ -97,7 +97,10 @@ public Boolean saveProgress(ResourceRequestDTO.ProgressRequestDTO request, Strin
9797
.orElseThrow(() -> new ResourceHandler(ErrorStatus.USER_PROGRESS_NOT_FOUND));
9898
// 만약 진도가 80이상인 경우 완료로 저장
9999
Integer requestProgress = request.getProgress();
100-
if (requestProgress >= 80) userEpisode.setIsComplete(true);
100+
if (requestProgress >= 80) {
101+
userEpisode.setIsComplete(true);
102+
checkUserCollectionComplete(episodeId, userId);
103+
}
101104
userEpisode.setCurrentProgress(requestProgress);
102105
userEpisodeProgressRepository.save(userEpisode);
103106
return userEpisode.getIsComplete();
@@ -115,4 +118,30 @@ public Boolean changeEpisodeComplete(Long episodeId, String loginId){
115118
userEpisodeProgressRepository.save(userEpisodeProgress);
116119
return isComplete;
117120
}
121+
122+
@Transactional
123+
public void checkUserCollectionComplete(Long episodeId, String loginId){
124+
Collection collection = collectionEpisodeRepository.findById(episodeId)
125+
.orElseThrow(() -> new ResourceHandler(ErrorStatus.EPISODE_NOT_FOUND))
126+
.getCollection();
127+
User user = userRepository.findById(loginId)
128+
.orElseThrow(() -> new ResourceHandler(ErrorStatus.USER_NOT_FOUND));
129+
UserCollection userCollection = userCollectionRepository.findByUserAndCollection(user, collection)
130+
.orElseThrow(() -> new ResourceHandler(ErrorStatus.USER_COLLECTION_NOT_FOUND));
131+
List<CollectionEpisode> episodes = collection.getEpisodes();
132+
boolean allCompleted = true;
133+
for (CollectionEpisode episode : episodes) {
134+
UserEpisodeProgressId userEpisodeProgressId = new UserEpisodeProgressId(episode.getId(), loginId);
135+
UserEpisodeProgress userEpisodeProgress = userEpisodeProgressRepository.findById(userEpisodeProgressId)
136+
.orElseThrow(() -> new ResourceHandler(ErrorStatus.USER_PROGRESS_NOT_FOUND));
137+
if (!userEpisodeProgress.getIsComplete().equals(Boolean.TRUE)) {
138+
allCompleted = false;
139+
break;
140+
}
141+
}
142+
if (allCompleted){
143+
userCollection.CompleteUserCollection();
144+
userCollectionRepository.save(userCollection);
145+
}
146+
}
118147
}

0 commit comments

Comments
 (0)