2017-08-18 16:08:27 +07:00
|
|
|
package io.spring.application;
|
2017-08-15 15:36:07 +07:00
|
|
|
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.application.data.CommentData;
|
|
|
|
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
|
2017-08-15 15:36:07 +07:00
|
|
|
import io.spring.core.user.User;
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.infrastructure.mybatis.readservice.CommentReadService;
|
2017-08-15 15:36:07 +07:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2017-08-15 16:52:23 +07:00
|
|
|
import java.util.List;
|
2017-08-15 15:36:07 +07:00
|
|
|
import java.util.Optional;
|
2017-08-18 11:09:07 +07:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
2017-08-15 15:36:07 +07:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class CommentQueryService {
|
|
|
|
private CommentReadService commentReadService;
|
|
|
|
private UserRelationshipQueryService userRelationshipQueryService;
|
|
|
|
|
|
|
|
public CommentQueryService(CommentReadService commentReadService, UserRelationshipQueryService userRelationshipQueryService) {
|
|
|
|
this.commentReadService = commentReadService;
|
|
|
|
this.userRelationshipQueryService = userRelationshipQueryService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<CommentData> 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);
|
|
|
|
}
|
2017-08-15 16:52:23 +07:00
|
|
|
|
2017-08-18 11:09:07 +07:00
|
|
|
public List<CommentData> findByArticleId(String articleId, User user) {
|
|
|
|
List<CommentData> comments = commentReadService.findByArticleId(articleId);
|
2017-11-02 14:27:49 +07:00
|
|
|
if (comments.size() > 0 && user != null) {
|
2017-08-18 11:09:07 +07:00
|
|
|
Set<String> 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;
|
2017-08-15 16:52:23 +07:00
|
|
|
}
|
2017-08-15 15:36:07 +07:00
|
|
|
}
|