diff --git a/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java b/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java index bd75661..a21d388 100644 --- a/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java +++ b/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentQueryService.java @@ -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; @@ -35,7 +36,10 @@ public ContentListResponse getRecommendContents(String memberId, LocalDate date) .map(RecommendContent::getContentUrl) .toList(); - List recommendContents = contentRepository.findByUrlIn(recommendContentUrls); + List recommendContents + = contentRepository.findByUrlIn(recommendContentUrls).stream() + .sorted(Comparator.comparing(BaseTimeEntity::getModifiedAt)) + .toList(); return ContentListResponse.from( recommendContents.stream() diff --git a/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentService.java b/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentService.java index 9884a96..c7a3918 100644 --- a/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentService.java +++ b/src/main/java/swm/betterlife/antifragile/domain/content/service/ContentService.java @@ -56,8 +56,11 @@ public ContentListResponse saveRecommendContents(String memberId, LocalDate date List recommendedContents = getRecommendContentsByAnalysis(analysis, member, prompt); + List savedContents = saveOrUpdateContents(recommendedContents); + diaryAnalysisService.saveRecommendContents(analysis, savedContents); + return ContentListResponse.from( - recommendedContents.stream() + savedContents.stream() .map(content -> ContentListResponse.ContentResponse.from( content, contentQueryService.getContentLikeNumber(content), @@ -86,8 +89,12 @@ public ContentListResponse saveReRecommendContents( = getRecommendContentsByAnalysis(analysis, member, prompt); // TODO: 추후에 feedback을 통해서 재추천 컨텐츠를 가져와야 함 + List savedContents = saveOrUpdateContents(recommendedContents); + diaryAnalysisService.saveRecommendContents(analysis, savedContents); + + return ContentListResponse.from( - recommendedContents.stream() + savedContents.stream() .map(content -> ContentListResponse.ContentResponse.from( content, contentQueryService.getContentLikeNumber(content), @@ -143,7 +150,25 @@ private List getRecommendContentsByAnalysis( } } + private List saveOrUpdateContents(List recommendedContents) { + List urls = recommendedContents.stream().map(Content::getUrl).toList(); + Map existingContents = contentRepository.findByUrlIn(urls).stream() + .collect(Collectors.toMap(Content::getUrl, Function.identity())); + List 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); } + }