package io.spring.application; import io.spring.application.data.CommentData; import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService; import io.spring.core.user.User; import io.spring.infrastructure.mybatis.readservice.CommentReadService; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @Service public class CommentQueryService { private CommentReadService commentReadService; private UserRelationshipQueryService userRelationshipQueryService; public CommentQueryService(CommentReadService commentReadService, UserRelationshipQueryService userRelationshipQueryService) { this.commentReadService = commentReadService; this.userRelationshipQueryService = userRelationshipQueryService; } public Optional findById(String id, User user) { CommentData commentData = commentReadService.findById(id); if (commentData == null) { return Optional.empty(); } else { commentData.getProfileData().setFollowing( userRelationshipQueryService.isUserFollowing( user.getId(), commentData.getProfileData().getId())); } return Optional.ofNullable(commentData); } public List findByArticleId(String articleId, User user) { List comments = commentReadService.findByArticleId(articleId); if (comments.size() > 0) { Set followingAuthors = userRelationshipQueryService.followingAuthors(user.getId(), comments.stream().map(commentData -> commentData.getProfileData().getId()).collect(Collectors.toList())); comments.forEach(commentData -> { if (followingAuthors.contains(commentData.getProfileData().getId())) { commentData.getProfileData().setFollowing(true); } }); } return comments; } }