Skip to content

Commit d5fdd8d

Browse files
authored
fix: 추천 콘텐츠 DB 저장 로직 추가
fix: 추천 콘텐츠 DB 저장 로직 추가
2 parents a548365 + 63439c4 commit d5fdd8d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.stereotype.Service;
88
import org.springframework.transaction.annotation.Transactional;
9+
import swm.betterlife.antifragile.common.entity.BaseTimeEntity;
910
import swm.betterlife.antifragile.common.exception.RecommendedContentNotFoundException;
1011
import swm.betterlife.antifragile.domain.content.dto.response.ContentDetailResponse;
1112
import swm.betterlife.antifragile.domain.content.dto.response.ContentListResponse;
@@ -35,7 +36,10 @@ public ContentListResponse getRecommendContents(String memberId, LocalDate date)
3536
.map(RecommendContent::getContentUrl)
3637
.toList();
3738

38-
List<Content> recommendContents = contentRepository.findByUrlIn(recommendContentUrls);
39+
List<Content> recommendContents
40+
= contentRepository.findByUrlIn(recommendContentUrls).stream()
41+
.sorted(Comparator.comparing(BaseTimeEntity::getModifiedAt))
42+
.toList();
3943

4044
return ContentListResponse.from(
4145
recommendContents.stream()

src/main/java/swm/betterlife/antifragile/domain/content/service/ContentService.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ public ContentListResponse saveRecommendContents(String memberId, LocalDate date
5656
List<Content> recommendedContents
5757
= getRecommendContentsByAnalysis(analysis, member, prompt);
5858

59+
List<Content> savedContents = saveOrUpdateContents(recommendedContents);
60+
diaryAnalysisService.saveRecommendContents(analysis, savedContents);
61+
5962
return ContentListResponse.from(
60-
recommendedContents.stream()
63+
savedContents.stream()
6164
.map(content -> ContentListResponse.ContentResponse.from(
6265
content,
6366
contentQueryService.getContentLikeNumber(content),
@@ -86,8 +89,12 @@ public ContentListResponse saveReRecommendContents(
8689
= getRecommendContentsByAnalysis(analysis, member, prompt);
8790
// TODO: 추후에 feedback을 통해서 재추천 컨텐츠를 가져와야 함
8891

92+
List<Content> savedContents = saveOrUpdateContents(recommendedContents);
93+
diaryAnalysisService.saveRecommendContents(analysis, savedContents);
94+
95+
8996
return ContentListResponse.from(
90-
recommendedContents.stream()
97+
savedContents.stream()
9198
.map(content -> ContentListResponse.ContentResponse.from(
9299
content,
93100
contentQueryService.getContentLikeNumber(content),
@@ -143,7 +150,25 @@ private List<Content> getRecommendContentsByAnalysis(
143150
}
144151
}
145152

153+
private List<Content> saveOrUpdateContents(List<Content> recommendedContents) {
154+
List<String> urls = recommendedContents.stream().map(Content::getUrl).toList();
155+
Map<String, Content> existingContents = contentRepository.findByUrlIn(urls).stream()
156+
.collect(Collectors.toMap(Content::getUrl, Function.identity()));
157+
List<Content> toSaveContents = new ArrayList<>();
158+
for (Content content : recommendedContents) {
159+
Content existingContent = existingContents.get(content.getUrl());
160+
if (existingContent != null) {
161+
existingContent.updateContent(content);
162+
toSaveContents.add(existingContent);
163+
} else {
164+
toSaveContents.add(content);
165+
}
166+
}
167+
return contentRepository.saveAll(toSaveContents);
168+
}
169+
146170
private void validateRecommendLimit(String memberId) {
147171
memberService.decrementRemainRecommendNumber(memberId);
148172
}
173+
149174
}

0 commit comments

Comments
 (0)