This commit is contained in:
2021-10-04 18:04:46 +02:00
parent 59321f4a3d
commit 8b0fab7488
2 changed files with 60 additions and 43 deletions
@@ -13,8 +13,11 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Component;
import com.querydsl.jpa.impl.JPAQueryFactory;
import de.bstly.board.model.Comment;
import de.bstly.board.model.QComment;
import de.bstly.board.model.QEntry;
import de.bstly.board.model.QVote;
import de.bstly.board.model.Types;
import de.bstly.board.model.VoteType;
@@ -29,26 +32,25 @@ public class CommentManager {
@Autowired
private CommentRepository commentRepository;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired
private VoteRepository voteRepository;
@Autowired
private VoteManager voteManager;
private QComment qComment = QComment.comment;
private QVote qVote = QVote.vote;
private QEntry qEntry = QEntry.entry;
/**
* Fetch by ranking.
*
* @param target the target
* @param parent the parent
* @param date the date
* @param target the target
* @param parent the parent
* @param date the date
* @param gravity the gravity
* @param page the page
* @param size the size
* @param page the page
* @param size the size
* @return the page
*/
@@ -68,10 +70,10 @@ public class CommentManager {
*
* @param target the target
* @param parent the parent
* @param date the date
* @param page the page
* @param size the size
* @param desc the desc
* @param date the date
* @param page the page
* @param size the size
* @param desc the desc
* @return the page
*/
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size,
@@ -93,11 +95,11 @@ public class CommentManager {
* Fetch by username.
*
* @param username the username
* @param orElse the or else
* @param date the date
* @param page the page
* @param size the size
* @param asc the asc
* @param orElse the or else
* @param date the date
* @param page the page
* @param size the size
* @param asc the asc
* @return the page
*/
public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
@@ -136,48 +138,56 @@ public class CommentManager {
* Apply metadata.
*
* @param username the username
* @param comment the comment
* @param comment the comment
*/
public void applyMetadata(String username, Comment comment) {
if (!comment.getMetadata().containsKey("comments")) {
public void applyMetadata(String username, Comment comment, List<String> ignore) {
ignore.addAll(comment.getMetadata().keySet());
if (!ignore.contains("comments")) {
comment.getMetadata().put("comments", count(comment.getTarget(), comment.getId()));
}
if (!comment.getMetadata().containsKey("points")) {
if (!ignore.contains("points")) {
comment.getMetadata().put("points",
voteManager.getPoints(comment.getId(), Types.comment));
}
if (!comment.getMetadata().containsKey("upvoted")) {
if (!ignore.contains("upvoted")) {
comment.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.eq(username))));
}
if (!comment.getMetadata().containsKey("downvoted")) {
if (!ignore.contains("downvoted")) {
comment.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment))
.and(qVote.type.eq(VoteType.down)).and(qVote.author.eq(username))));
}
if (!comment.getMetadata().containsKey("unvote")) {
if (!ignore.contains("unvote")) {
comment.getMetadata().put("unvote",
voteRepository.exists(
qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment))
.and(qVote.author.eq(username))));
}
if (!ignore.contains("entry")) {
comment.getMetadata().put("entry", jpaQueryFactory.selectFrom(qEntry)
.where(qEntry.id.eq(comment.getTarget())).select(qEntry.title).fetchOne());
}
}
/**
* Apply metadata.
*
* @param username the username
* @param entries the entries
* @param entries the entries
*/
public void applyMetadata(String username, List<Comment> entries) {
public void applyMetadata(String username, List<Comment> entries, List<String> ignore) {
for (Comment comment : entries) {
applyMetadata(username, comment);
applyMetadata(username, comment, ignore);
}
}