@@ -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