Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import swm.betterlife.antifragile.common.entity.BaseTimeEntity;
import swm.betterlife.antifragile.common.exception.RecommendedContentNotFoundException;
import swm.betterlife.antifragile.domain.content.dto.response.ContentDetailResponse;
import swm.betterlife.antifragile.domain.content.dto.response.ContentListResponse;
Expand Down Expand Up @@ -35,7 +36,10 @@ public ContentListResponse getRecommendContents(String memberId, LocalDate date)
.map(RecommendContent::getContentUrl)
.toList();

List<Content> recommendContents = contentRepository.findByUrlIn(recommendContentUrls);
List<Content> recommendContents
= contentRepository.findByUrlIn(recommendContentUrls).stream()
.sorted(Comparator.comparing(BaseTimeEntity::getModifiedAt))
.toList();

return ContentListResponse.from(
recommendContents.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ public ContentListResponse saveRecommendContents(String memberId, LocalDate date
List<Content> recommendedContents
= getRecommendContentsByAnalysis(analysis, member, prompt);

List<Content> savedContents = saveOrUpdateContents(recommendedContents);
diaryAnalysisService.saveRecommendContents(analysis, savedContents);

return ContentListResponse.from(
recommendedContents.stream()
savedContents.stream()
.map(content -> ContentListResponse.ContentResponse.from(
content,
contentQueryService.getContentLikeNumber(content),
Expand Down Expand Up @@ -86,8 +89,12 @@ public ContentListResponse saveReRecommendContents(
= getRecommendContentsByAnalysis(analysis, member, prompt);
// TODO: 추후에 feedback을 통해서 재추천 컨텐츠를 가져와야 함

List<Content> savedContents = saveOrUpdateContents(recommendedContents);
diaryAnalysisService.saveRecommendContents(analysis, savedContents);


return ContentListResponse.from(
recommendedContents.stream()
savedContents.stream()
.map(content -> ContentListResponse.ContentResponse.from(
content,
contentQueryService.getContentLikeNumber(content),
Expand Down Expand Up @@ -143,7 +150,25 @@ private List<Content> getRecommendContentsByAnalysis(
}
}

private List<Content> saveOrUpdateContents(List<Content> recommendedContents) {
List<String> urls = recommendedContents.stream().map(Content::getUrl).toList();
Map<String, Content> existingContents = contentRepository.findByUrlIn(urls).stream()
.collect(Collectors.toMap(Content::getUrl, Function.identity()));
List<Content> toSaveContents = new ArrayList<>();
for (Content content : recommendedContents) {
Content existingContent = existingContents.get(content.getUrl());
if (existingContent != null) {
existingContent.updateContent(content);
toSaveContents.add(existingContent);
} else {
toSaveContents.add(content);
}
}
return contentRepository.saveAll(toSaveContents);
}

private void validateRecommendLimit(String memberId) {
memberService.decrementRemainRecommendNumber(memberId);
}

}
Loading