Skip to content

Commit 1a6f1c9

Browse files
authored
Merge pull request #235 from TeamLearningFlow/develop
Develop
2 parents ac797e9 + 9f10f49 commit 1a6f1c9

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

src/main/java/learningFlow/learningFlow_BE/converter/ResourceConverter.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import learningFlow.learningFlow_BE.domain.CollectionEpisode;
88
import learningFlow.learningFlow_BE.domain.UserCollection;
99
import java.util.*;
10+
import java.util.function.Function;
11+
import java.util.stream.Collectors;
1012

1113
public class ResourceConverter {
12-
public static ResourceResponseDTO.ResourceUrlDTO watchEpisode(Collection collection, UserEpisodeProgress userProgress, Resource resource, Optional<Memo> memo){
14+
public static ResourceResponseDTO.ResourceUrlDTO watchEpisode(Collection collection, UserEpisodeProgress userProgress, Resource resource, Optional<Memo> memo
15+
,List<UserEpisodeProgress> userEpisodeProgressList){
1316
String memoContents = "작성하신 글의 첫 줄은 노트의 제목이 됩니다, 최대 2,000자까지 입력하실 수 있어요";
1417
if (memo.isPresent())
1518
memoContents = memo.get().getContents();
@@ -21,15 +24,16 @@ public static ResourceResponseDTO.ResourceUrlDTO watchEpisode(Collection collect
2124
.urlTitle(resource.getTitle())
2225
.progress(userProgress.getCurrentProgress())
2326
.memoContents(memoContents)
24-
.episodeInformationList(episodeInformationList(collection,userProgress))
27+
.episodeInformationList(episodeInformationList(collection,userProgress, userEpisodeProgressList))
2528
.build();
2629
}
2730
public static ResourceResponseDTO.ResourceBlogUrlDTO watchBlogEpisode(
2831
Collection collection,
2932
UserEpisodeProgress userProgress,
3033
String pageResource,
3134
String resourceTitle,
32-
Optional<Memo> memo){
35+
Optional<Memo> memo,
36+
List<UserEpisodeProgress> userEpisodeProgressList){
3337
String memoContents = "작성하신 글의 첫 줄은 노트의 제목이 됩니다, 최대 2,000자까지 입력하실 수 있어요";
3438
if (memo.isPresent())
3539
memoContents = memo.get().getContents();
@@ -41,15 +45,38 @@ public static ResourceResponseDTO.ResourceBlogUrlDTO watchBlogEpisode(
4145
.urlTitle(resourceTitle)
4246
.progress(userProgress.getCurrentProgress())
4347
.memoContents(memoContents)
44-
.episodeInformationList(episodeInformationList(collection, userProgress))
48+
.episodeInformationList(episodeInformationList(collection, userProgress, userEpisodeProgressList))
4549
.build();
4650
}
4751

4852
public static List<ResourceResponseDTO.episodeInformation> episodeInformationList(
49-
Collection collection, UserEpisodeProgress userEpisodeProgress
53+
Collection collection, UserEpisodeProgress userEpisodeProgress, List<UserEpisodeProgress> userEpisodeProgressList
5054
) {
5155
List<ResourceResponseDTO.episodeInformation> episodeInformationList = new ArrayList<>();
5256

57+
// userEpisodeProgressList를 에피소드 ID를 키로 하는 Map으로 변환
58+
Map<Long, UserEpisodeProgress> progressMap = userEpisodeProgressList.stream()
59+
.collect(Collectors.toMap(
60+
progress -> progress.getUserEpisodeProgressId().getCollectionEpisodeId(),
61+
Function.identity()
62+
));
63+
64+
// 컬렉션에 속한 각 에피소드마다 진행 상태를 매핑
65+
for (CollectionEpisode episode : collection.getEpisodes()) {
66+
// 해당 에피소드에 대해 UserEpisodeProgress가 존재하면 isComplete를 가져오고,
67+
// 없으면 기본값(false)를 사용
68+
Boolean isComplete = progressMap.containsKey(episode.getId())
69+
? progressMap.get(episode.getId()).getIsComplete() : false;
70+
71+
episodeInformationList.add(new ResourceResponseDTO.episodeInformation(
72+
episode.getId(),
73+
episode.getEpisodeNumber(),
74+
episode.getResource().getTitle(),
75+
isComplete,
76+
episode.getResource().getType()
77+
));
78+
}/*
79+
// 유저 episodeProgress에서 가져올 것
5380
for (CollectionEpisode episode : collection.getEpisodes()) {
5481
episodeInformationList.add(new ResourceResponseDTO.episodeInformation(
5582
episode.getId(),
@@ -58,7 +85,7 @@ public static List<ResourceResponseDTO.episodeInformation> episodeInformationLis
5885
userEpisodeProgress.getIsComplete(),
5986
episode.getResource().getType()
6087
));
61-
}
88+
}*/
6289
episodeInformationList.sort(Comparator.comparingInt(ResourceResponseDTO.episodeInformation::getEpisodeNumber));
6390
return episodeInformationList;
6491
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package learningFlow.learningFlow_BE.repository;
22

3+
import learningFlow.learningFlow_BE.domain.Collection;
34
import learningFlow.learningFlow_BE.domain.CollectionEpisode;
45
import org.springframework.data.jpa.repository.JpaRepository;
5-
import org.springframework.data.jpa.repository.Query;
6-
import org.springframework.data.repository.query.Param;
6+
import java.util.List;
77

88
public interface CollectionEpisodeRepository extends JpaRepository<CollectionEpisode, Long> {
9+
10+
List<CollectionEpisode> findByCollection(Collection collection);
911
}

src/main/java/learningFlow/learningFlow_BE/repository/UserEpisodeProgressRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import org.springframework.data.jpa.repository.Query;
88
import org.springframework.data.repository.query.Param;
99

10+
import java.util.List;
11+
1012
public interface UserEpisodeProgressRepository extends JpaRepository<UserEpisodeProgress, UserEpisodeProgressId> {
1113
@Modifying
1214
@Query("DELETE FROM UserEpisodeProgress uep WHERE uep.id.userId = :loginId")
1315
void deleteAllByUserId(@Param("loginId") String loginId);
16+
17+
List<UserEpisodeProgress> findByUserEpisodeProgressId_UserIdAndUserEpisodeProgressId_CollectionEpisodeIdIn(String loginId, List<Long> episodeId);
1418
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.List;
1414
import java.util.Optional;
15+
import java.util.stream.Collectors;
1516

1617
@Service
1718
@RequiredArgsConstructor
@@ -70,6 +71,20 @@ public Optional<Memo> getMemoContents(Long episodeId){
7071
return memoRepository.findByEpisodeId(episodeId);
7172
}
7273

74+
@Transactional
75+
public List<UserEpisodeProgress> getEpisodeProgress(String loginId, Long episodeId){
76+
CollectionEpisode episode = collectionEpisodeRepository.findById(episodeId)
77+
.orElseThrow(() -> new ResourceHandler(ErrorStatus.EPISODE_NOT_FOUND));
78+
Collection collection = episode.getCollection();
79+
List<CollectionEpisode> episodes = collectionEpisodeRepository.findByCollection(collection);
80+
List<Long> episodeIds = episodes.stream()
81+
.map(CollectionEpisode::getId)
82+
.collect(Collectors.toList());
83+
84+
return userEpisodeProgressRepository
85+
.findByUserEpisodeProgressId_UserIdAndUserEpisodeProgressId_CollectionEpisodeIdIn(loginId, episodeIds);
86+
}
87+
7388
@Transactional
7489
public void updateUserCollection(CollectionEpisode episode, String loginId) {
7590
Collection collection = episode.getCollection();

src/main/java/learningFlow/learningFlow_BE/web/controller/ResourceRestController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.validation.annotation.Validated;
3131
import org.springframework.web.bind.annotation.*;
3232

33+
import java.util.List;
3334
import java.util.Optional;
3435

3536
@RestController
@@ -76,8 +77,9 @@ public ApiResponse<ResourceResponseDTO.ResourceUrlDTO> watchEpisode(
7677
Collection collection = resourceService.getCollection(episodeId);
7778
Optional<Memo> memo = resourceService.getMemoContents(episodeId);
7879
Resource resource = youtubeUrlEmbedService.getResource(episodeId);
80+
List<UserEpisodeProgress> episodeProgress = resourceService.getEpisodeProgress(loginId, episodeId);
7981
ResourceResponseDTO.ResourceUrlDTO response =
80-
ResourceConverter.watchEpisode(collection, userEpisodeProgress, resource, memo);
82+
ResourceConverter.watchEpisode(collection, userEpisodeProgress, resource, memo, episodeProgress);
8183
return ApiResponse.onSuccess(response);
8284
}
8385

@@ -115,8 +117,9 @@ public ApiResponse<ResourceResponseDTO.ResourceBlogUrlDTO> watchBlogEpisode(
115117
Collection collection = resourceService.getCollection(episodeId);
116118
Optional<Memo> memo = resourceService.getMemoContents(episodeId);
117119
String resourceTitle = resourceService.getResource(episodeId).getTitle();
120+
List<UserEpisodeProgress> episodeProgress = resourceService.getEpisodeProgress(loginId, episodeId);
118121
String blogSourceUrl = "/resources/" + episodeId + "/blog/content";
119-
ResourceResponseDTO.ResourceBlogUrlDTO response = ResourceConverter.watchBlogEpisode(collection, userEpisodeProgress, blogSourceUrl, resourceTitle, memo);
122+
ResourceResponseDTO.ResourceBlogUrlDTO response = ResourceConverter.watchBlogEpisode(collection, userEpisodeProgress, blogSourceUrl, resourceTitle, memo, episodeProgress);
120123
return ApiResponse.onSuccess(response);
121124
}
122125

0 commit comments

Comments
 (0)