Skip to content

Commit 1396488

Browse files
Merge pull request #104 from TeamLearningFlow/develop
cors 수정
2 parents 41dbe16 + d58eec5 commit 1396488

15 files changed

Lines changed: 138 additions & 123 deletions

File tree

src/main/generated/learningFlow/learningFlow_BE/domain/uuid/QUuid.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/main/java/learningFlow/learningFlow_BE/config/CorsConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public CorsFilter corsFilter() {
1717
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
1818
CorsConfiguration config = new CorsConfiguration();
1919
config.setAllowCredentials(true);
20-
config.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:8081")); // ✅ Swagger 포함
20+
config.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:8081", "http://localhost:8080", "http://onboarding.p-e.kr:8080", "http://54.180.118.227")); // ✅ Swagger 포함
2121
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
2222
config.setAllowedHeaders(List.of("*"));
2323
config.setMaxAge(3600L);

src/main/java/learningFlow/learningFlow_BE/config/security/SecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public PasswordEncoder passwordEncoder() {
8989
@Bean
9090
public CorsConfigurationSource corsConfigurationSource() {
9191
CorsConfiguration configuration = new CorsConfiguration();
92-
configuration.setAllowedOrigins(List.of("http://localhost:3000")); // 프론트엔드 주소
92+
configuration.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:8080", "http://onboarding.p-e.kr:8080", "http://54.180.118.227")); // 프론트엔드 주소
9393
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
9494
configuration.setAllowedHeaders(List.of("*"));
9595
configuration.setAllowCredentials(true);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public static CollectionResponseDTO.CollectionPreviewDTO toCollectionPreviewDTO(
9797
.progressRatePercentage(learningInfo.getProgressRate())
9898
.progressRatio(calculateProgressRatio(collection, learningInfo))
9999
.learningStatus(learningInfo.getLearningStatus())
100-
.completedTime(learningInfo.getCompletedTime())
100+
.startDate(learningInfo.getStartDate())
101+
.completedDate(learningInfo.getCompletedDate())
101102
.build();
102103
}
103104

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static HomeResponseDTO.GuestHomeInfoDTO convertToGuestHomeInfoDTO(
1919
}
2020

2121
public static HomeResponseDTO.UserHomeInfoDTO convertToUserHomeInfoDTO(
22-
HomeResponseDTO.RecentLearningDTO recentLearning,
22+
CollectionResponseDTO.CollectionPreviewDTO recentLearning,
2323
List<Collection> recommendedCollections,
2424
User user,
2525
int size,

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,6 @@ public static ResourceResponseDTO.SearchResultResourceDTO convertToResourceDTO(C
7272
.build();
7373
}
7474

75-
public static HomeResponseDTO.RecentLearningDTO toRecentLearningDTO(UserCollection userCollection) {
76-
Collection collection = userCollection.getCollection();
77-
int currentEpisode = userCollection.getUserCollectionStatus();
78-
79-
List<ResourceResponseDTO.SearchResultResourceDTO> resources = collection.getEpisodes().stream()
80-
.filter(episode -> episode.getEpisodeNumber() <= currentEpisode)
81-
.map(episode -> ResourceResponseDTO.SearchResultResourceDTO.builder()
82-
.resourceId(episode.getResource().getId())
83-
.episodeName(episode.getEpisodeName())
84-
.url(episode.getResource().getUrl())
85-
.resourceSource(extractResourceSource(episode.getResource().getUrl()))
86-
.episodeNumber(episode.getEpisodeNumber())
87-
.build())
88-
.toList();
89-
90-
CollectionResponseDTO.CompletedCollectionDTO completedCollectionDTO
91-
= CollectionConverter.convertToCompletedCollectionDTO(userCollection);
92-
93-
return HomeResponseDTO.RecentLearningDTO.builder()
94-
.collection(completedCollectionDTO)
95-
.resources(resources)
96-
.progressRatio(calculateProgressRatio(userCollection))
97-
.build();
98-
}
99-
10075
public static ResourceResponseDTO.RecentlyWatchedEpisodeDTO convertToRecentlyWatchedEpisodeDTO(
10176
UserCollection userCollection,
10277
UserEpisodeProgress userEpisodeProgress

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,25 @@ public static UserInfoDTO convertToUserInfoDTO(User user) {
3434
}
3535

3636
public static UserResponseDTO.UserMyPageResponseDTO convertToUserMyPageResponseDTO(
37+
User user,
3738
List<ResourceResponseDTO.RecentlyWatchedEpisodeDTO> recentlyWatchedEpisodeDTOList,
38-
List<UserCollection> completedUserCollectionList
39+
List<CollectionResponseDTO.CollectionPreviewDTO> completedCollectionList
3940
) {
4041

41-
List<CollectionResponseDTO.CompletedCollectionDTO> completedCollectionList = completedUserCollectionList.stream()
42-
.map(CollectionConverter::convertToCompletedCollectionDTO).toList();
43-
4442
return UserResponseDTO.UserMyPageResponseDTO.builder()
43+
.userPreviewDTO(convertToUserPreviewDTO(user))
4544
.recentlyWatchedEpisodeList(recentlyWatchedEpisodeDTOList)
4645
.completedCollectionList(completedCollectionList)
4746
.build();
4847
}
48+
49+
private static UserResponseDTO.UserPreviewDTO convertToUserPreviewDTO(User user) {
50+
51+
return UserResponseDTO.UserPreviewDTO.builder()
52+
.name(user.getName())
53+
.email(user.getEmail())
54+
.job(user.getJob().getDescription())
55+
.profileImgUrl(user.getProfileImgUrl())
56+
.build();
57+
}
4958
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.time.LocalDateTime;
1212

13+
@Getter
1314
@MappedSuperclass
1415
@EntityListeners(AuditingEntityListener.class)
1516
public abstract class BaseEntity {

src/main/java/learningFlow/learningFlow_BE/repository/collection/CollectionRepository.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@
22

33
import learningFlow.learningFlow_BE.domain.Collection;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
57

68
import java.util.List;
9+
import java.util.Optional;
710

811
public interface CollectionRepository extends JpaRepository<Collection, Long>, CollectionRepositoryCustom {
912
List<Collection> findByIdIn(List<Long> ids);
13+
14+
//N+1 문제 개선을 위한 패치 조인 쿼리
15+
@Query("SELECT DISTINCT c FROM Collection c " +
16+
"LEFT JOIN FETCH c.episodes e " +
17+
"LEFT JOIN FETCH e.resource " +
18+
"WHERE c.id = :collectionId")
19+
Optional<Collection> findByIdWithEpisodesAndResources(@Param("collectionId") Long collectionId);
20+
21+
@Query("SELECT DISTINCT c FROM Collection c " +
22+
"LEFT JOIN FETCH c.episodes e " +
23+
"LEFT JOIN FETCH e.resource " +
24+
"WHERE c.id IN :collectionIds")
25+
List<Collection> findByIdInWithEpisodesAndResources(@Param("collectionIds") List<Long> collectionIds);
26+
1027
}

src/main/java/learningFlow/learningFlow_BE/service/collection/CollectionService.java

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,31 @@ public CollectionResponseDTO.CollectionPreviewDTO CollectionDetails(Long collect
4343

4444
Authentication authentication = (principalDetails != null) ? SecurityContextHolder.getContext().getAuthentication() : null;
4545

46-
Collection collection = collectionRepository.findById(collectionId)
46+
Collection collection = collectionRepository.findByIdWithEpisodesAndResources(collectionId) //N+1 문제 개선
4747
.orElseThrow(() -> new CollectionHandler(ErrorStatus.COLLECTION_NOT_FOUND));
4848

4949
User currentUser = null;
5050
if (authentication != null && authentication.getPrincipal() instanceof PrincipalDetails) {
5151
currentUser = ((PrincipalDetails) authentication.getPrincipal()).getUser();
5252
}
5353

54-
CollectionResponseDTO.CollectionLearningInfo learningInfo = getLearningInfo(collection, currentUser);
54+
CollectionResponseDTO.CollectionLearningInfo learningInfo = getLearningInfo(collection, currentUser, true);
5555

5656
return CollectionConverter.toCollectionPreviewDTO(collection, learningInfo, currentUser);
5757
}
5858

59+
private List<ResourceResponseDTO.SearchResultResourceDTO> getAllResources(
60+
Collection collection
61+
) {
62+
List<CollectionEpisode> episodes = collection.getEpisodes();
63+
64+
// 모든 에피소드를 에피소드 번호 순으로 정렬
65+
return episodes.stream()
66+
.sorted(Comparator.comparing(CollectionEpisode::getEpisodeNumber))
67+
.map(ResourceConverter::convertToResourceDTO)
68+
.toList();
69+
}
70+
5971
public CollectionResponseDTO.SearchResultDTO search(SearchRequestDTO.SearchConditionDTO condition, Long lastId, PrincipalDetails principalDetails) {
6072

6173
Authentication authentication = (principalDetails != null) ? SecurityContextHolder.getContext().getAuthentication() : null;
@@ -85,7 +97,7 @@ public CollectionResponseDTO.SearchResultDTO search(SearchRequestDTO.SearchCondi
8597
Map<Long, CollectionResponseDTO.CollectionLearningInfo> learningInfoMap = collections.stream()
8698
.collect(Collectors.toMap(
8799
Collection::getId,
88-
collection -> getLearningInfo(collection, currentUser)
100+
collection -> getLearningInfo(collection, currentUser, false)
89101
));
90102

91103
return CollectionConverter.toSearchResultDTO(
@@ -99,12 +111,18 @@ public CollectionResponseDTO.SearchResultDTO search(SearchRequestDTO.SearchCondi
99111
);
100112
}
101113

102-
public CollectionResponseDTO.CollectionLearningInfo getLearningInfo(Collection collection, User user) {
114+
public CollectionResponseDTO.CollectionLearningInfo getLearningInfo(
115+
Collection collection,
116+
User user,
117+
boolean isDetailView
118+
) {
103119
if (user == null) {
104120
return CollectionResponseDTO.CollectionLearningInfo.builder()
105121
.learningStatus("BEFORE")
106122
.progressRate(null)
107-
.resourceDTOList(getFilteredResources(collection, null, 0))
123+
.resourceDTOList(isDetailView ?
124+
getAllResources(collection) : // 상세 조회면 전체 리소스
125+
getFilteredResources(collection, null, 0)) // 아니면 필터링된 리소스
108126
.build();
109127
}
110128

@@ -114,7 +132,9 @@ public CollectionResponseDTO.CollectionLearningInfo getLearningInfo(Collection c
114132
return CollectionResponseDTO.CollectionLearningInfo.builder()
115133
.learningStatus("BEFORE")
116134
.progressRate(null)
117-
.resourceDTOList(getFilteredResources(collection, null, 0))
135+
.resourceDTOList(isDetailView ?
136+
getAllResources(collection) : // 상세 조회면 전체 리소스
137+
getFilteredResources(collection, null, 0)) // 아니면 필터링된 리소스
118138
.build();
119139
}
120140

@@ -123,17 +143,23 @@ public CollectionResponseDTO.CollectionLearningInfo getLearningInfo(Collection c
123143
return CollectionResponseDTO.CollectionLearningInfo.builder()
124144
.learningStatus("COMPLETED")
125145
.progressRate(100)
126-
.completedTime(realUserCollection.getCompletedTime())
127-
.resourceDTOList(getFilteredResources(collection, user, realUserCollection.getUserCollectionStatus()))
146+
.startDate(realUserCollection.getCreatedAt().toLocalDate())
147+
.completedDate(realUserCollection.getCompletedTime())
148+
.resourceDTOList(isDetailView ?
149+
getAllResources(collection) : // 상세 조회면 전체 리소스
150+
new ArrayList<>()) // 아니면 빈 리스트
128151
.build();
129152
}
130153

131154
int progressRate = calculateProgressRate(realUserCollection);
132155
return CollectionResponseDTO.CollectionLearningInfo.builder()
133156
.learningStatus("IN_PROGRESS")
134157
.progressRate(progressRate)
158+
.startDate(realUserCollection.getCreatedAt().toLocalDate())
135159
.currentEpisode(realUserCollection.getUserCollectionStatus())
136-
.resourceDTOList(getFilteredResources(collection, user, realUserCollection.getUserCollectionStatus()))
160+
.resourceDTOList(isDetailView ?
161+
getAllResources(collection) :
162+
getFilteredResources(collection, user, realUserCollection.getUserCollectionStatus()))
137163
.build();
138164
}
139165

@@ -143,7 +169,8 @@ private int calculateProgressRate(UserCollection userCollection) {
143169
}
144170

145171
private List<ResourceResponseDTO.SearchResultResourceDTO> getFilteredResources(
146-
Collection collection, User user, int currentEpisode) {
172+
Collection collection, User user, int currentEpisode
173+
) {
147174
List<CollectionEpisode> episodes = collection.getEpisodes();
148175
List<CollectionEpisode> filteredEpisodes;
149176

@@ -156,17 +183,17 @@ private List<ResourceResponseDTO.SearchResultResourceDTO> getFilteredResources(
156183
}
157184

158185
if (user == null || currentEpisode == 0) {
159-
// 비회원이거나 수강 전인 경우 처음 3개
186+
// 비회원이거나 수강 전인 경우 처음 4개
160187
filteredEpisodes = episodes.stream()
161188
.sorted(Comparator.comparing(CollectionEpisode::getEpisodeNumber))
162-
.limit(3)
189+
.limit(4)
163190
.toList();
164191
} else {
165192
// 수강 중인 경우 현재 회차부터 3개
166193
filteredEpisodes = episodes.stream()
167194
.sorted(Comparator.comparing(CollectionEpisode::getEpisodeNumber))
168195
.filter(ep -> ep.getEpisodeNumber() >= currentEpisode)
169-
.limit(3)
196+
.limit(4)
170197
.toList();
171198
}
172199

@@ -178,12 +205,24 @@ private List<ResourceResponseDTO.SearchResultResourceDTO> getFilteredResources(
178205
public HomeResponseDTO.GuestHomeInfoDTO getGuestHomeCollections() {
179206
List<Collection> collections = collectionRepository.findTopBookmarkedCollections(HOME_COLLECTION_SIZE);
180207

181-
return HomeConverter.convertToGuestHomeInfoDTO(CollectionConverter.convertToHomeCollection(collections));
208+
List<CollectionResponseDTO.CollectionPreviewDTO> GuestCollectionList = collections.stream()
209+
.map(collection -> {
210+
CollectionResponseDTO.CollectionLearningInfo defaultLearningInfo =
211+
CollectionResponseDTO.CollectionLearningInfo.builder()
212+
.learningStatus("BEFORE")
213+
.progressRate(null)
214+
.resourceDTOList(getFilteredResources(collection, null, 0))
215+
.build();
216+
return CollectionConverter.toCollectionPreviewDTO(collection, defaultLearningInfo, null);
217+
})
218+
.toList();
219+
220+
return HomeConverter.convertToGuestHomeInfoDTO(GuestCollectionList);
182221
}
183222

184223
public HomeResponseDTO.UserHomeInfoDTO getUserHomeCollections(User user) {
185224
// 최근 학습 컬렉션 조회
186-
HomeResponseDTO.RecentLearningDTO recentLearning = getRecentLearning(user);
225+
CollectionResponseDTO.CollectionPreviewDTO recentLearning = getRecentLearning(user);
187226

188227
// 추천 컬렉션 목록 조회
189228

@@ -225,7 +264,7 @@ public HomeResponseDTO.UserHomeInfoDTO getUserHomeCollections(User user) {
225264
Map<Long, CollectionResponseDTO.CollectionLearningInfo> learningInfoMap = recommendedCollections.stream()
226265
.collect(Collectors.toMap(
227266
Collection::getId,
228-
collection -> getLearningInfo(collection, user)
267+
collection -> getLearningInfo(collection, user, false)
229268
));
230269

231270
return HomeConverter.convertToUserHomeInfoDTO(
@@ -237,10 +276,14 @@ public HomeResponseDTO.UserHomeInfoDTO getUserHomeCollections(User user) {
237276
);
238277
}
239278

240-
private HomeResponseDTO.RecentLearningDTO getRecentLearning(User user) {
279+
private CollectionResponseDTO.CollectionPreviewDTO getRecentLearning(User user) {
241280
return userCollectionRepository
242281
.findFirstByUserAndStatusOrderByCompletedTimeDesc(user, UserCollectionStatus.IN_PROGRESS)
243-
.map(ResourceConverter::toRecentLearningDTO)
282+
.map(userCollection -> CollectionConverter.toCollectionPreviewDTO(
283+
userCollection.getCollection(),
284+
getLearningInfo(userCollection.getCollection(), user, false),
285+
user
286+
))
244287
.orElse(null);
245288
}
246289

0 commit comments

Comments
 (0)