initial commit
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.board.businesslogic;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import de.bstly.board.model.Comment;
|
||||
import de.bstly.board.model.QComment;
|
||||
import de.bstly.board.model.QVote;
|
||||
import de.bstly.board.model.Types;
|
||||
import de.bstly.board.model.VoteType;
|
||||
import de.bstly.board.repository.CommentRepository;
|
||||
import de.bstly.board.repository.VoteRepository;
|
||||
|
||||
/**
|
||||
* @author Lurkars
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class CommentManager {
|
||||
|
||||
@Autowired
|
||||
private CommentRepository commentRepository;
|
||||
@Autowired
|
||||
private VoteRepository voteRepository;
|
||||
@Autowired
|
||||
private VoteManager voteManager;
|
||||
private QComment qComment = QComment.comment;
|
||||
private QVote qVote = QVote.vote;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param parent
|
||||
* @param target
|
||||
* @param page
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
|
||||
public Page<Comment> fetchByRanking(Long target, Long parent, Instant date, double gravity,
|
||||
int page, int size) {
|
||||
if (parent == null) {
|
||||
return commentRepository.findAllByRankingAndParent(target, date, gravity,
|
||||
PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
return commentRepository.findAllByRankingAndParent(target, parent, date, gravity,
|
||||
PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param order
|
||||
* @return
|
||||
*/
|
||||
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size) {
|
||||
if (parent == null) {
|
||||
return commentRepository.findAll(
|
||||
qComment.target.eq(target).and(qComment.parent.isNull())
|
||||
.and(qComment.created.before(date)),
|
||||
PageRequest.of(page, size, Sort.by(Order.asc("created"))));
|
||||
}
|
||||
|
||||
return commentRepository.findAll(
|
||||
qComment.target.eq(target).and(qComment.parent.eq(parent))
|
||||
.and(qComment.created.before(date)),
|
||||
PageRequest.of(page, size, Sort.by(Order.asc("created"))));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @param parent
|
||||
* @return
|
||||
*/
|
||||
public Long count(Long target, Long parent) {
|
||||
if (parent == null) {
|
||||
return count(target);
|
||||
}
|
||||
|
||||
return commentRepository.count(qComment.target.eq(target).and(qComment.parent.eq(parent)));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
public Long count(Long target) {
|
||||
return commentRepository.count(qComment.target.eq(target));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param username
|
||||
* @param comment
|
||||
*/
|
||||
public void applyMetadata(String username, Comment comment) {
|
||||
if (!comment.getMetadata().containsKey("comments")) {
|
||||
comment.getMetadata().put("comments", count(comment.getTarget(), comment.getId()));
|
||||
}
|
||||
if (!comment.getMetadata().containsKey("points")) {
|
||||
comment.getMetadata().put("points",
|
||||
voteManager.getPoints(comment.getId(), Types.entry));
|
||||
}
|
||||
if (!comment.getMetadata().containsKey("vote")) {
|
||||
comment.getMetadata().put("vote",
|
||||
!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("unvote")) {
|
||||
comment.getMetadata().put("unvote",
|
||||
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("downvote")) {
|
||||
comment.getMetadata()
|
||||
.put("downvote",
|
||||
!voteRepository.exists(qVote.target.eq(comment.getId())
|
||||
.and(qVote.targetType.eq(Types.comment))
|
||||
.and(qVote.author.eq(username))));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entries
|
||||
*/
|
||||
public void applyMetadata(String username, List<Comment> entries) {
|
||||
for (Comment comment : entries) {
|
||||
applyMetadata(username, comment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(Long id) {
|
||||
return commentRepository.existsById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public Comment get(Long id) {
|
||||
return commentRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param comment
|
||||
* @return
|
||||
*/
|
||||
public Comment save(Comment comment) {
|
||||
return commentRepository.save(comment);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param commentId
|
||||
* @return
|
||||
*/
|
||||
public long getPoints(Long commentId) {
|
||||
long upvotes = voteRepository.count(qVote.targetType.eq(Types.comment)
|
||||
.and(qVote.type.eq(VoteType.up)).and(qVote.target.eq(commentId)));
|
||||
long downvotes = voteRepository.count(qVote.targetType.eq(Types.comment)
|
||||
.and(qVote.type.eq(VoteType.down)).and(qVote.target.eq(commentId)));
|
||||
return upvotes - downvotes;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user