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
143 changes: 20 additions & 123 deletions src/main/java/com/techfork/post/application/query/PostQueryService.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
package com.techfork.post.application.query;

import com.techfork.activity.bookmark.infrastructure.BookmarkRepository;
import com.techfork.post.application.query.result.CompanyListItemResult;
import com.techfork.post.application.query.result.GetCompanyListResult;
import com.techfork.post.application.query.result.GetPostDetailResult;
import com.techfork.post.application.query.result.GetPostListResult;
import com.techfork.post.application.query.result.PostListItemResult;
import com.techfork.post.domain.PostKeyword;
import com.techfork.post.domain.enums.EPostSortType;
import com.techfork.post.infrastructure.PostKeywordRepository;
import com.techfork.post.infrastructure.PostRepository;
import com.techfork.post.infrastructure.row.CompanyRow;
import com.techfork.post.infrastructure.row.PostDetailRow;
import com.techfork.post.infrastructure.row.PostInfoRow;
import com.techfork.global.exception.CommonErrorCode;
import com.techfork.global.exception.GeneralException;
import com.techfork.global.util.CloudflareThirdPartyThumbnailOptimizer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PostQueryService {

private final PostRepository postRepository;
private final PostKeywordRepository postKeywordRepository;
private final BookmarkRepository bookmarkRepository;
private final CloudflareThirdPartyThumbnailOptimizer thumbnailOptimizer;
private final PostReadModelEnricher postReadModelEnricher;

public GetCompanyListResult getCompanies() {
List<String> companies = postRepository.findDistinctCompanies();
Expand Down Expand Up @@ -68,25 +57,15 @@ public GetCompanyListResult getCompaniesV2() {
public GetPostListResult getPostsByCompany(GetPostsByCompanyQuery query) {
PageRequest pageRequest = PageRequest.of(0, query.size() + 1);
List<PostInfoRow> posts = postRepository.findByCompanyWithCursor(query.company(), query.lastPostId(), pageRequest);
List<PostInfoRow> postsWithKeywords = attachKeywordsToPostInfoList(posts);

if (query.userId() != null) {
postsWithKeywords = attachBookmarksToPostInfoList(postsWithKeywords, query.userId());
}

return toGetPostListResult(postsWithKeywords, query.size());
List<PostInfoRow> enrichedPosts = postReadModelEnricher.enrichPostInfoRows(posts, query.userId());
return toGetPostListResult(enrichedPosts, query.size());
}

public GetPostListResult getPostsByCompanyV2(GetPostsByCompanyV2Query query) {
PageRequest pageRequest = PageRequest.of(0, query.size() + 1);
List<PostInfoRow> posts = postRepository.findByCompanyNamesWithCursor(query.companies(), query.lastPublishedAt(), query.lastPostId(), pageRequest);
List<PostInfoRow> postsWithKeywords = attachKeywordsToPostInfoList(posts);

if (query.userId() != null) {
postsWithKeywords = attachBookmarksToPostInfoList(postsWithKeywords, query.userId());
}

return toGetPostListResult(postsWithKeywords, query.size());
List<PostInfoRow> enrichedPosts = postReadModelEnricher.enrichPostInfoRows(posts, query.userId());
return toGetPostListResult(enrichedPosts, query.size());
}

public GetPostListResult getRecentPosts(GetRecentPostsQuery query) {
Expand All @@ -99,13 +78,8 @@ public GetPostListResult getRecentPosts(GetRecentPostsQuery query) {
posts = postRepository.findRecentPostsWithCursor(query.lastPostId(), pageRequest);
}

List<PostInfoRow> postsWithKeywords = attachKeywordsToPostInfoList(posts);

if (query.userId() != null) {
postsWithKeywords = attachBookmarksToPostInfoList(postsWithKeywords, query.userId());
}

return toGetPostListResult(postsWithKeywords, query.size());
List<PostInfoRow> enrichedPosts = postReadModelEnricher.enrichPostInfoRows(posts, query.userId());
return toGetPostListResult(enrichedPosts, query.size());
}

public GetPostListResult getRecentPostsV2(GetRecentPostsV2Query query) {
Expand All @@ -118,106 +92,29 @@ public GetPostListResult getRecentPostsV2(GetRecentPostsV2Query query) {
posts = postRepository.findRecentPostsWithCursorV2(query.lastPublishedAt(), query.lastPostId(), pageRequest);
}

List<PostInfoRow> postsWithKeywords = attachKeywordsToPostInfoList(posts);

if (query.userId() != null) {
postsWithKeywords = attachBookmarksToPostInfoList(postsWithKeywords, query.userId());
}

return toGetPostListResult(postsWithKeywords, query.size());
List<PostInfoRow> enrichedPosts = postReadModelEnricher.enrichPostInfoRows(posts, query.userId());
return toGetPostListResult(enrichedPosts, query.size());
}

public GetPostDetailResult getPostDetail(GetPostDetailQuery query) {
PostDetailRow postDetail = postRepository.findByIdWithTechBlog(query.postId())
.orElseThrow(() -> new GeneralException(CommonErrorCode.NOT_FOUND));

List<String> keywords = postKeywordRepository.findByPostIdIn(List.of(query.postId()))
.stream()
.map(PostKeyword::getKeyword)
.toList();

Boolean isBookmarked = null;
if (query.userId() != null) {
isBookmarked = !bookmarkRepository.findBookmarkedPostIds(query.userId(), List.of(query.postId())).isEmpty();
}
PostDetailRow enrichedPostDetail = postReadModelEnricher.enrichPostDetailRow(postDetail, query.userId());

return GetPostDetailResult.builder()
.id(postDetail.id())
.title(postDetail.title())
.summary(postDetail.summary())
.company(postDetail.company())
.url(postDetail.url())
.logoUrl(postDetail.logoUrl())
.publishedAt(postDetail.publishedAt())
.viewCount(postDetail.viewCount())
.keywords(keywords)
.isBookmarked(isBookmarked)
.id(enrichedPostDetail.id())
.title(enrichedPostDetail.title())
.summary(enrichedPostDetail.summary())
.company(enrichedPostDetail.company())
.url(enrichedPostDetail.url())
.logoUrl(enrichedPostDetail.logoUrl())
.publishedAt(enrichedPostDetail.publishedAt())
.viewCount(enrichedPostDetail.viewCount())
.keywords(enrichedPostDetail.keywords())
.isBookmarked(enrichedPostDetail.isBookmarked())
.build();
}

private List<PostInfoRow> attachKeywordsToPostInfoList(List<PostInfoRow> posts) {
if (posts.isEmpty()) {
return posts;
}

List<Long> postIds = posts.stream()
.map(PostInfoRow::id)
.toList();

Map<Long, List<String>> keywordMap = postKeywordRepository.findByPostIdIn(postIds)
.stream()
.collect(Collectors.groupingBy(
pk -> pk.getPost().getId(),
Collectors.mapping(PostKeyword::getKeyword, Collectors.toList())
));

return posts.stream()
.map(post -> PostInfoRow.builder()
.id(post.id())
.title(post.title())
.shortSummary(post.shortSummary())
.company(post.company())
.url(post.url())
.logoUrl(post.logoUrl())
.thumbnailUrl(thumbnailOptimizer.optimize(post.thumbnailUrl()))
.publishedAt(post.publishedAt())
.viewCount(post.viewCount())
.keywords(keywordMap.getOrDefault(post.id(), List.of()))
.isBookmarked(null)
.build())
.toList();
}

private List<PostInfoRow> attachBookmarksToPostInfoList(List<PostInfoRow> posts, Long userId) {
if (posts.isEmpty()) {
return posts;
}

List<Long> postIds = posts.stream()
.map(PostInfoRow::id)
.toList();

Set<Long> bookmarkedPostIds = bookmarkRepository.findBookmarkedPostIds(userId, postIds)
.stream()
.collect(Collectors.toSet());

return posts.stream()
.map(post -> PostInfoRow.builder()
.id(post.id())
.title(post.title())
.shortSummary(post.shortSummary())
.company(post.company())
.url(post.url())
.logoUrl(post.logoUrl())
.thumbnailUrl(thumbnailOptimizer.optimize(post.thumbnailUrl()))
.publishedAt(post.publishedAt())
.viewCount(post.viewCount())
.keywords(post.keywords())
.isBookmarked(bookmarkedPostIds.contains(post.id()))
.build())
.toList();
}

private GetPostListResult toGetPostListResult(List<PostInfoRow> posts, int requestedSize) {
boolean hasNext = posts.size() > requestedSize;
List<PostInfoRow> content = hasNext ? posts.subList(0, requestedSize) : posts;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.techfork.post.application.query;

import com.techfork.activity.bookmark.application.query.lookup.BookmarkLookupService;
import com.techfork.global.util.CloudflareThirdPartyThumbnailOptimizer;
import com.techfork.post.application.query.lookup.PostKeywordLookupService;
import com.techfork.post.infrastructure.row.PostDetailRow;
import com.techfork.post.infrastructure.row.PostInfoRow;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PostReadModelEnricher {

private final PostKeywordLookupService postKeywordLookupService;
private final BookmarkLookupService bookmarkLookupService;
private final CloudflareThirdPartyThumbnailOptimizer thumbnailOptimizer;

public List<PostInfoRow> enrichPostInfoRows(List<PostInfoRow> posts, Long userId) {
if (posts.isEmpty()) {
return List.of();
}

List<Long> postIds = posts.stream()
.map(PostInfoRow::id)
.toList();

Map<Long, List<String>> keywordMap = postKeywordLookupService.getKeywordsByPostIds(postIds);
Set<Long> bookmarkedPostIds = userId == null
? Set.of()
: bookmarkLookupService.getBookmarkedPostIds(userId, postIds);

return posts.stream()
.map(post -> post.toBuilder()
.thumbnailUrl(thumbnailOptimizer.optimize(post.thumbnailUrl()))
.keywords(keywordMap.getOrDefault(post.id(), List.of()))
.isBookmarked(resolveBookmarkStatus(userId, bookmarkedPostIds, post.id()))
.build())
.toList();
}

public PostDetailRow enrichPostDetailRow(PostDetailRow postDetail, Long userId) {
Long postId = postDetail.id();
List<String> keywords = postKeywordLookupService.getKeywordsByPostIds(List.of(postId))
.getOrDefault(postId, List.of());
Boolean isBookmarked = resolveBookmarkStatus(
userId,
userId == null ? Set.of() : bookmarkLookupService.getBookmarkedPostIds(userId, List.of(postId)),
postId
);

return PostDetailRow.builder()
.id(postDetail.id())
.title(postDetail.title())
.summary(postDetail.summary())
.company(postDetail.company())
.url(postDetail.url())
.logoUrl(postDetail.logoUrl())
.publishedAt(postDetail.publishedAt())
.viewCount(postDetail.viewCount())
.keywords(keywords)
.isBookmarked(isBookmarked)
.build();
}

private Boolean resolveBookmarkStatus(Long userId, Set<Long> bookmarkedPostIds, Long postId) {
if (userId == null) {
return null;
}
return bookmarkedPostIds.contains(postId);
}
}
Loading