This commit is contained in:
_Bastler 2021-10-04 18:04:46 +02:00
parent 59321f4a3d
commit 8b0fab7488
2 changed files with 60 additions and 43 deletions

View File

@ -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);
}
}

View File

@ -4,6 +4,7 @@
package de.bstly.board.controller;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,6 +20,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.CommentManager;
import de.bstly.board.businesslogic.VoteManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
@ -54,11 +57,11 @@ public class CommentController extends BaseController {
/**
* Fetch by rank.
*
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param gravityParameter the gravity parameter
* @return the page
*/
@ -73,15 +76,16 @@ public class CommentController extends BaseController {
Page<Comment> comments = fetchByDate(target, parent, pageParameter, sizeParameter,
dateParameter, Optional.of(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
List<String> ignore = Lists.newArrayList("entry");
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments;
}
/**
* Fetch by date.
*
* @param target the target
* @param parent the parent
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
@ -99,19 +103,20 @@ public class CommentController extends BaseController {
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), descParameter.orElse(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
List<String> ignore = Lists.newArrayList("entry");
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments;
}
/**
* Fetch by username.
*
* @param username the username
* @param parent the parent
* @param username the username
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param ascParameter the asc parameter
* @param ascParameter the asc parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@ -125,7 +130,8 @@ public class CommentController extends BaseController {
Page<Comment> comments = commentManager.fetchByUsername(username, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(),
Lists.newArrayList());
return comments;
}
@ -158,7 +164,7 @@ public class CommentController extends BaseController {
throw new EntityResponseStatusException(HttpStatus.NOT_FOUND);
}
commentManager.applyMetadata(getCurrentUsername(), comment);
commentManager.applyMetadata(getCurrentUsername(), comment, Lists.newArrayList());
return comment;
}
@ -191,7 +197,8 @@ public class CommentController extends BaseController {
vote.setAuthor(getCurrentUsername());
voteManager.save(vote);
commentManager.applyMetadata(getCurrentUsername(), comment);
List<String> ignore = Lists.newArrayList("entry");
commentManager.applyMetadata(getCurrentUsername(), comment, ignore);
return comment;
}