1010import org .springframework .stereotype .Service ;
1111import org .springframework .transaction .annotation .Transactional ;
1212
13+ import java .util .List ;
1314import 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