improvements + bookmarks
This commit is contained in:
@@ -14,6 +14,9 @@ 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;
|
||||
@@ -24,8 +27,7 @@ import de.bstly.board.repository.EntryRepository;
|
||||
import de.bstly.board.repository.VoteRepository;
|
||||
|
||||
/**
|
||||
* @author Lurkars
|
||||
*
|
||||
* The Class EntryManager.
|
||||
*/
|
||||
@Component
|
||||
public class EntryManager {
|
||||
@@ -38,29 +40,39 @@ public class EntryManager {
|
||||
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;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @return
|
||||
* Direct fetch by ranking.
|
||||
*
|
||||
* @param date the date
|
||||
* @param gravity the gravity
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @return the page
|
||||
*/
|
||||
public Page<Entry> fetchByRanking(int page, int size) {
|
||||
return entryRepository.findAll(
|
||||
PageRequest.of(page, size, Sort.by(Order.desc("ranking"), Order.desc("created"))));
|
||||
public Page<RankedEntry> fetchByRanking(Instant date, double gravity, int page,
|
||||
int size) {
|
||||
return entryRepository.findAllByRanking(date, gravity, PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param order
|
||||
* @return
|
||||
* Fetch by date.
|
||||
*
|
||||
* @param date the date
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @return the page
|
||||
*/
|
||||
public Page<Entry> fetchByDate(Instant date, int page, int size) {
|
||||
return entryRepository.findAll(qEntry.created.before(date),
|
||||
@@ -68,53 +80,69 @@ public class EntryManager {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @return
|
||||
* Gets the entries.
|
||||
*
|
||||
* @param username the username
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @return the entries
|
||||
*/
|
||||
public Page<RankedEntry> directFetchByRanking(Instant date, double gravity, int page,
|
||||
int size) {
|
||||
return entryRepository.findAllByRanking(date, gravity, PageRequest.of(page, size));
|
||||
public Page<Entry> 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"))));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entry
|
||||
* 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))));
|
||||
.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))));
|
||||
entry.getMetadata().put("unvote", voteRepository.exists(qVote.target.eq(entry.getId())
|
||||
.and(qVote.targetType.eq(Types.entry)).and(qVote.author.eq(username))));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entries
|
||||
* Apply metadata.
|
||||
*
|
||||
* @param username the username
|
||||
* @param entries the entries
|
||||
*/
|
||||
public void applyMetadata(String username, List<Entry> entries) {
|
||||
for (Entry entry : entries) {
|
||||
@@ -123,32 +151,39 @@ public class EntryManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
* Exists.
|
||||
*
|
||||
* @param id the id
|
||||
* @return true, if successful
|
||||
*/
|
||||
public boolean exists(Long id) {
|
||||
return entryRepository.existsById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
* Gets the.
|
||||
*
|
||||
* @param id the id
|
||||
* @return the entry
|
||||
*/
|
||||
public Entry get(Long id) {
|
||||
return entryRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entry
|
||||
* @return
|
||||
* Save.
|
||||
*
|
||||
* @param entry the entry
|
||||
* @return the entry
|
||||
*/
|
||||
public Entry save(Entry entry) {
|
||||
return entryRepository.save(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entry
|
||||
* Delete.
|
||||
*
|
||||
* @param entry the entry
|
||||
*/
|
||||
public void delete(Entry entry) {
|
||||
commentManager.deleteByTarget(entry.getId());
|
||||
@@ -157,9 +192,10 @@ public class EntryManager {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entryId
|
||||
* @return
|
||||
* 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)
|
||||
|
||||
Reference in New Issue
Block a user