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.data.domain.Sort.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.querydsl.jpa.impl.JPAQueryFactory;
import de.bstly.board.model.Comment; import de.bstly.board.model.Comment;
import de.bstly.board.model.QComment; import de.bstly.board.model.QComment;
import de.bstly.board.model.QEntry;
import de.bstly.board.model.QVote; import de.bstly.board.model.QVote;
import de.bstly.board.model.Types; import de.bstly.board.model.Types;
import de.bstly.board.model.VoteType; import de.bstly.board.model.VoteType;
@ -29,26 +32,25 @@ public class CommentManager {
@Autowired @Autowired
private CommentRepository commentRepository; private CommentRepository commentRepository;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired @Autowired
private VoteRepository voteRepository; private VoteRepository voteRepository;
@Autowired @Autowired
private VoteManager voteManager; private VoteManager voteManager;
private QComment qComment = QComment.comment; private QComment qComment = QComment.comment;
private QVote qVote = QVote.vote; private QVote qVote = QVote.vote;
private QEntry qEntry = QEntry.entry;
/** /**
* Fetch by ranking. * Fetch by ranking.
* *
* @param target the target * @param target the target
* @param parent the parent * @param parent the parent
* @param date the date * @param date the date
* @param gravity the gravity * @param gravity the gravity
* @param page the page * @param page the page
* @param size the size * @param size the size
* @return the page * @return the page
*/ */
@ -68,10 +70,10 @@ public class CommentManager {
* *
* @param target the target * @param target the target
* @param parent the parent * @param parent the parent
* @param date the date * @param date the date
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param desc the desc * @param desc the desc
* @return the page * @return the page
*/ */
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size, public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size,
@ -93,11 +95,11 @@ public class CommentManager {
* Fetch by username. * Fetch by username.
* *
* @param username the username * @param username the username
* @param orElse the or else * @param orElse the or else
* @param date the date * @param date the date
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param asc the asc * @param asc the asc
* @return the page * @return the page
*/ */
public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page, public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
@ -136,48 +138,56 @@ public class CommentManager {
* Apply metadata. * Apply metadata.
* *
* @param username the username * @param username the username
* @param comment the comment * @param comment the comment
*/ */
public void applyMetadata(String username, Comment comment) { public void applyMetadata(String username, Comment comment, List<String> ignore) {
if (!comment.getMetadata().containsKey("comments")) {
ignore.addAll(comment.getMetadata().keySet());
if (!ignore.contains("comments")) {
comment.getMetadata().put("comments", count(comment.getTarget(), comment.getId())); comment.getMetadata().put("comments", count(comment.getTarget(), comment.getId()));
} }
if (!comment.getMetadata().containsKey("points")) { if (!ignore.contains("points")) {
comment.getMetadata().put("points", comment.getMetadata().put("points",
voteManager.getPoints(comment.getId(), Types.comment)); voteManager.getPoints(comment.getId(), Types.comment));
} }
if (!comment.getMetadata().containsKey("upvoted")) { if (!ignore.contains("upvoted")) {
comment.getMetadata().put("upvoted", comment.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(comment.getId()) voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment)).and(qVote.type.eq(VoteType.up)) .and(qVote.targetType.eq(Types.comment)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.eq(username)))); .and(qVote.author.eq(username))));
} }
if (!comment.getMetadata().containsKey("downvoted")) { if (!ignore.contains("downvoted")) {
comment.getMetadata().put("downvoted", comment.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(comment.getId()) voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment)) .and(qVote.targetType.eq(Types.comment))
.and(qVote.type.eq(VoteType.down)).and(qVote.author.eq(username)))); .and(qVote.type.eq(VoteType.down)).and(qVote.author.eq(username))));
} }
if (!comment.getMetadata().containsKey("unvote")) { if (!ignore.contains("unvote")) {
comment.getMetadata().put("unvote", comment.getMetadata().put("unvote",
voteRepository.exists( voteRepository.exists(
qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment)) qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment))
.and(qVote.author.eq(username)))); .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. * Apply metadata.
* *
* @param username the username * @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) { for (Comment comment : entries) {
applyMetadata(username, comment); applyMetadata(username, comment, ignore);
} }
} }

View File

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