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;
|
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;
|
2021-03-19 14:23:37 +07:00
|
|
|
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
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;
|
2021-03-19 14:23:37 +07:00
|
|
|
import org.springframework.stereotype.Service;
|
2017-08-15 15:36:07 +07:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class CommentQueryService {
|
2021-03-19 14:23:37 +07:00
|
|
|
private CommentReadService commentReadService;
|
|
|
|
private UserRelationshipQueryService userRelationshipQueryService;
|
2017-08-15 15:36:07 +07:00
|
|
|
|
2021-03-19 14:23:37 +07:00
|
|
|
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()));
|
2017-08-15 15:36:07 +07:00
|
|
|
}
|
2021-03-19 14:23:37 +07:00
|
|
|
return Optional.ofNullable(commentData);
|
|
|
|
}
|
2017-08-15 15:36:07 +07:00
|
|
|
|
2021-03-19 14:23:37 +07:00
|
|
|
public List<CommentData> findByArticleId(String articleId, User user) {
|
|
|
|
List<CommentData> comments = commentReadService.findByArticleId(articleId);
|
|
|
|
if (comments.size() > 0 && user != null) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2017-08-15 15:36:07 +07:00
|
|
|
}
|
2021-03-19 14:23:37 +07:00
|
|
|
return comments;
|
|
|
|
}
|
2017-08-15 16:52:23 +07:00
|
|
|
|
2021-03-19 14:23:37 +07:00
|
|
|
public CursorPager<CommentData> findByArticleIdWithCursor(
|
|
|
|
String articleId, User user, CursorPageParameter page) {
|
|
|
|
List<CommentData> comments = commentReadService.findByArticleIdWithCursor(articleId, page);
|
|
|
|
if (comments.isEmpty()) {
|
|
|
|
return new CursorPager<>(new ArrayList<>(), page.getDirection(), false);
|
|
|
|
}
|
|
|
|
if (user != null) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
boolean hasExtra = comments.size() > page.getLimit();
|
|
|
|
if (hasExtra) {
|
|
|
|
comments.remove(page.getLimit());
|
|
|
|
}
|
|
|
|
if (!page.isNext()) {
|
|
|
|
Collections.reverse(comments);
|
2017-08-15 16:52:23 +07:00
|
|
|
}
|
2021-03-19 14:23:37 +07:00
|
|
|
return new CursorPager<>(comments, page.getDirection(), hasExtra);
|
|
|
|
}
|
2017-08-15 15:36:07 +07:00
|
|
|
}
|