-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionConverter.java
More file actions
153 lines (134 loc) · 6.4 KB
/
CollectionConverter.java
File metadata and controls
153 lines (134 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package learningFlow.learningFlow_BE.converter;
import learningFlow.learningFlow_BE.domain.*;
import learningFlow.learningFlow_BE.domain.enums.InterestField;
import learningFlow.learningFlow_BE.domain.enums.ResourceType;
import learningFlow.learningFlow_BE.web.dto.search.SearchRequestDTO;
import learningFlow.learningFlow_BE.web.dto.collection.CollectionResponseDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CollectionConverter {
public static List<CollectionResponseDTO.CollectionPreviewDTO> convertToHomeCollection(
List<Collection> collections
) {
return collections.stream()
.map(collection -> CollectionConverter.toCollectionPreviewDTO(
collection,
CollectionResponseDTO.CollectionLearningInfo.builder()
.learningStatus("BEFORE")
.progressRate(null)
.resourceDTOList(new ArrayList<>())
.build(),
null))
.toList();
}
public static SearchRequestDTO.SearchConditionDTO toSearchConditionDTO(
String keyword,
InterestField interestFields,
Integer preferMediaType,
List<Integer> difficulties,
List<String> amounts,
Integer sortType
) {
return SearchRequestDTO.SearchConditionDTO.builder()
.keyword(keyword)
.interestFields(interestFields)
.preferMediaType(preferMediaType)
.difficulties(difficulties)
.amounts(amounts)
.sortType(sortType)
.build();
}
public static CollectionResponseDTO.SearchResultDTO toSearchResultDTO(
List<Collection> collections,
boolean hasNext,
int totalPages,
int currentPage,
User currentUser,
Map<Long, CollectionResponseDTO.CollectionLearningInfo> learningInfoMap,
int totalCount
) {
List<CollectionResponseDTO.CollectionPreviewDTO> list = collections.stream()
.map(collection -> toCollectionPreviewDTO(
collection,
learningInfoMap.get(collection.getId()),
currentUser))
.toList();
return CollectionResponseDTO.SearchResultDTO.builder()
.searchResults(list)
.hasNext(hasNext)
.currentPage(currentPage)
.totalPages(totalPages)
.totalCount(totalCount)
.build();
}
public static CollectionResponseDTO.CollectionPreviewDTO toCollectionPreviewDTO(
Collection collection,
CollectionResponseDTO.CollectionLearningInfo learningInfo,
User currentUser
) {
Integer textCount = countResourcesByType(collection,ResourceType.TEXT);
Integer videoCount = countResourcesByType(collection, ResourceType.VIDEO);
return CollectionResponseDTO.CollectionPreviewDTO.builder()
.collectionId(collection.getId())
.imageUrl(collection.getCollectionImgUrl())
.interestField(collection.getInterestField())
.title(collection.getTitle())
.creator(collection.getCreator())
.keywords(collection.getKeywords())
.difficulties(collection.getDifficulty())
.amount(collection.getAmount())
.runtime(getTotalHours(collection))
.textCount(textCount)
.videoCount(videoCount)
.resource(learningInfo.getResourceDTOList())
.likesCount(collection.getBookmarkCount()) //북마크 -> 좋아요로 이름만 변경
.isLiked(currentUser != null && currentUser.hasBookmarked(collection.getId())) //북마크 -> 좋아요로 이름만 변경
.progressRatePercentage(learningInfo.getProgressRate())
.progressRatio(calculateProgressRatio(collection, learningInfo))
.learningStatus(learningInfo.getLearningStatus())
.startDate(learningInfo.getStartDate())
.completedDate(learningInfo.getCompletedDate())
.build();
}
private static String calculateProgressRatio(Collection collection, CollectionResponseDTO.CollectionLearningInfo learningInfo) {
if (learningInfo.getProgressRate() == null) {
return null;
}
int totalEpisodes = collection.getEpisodes().size();
int currentEpisode = "COMPLETED".equals(learningInfo.getLearningStatus())
? totalEpisodes
: learningInfo.getCurrentEpisode();
double progressPercentage = (double) currentEpisode / totalEpisodes * 100;
return String.format("%d / %d회차 (%.0f%%)",
currentEpisode,
totalEpisodes,
progressPercentage);
}
public static CollectionResponseDTO.CompletedCollectionDTO convertToCompletedCollectionDTO(
UserCollection userCollection
) {
return CollectionResponseDTO.CompletedCollectionDTO.builder()
.collectionId(userCollection.getCollection().getId())
.interestField(userCollection.getCollection().getInterestField())
.collectionTitle(userCollection.getCollection().getTitle())
.creator(userCollection.getCollection().getCreator())
.keywords(userCollection.getCollection().getKeywords())
.difficulties(userCollection.getCollection().getDifficulty())
.runtime(getTotalHours(userCollection.getCollection()))
.lastAccessedTime(userCollection.getCompletedTime())
.build();
}
private static int getTotalHours(Collection collection) {
int totalSeconds = collection.getEpisodes().stream()
.map(CollectionEpisode::getResource)
.mapToInt(Resource::getStudyDuration).sum();
return (int) Math.ceil((double) totalSeconds / 3600);
}
private static int countResourcesByType(Collection collection, ResourceType type) {
return (int) collection.getEpisodes().stream()
.map(CollectionEpisode::getResource)
.filter(resource -> resource.getType() == type)
.count();
}
}