/** * */ package de.bstly.board.businesslogic; import java.time.Instant; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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 com.google.common.collect.Lists; import de.bstly.board.model.Bookmarks; import de.bstly.board.model.Entry; import de.bstly.board.model.QEntry; import de.bstly.board.model.QVote; import de.bstly.board.model.RankedEntry; import de.bstly.board.model.Types; import de.bstly.board.model.VoteType; import de.bstly.board.repository.EntryRepository; import de.bstly.board.repository.VoteRepository; /** * The Class EntryManager. */ @Component public class EntryManager { @Autowired private EntryRepository entryRepository; @Autowired private CommentManager commentManager; @Autowired private VoteManager voteManager; @Autowired private VoteRepository voteRepository; @Autowired private BookmarksManager bookmarksManager; private QEntry qEntry = QEntry.entry; private QVote qVote = QVote.vote; @Value("${bstly.board.ranking.gravity:1.8}") private double GRAVITY; /** * Fetch by ranking. * * @param date the date * @param gravity the gravity * @param page the page * @param size the size * @return the page */ public Page fetchByRanking(Instant date, double gravity, int page, int size) { return entryRepository.findAllByRanking(date, gravity, PageRequest.of(page, size)); } /** * Fetch by date. * * @param date the date * @param page the page * @param size the size * @return the page */ public Page fetchByDate(Instant date, int page, int size) { return entryRepository.findAll(qEntry.created.before(date), PageRequest.of(page, size, Sort.by(Order.desc("created")))); } /** * Fetch by user. * * @param username the username * @param date the date * @param page the page * @param size the size * @param asc the asc * @return the page */ public Page fetchByUser(String username, Instant date, int page, int size, boolean asc) { Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created")); return entryRepository.findAll(qEntry.author.eq(username).and(qEntry.created.before(date)), PageRequest.of(page, size, sort)); } /** * Fetch by bookmarks. * * @param username the username * @param page the page * @param size the size * @return the page */ public Page fetchByBookmarks(String username, int page, int size) { Bookmarks bookmarks = bookmarksManager.get(username); if (bookmarks.getEntries() == null) { bookmarks.setEntries(Lists.newArrayList()); } return entryRepository.findAll(qEntry.id.in(bookmarks.getEntries()), PageRequest.of(page, size, Sort.by(Order.desc("created")))); } /** * Apply metadata. * * @param username the username * @param entry the entry */ public void applyMetadata(String username, Entry entry) { if (!entry.getMetadata().containsKey("comments")) { entry.getMetadata().put("comments", commentManager.count(entry.getId())); } if (!entry.getMetadata().containsKey("points")) { entry.getMetadata().put("points", voteManager.getPoints(entry.getId(), Types.entry)); } if (!entry.getMetadata().containsKey("bookmarked")) { entry.getMetadata().put("bookmarked", bookmarksManager.hasEntry(username, entry.getId())); } if (!entry.getMetadata().containsKey("upvoted")) { entry.getMetadata().put("upvoted", voteRepository.exists(qVote.target.eq(entry.getId()) .and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.up)) .and(qVote.author.eq(username)))); } if (!entry.getMetadata().containsKey("downvoted")) { entry.getMetadata().put("downvoted", voteRepository.exists(qVote.target.eq(entry.getId()) .and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.down)) .and(qVote.author.eq(username)))); } if (!entry.getMetadata().containsKey("unvote")) { entry.getMetadata().put("unvote", voteRepository.exists(qVote.target.eq(entry.getId()) .and(qVote.targetType.eq(Types.entry)).and(qVote.author.eq(username)))); } } /** * Apply metadata. * * @param username the username * @param entries the entries */ public void applyMetadata(String username, List entries) { for (Entry entry : entries) { applyMetadata(username, entry); } } /** * Exists. * * @param id the id * @return true, if successful */ public boolean exists(Long id) { return entryRepository.existsById(id); } /** * Gets the. * * @param id the id * @return the entry */ public Entry get(Long id) { return entryRepository.findById(id).orElse(null); } /** * Save. * * @param entry the entry * @return the entry */ public Entry save(Entry entry) { return entryRepository.save(entry); } /** * Delete. * * @param entry the entry */ public void delete(Entry entry) { commentManager.deleteByTarget(entry.getId()); voteManager.deleteByTarget(entry.getId(), Types.entry); entryRepository.delete(entry); } /** * Gets the points. * * @param entryId the entry id * @return the points */ public long getPoints(Long entryId) { long upvotes = voteRepository.count(qVote.targetType.eq(Types.entry) .and(qVote.type.eq(VoteType.up)).and(qVote.target.eq(entryId))); long downvotes = voteRepository.count(qVote.targetType.eq(Types.entry) .and(qVote.type.eq(VoteType.down)).and(qVote.target.eq(entryId))); return upvotes - downvotes; } /** * Gets the user points. * * @param entryId the entry id * @param username the username * @return the user points */ public long getUserPoints(Long entryId, String username) { long upvotes = voteRepository.count(qVote.targetType.eq(Types.entry) .and(qVote.type.eq(VoteType.up).and(qVote.author.ne(username))) .and(qVote.target.eq(entryId))); long downvotes = voteRepository.count(qVote.targetType.eq(Types.entry) .and(qVote.type.eq(VoteType.down).and(qVote.author.ne(username))) .and(qVote.target.eq(entryId))); return upvotes - downvotes; } }