@@ -56,8 +56,11 @@ public ContentListResponse saveRecommendContents(String memberId, LocalDate date
56
56
List <Content > recommendedContents
57
57
= getRecommendContentsByAnalysis (analysis , member , prompt );
58
58
59
+ List <Content > savedContents = saveOrUpdateContents (recommendedContents );
60
+ diaryAnalysisService .saveRecommendContents (analysis , savedContents );
61
+
59
62
return ContentListResponse .from (
60
- recommendedContents .stream ()
63
+ savedContents .stream ()
61
64
.map (content -> ContentListResponse .ContentResponse .from (
62
65
content ,
63
66
contentQueryService .getContentLikeNumber (content ),
@@ -86,8 +89,12 @@ public ContentListResponse saveReRecommendContents(
86
89
= getRecommendContentsByAnalysis (analysis , member , prompt );
87
90
// TODO: 추후에 feedback을 통해서 재추천 컨텐츠를 가져와야 함
88
91
92
+ List <Content > savedContents = saveOrUpdateContents (recommendedContents );
93
+ diaryAnalysisService .saveRecommendContents (analysis , savedContents );
94
+
95
+
89
96
return ContentListResponse .from (
90
- recommendedContents .stream ()
97
+ savedContents .stream ()
91
98
.map (content -> ContentListResponse .ContentResponse .from (
92
99
content ,
93
100
contentQueryService .getContentLikeNumber (content ),
@@ -143,7 +150,25 @@ private List<Content> getRecommendContentsByAnalysis(
143
150
}
144
151
}
145
152
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
+
146
170
private void validateRecommendLimit (String memberId ) {
147
171
memberService .decrementRemainRecommendNumber (memberId );
148
172
}
173
+
149
174
}
0 commit comments