Skip to content

Commit

Permalink
fix: 추천 콘텐츠 DB 저장 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
junseokkim committed Nov 20, 2024
1 parent a548365 commit 3b78f89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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,9 @@ public ContentListResponse getRecommendContents(String memberId, LocalDate date)
.map(RecommendContent::getContentUrl)
.toList();

List<Content> recommendContents = contentRepository.findByUrlIn(recommendContentUrls);
List<Content> recommendContents = contentRepository.findByUrlIn(recommendContentUrls).stream()

Check warning on line 39 in src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java

View workflow job for this annotation

GitHub Actions / Checkstyle job

[testtool] reported by reviewdog 🐶 Line is longer than 100 characters (found 102). Raw Output: /github/workspace/./src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java:39:0: warning: Line is longer than 100 characters (found 102). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.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);
}

}

0 comments on commit 3b78f89

Please sign in to comment.