Skip to content
Merged
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 @@ -9,7 +9,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
Expand Down Expand Up @@ -50,18 +49,15 @@ public ContentListResponse saveRecommendContents(String memberId, LocalDate date
diaryAnalysisService.getDiaryAnalysisByMemberIdAndDate(memberId, date);
Member member = memberService.getMemberById(memberId);

String prompt = "이 일기의 감정에 정신적으로 도움이 되는 메타데이터를 10개 추천해줘";
String prompt = "이 일기와 조금이라도 관련있는 메타데이터를 10개 추천해줘";
prompt = recommendService.createPrompt(
analysis.getEmotions(), analysis.getEvent(), member, prompt
);
List<Content> recommendedContents
= getRecommendContentsByAnalysis(analysis, member, prompt);

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

return ContentListResponse.from(
savedContents.stream()
recommendedContents.stream()
.map(content -> ContentListResponse.ContentResponse.from(
content,
contentQueryService.getContentLikeNumber(content),
Expand All @@ -81,7 +77,6 @@ public ContentListResponse saveReRecommendContents(
DiaryAnalysis analysis =
diaryAnalysisService.getDiaryAnalysisByMemberIdAndDate(memberId, date);
Member member = memberService.getMemberById(memberId);
List<String> recommendedUrls = extractRecommendContentUrls(analysis);

String prompt = "지금 사용자의 상태에 따라 관련되거나 정신적으로 도움 되는 콘텐츠 10개를 추천해줘";
prompt = recommendService.createPrompt(
Expand All @@ -91,12 +86,8 @@ public ContentListResponse saveReRecommendContents(
= getRecommendContentsByAnalysis(analysis, member, prompt);
// TODO: 추후에 feedback을 통해서 재추천 컨텐츠를 가져와야 함

List<Content> savedContents = saveOrUpdateContents(recommendedContents);

diaryAnalysisService.saveRecommendContents(analysis, savedContents);

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

private List<Content> getRecommendContentsByAnalysis(
DiaryAnalysis analysis,
List<String> recommendedUrls,
String feedback
) {
// TODO: gpt api와 youtube api를 통해서 재추천 컨텐츠를 가져와야 함
return null;
}

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);
}

private List<String> extractRecommendContentUrls(DiaryAnalysis analysis) {
return analysis.getRecommendContents().stream()
.map(RecommendContent::getContentUrl)
.toList();
}
}
Loading