improvements + bookmarks

This commit is contained in:
2021-10-04 13:02:40 +02:00
parent 1fc18fdeb2
commit d3f6c86db6
48 changed files with 1542 additions and 432 deletions
@@ -0,0 +1,97 @@
/**
*
*/
package de.bstly.board.businesslogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import com.google.common.collect.Lists;
import de.bstly.board.model.Bookmarks;
import de.bstly.board.model.QBookmarks;
import de.bstly.board.repository.BookmarksRepository;
import de.bstly.board.repository.EntryRepository;
import de.bstly.board.repository.LocalUserRepository;
/**
* The Class BookmarksManager.
*/
@Component
public class BookmarksManager {
@Autowired
private BookmarksRepository bookmarksRepository;
@Autowired
private EntryRepository entryRepository;
@Autowired
private LocalUserRepository localUserRepository;
private QBookmarks qBookmarks = QBookmarks.bookmarks;
/**
* Gets the.
*
* @param username the username
* @return the bookmarks
*/
public Bookmarks get(String username) {
return bookmarksRepository.findById(username).orElse(new Bookmarks(username));
}
/**
* Checks for entry.
*
* @param username the username
* @param entryId the entry id
* @return true, if successful
*/
public boolean hasEntry(String username, Long entryId) {
return bookmarksRepository
.exists(qBookmarks.username.eq(username).and(qBookmarks.entries.contains(entryId)));
}
/**
* Adds the entry.
*
* @param username the username
* @param entryId the entry id
*/
public void addEntry(String username, Long entryId) {
Assert.isTrue(entryRepository.existsById(entryId), "Invalid entryid");
Assert.isTrue(localUserRepository.existsById(username), "Invalid username");
Bookmarks bookmarks = get(username);
if (bookmarks.getEntries() == null) {
bookmarks.setEntries(Lists.newArrayList());
}
if (!hasEntry(username, entryId)) {
bookmarks.getEntries().add(entryId);
bookmarksRepository.save(bookmarks);
}
}
/**
* Removes the entry.
*
* @param username the username
* @param entryId the entry id
*/
public void removeEntry(String username, Long entryId) {
Assert.isTrue(entryRepository.existsById(entryId), "Invalid entryid");
Assert.isTrue(localUserRepository.existsById(username), "Invalid username");
Bookmarks bookmarks = get(username);
if (bookmarks.getEntries() == null) {
bookmarks.setEntries(Lists.newArrayList());
}
if (hasEntry(username, entryId)) {
bookmarks.getEntries().remove(entryId);
bookmarksRepository.save(bookmarks);
}
}
}
@@ -22,28 +22,34 @@ import de.bstly.board.repository.CommentRepository;
import de.bstly.board.repository.VoteRepository;
/**
* @author Lurkars
*
* The Class CommentManager.
*/
@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
* Fetch by ranking.
*
* @param target the target
* @param parent the parent
* @param date the date
* @param gravity the gravity
* @param page the page
* @param size the size
* @return the page
*/
public Page<Comment> fetchByRanking(Long target, Long parent, Instant date, double gravity,
@@ -58,11 +64,14 @@ public class CommentManager {
}
/**
*
* @param page
* @param size
* @param order
* @return
* Fetch by date.
*
* @param target the target
* @param parent the parent
* @param date the date
* @param page the page
* @param size the size
* @return the page
*/
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size) {
if (parent == null) {
@@ -79,10 +88,11 @@ public class CommentManager {
}
/**
*
* @param target
* @param parent
* @return
* Count.
*
* @param target the target
* @param parent the parent
* @return the long
*/
public Long count(Long target, Long parent) {
if (parent == null) {
@@ -93,18 +103,20 @@ public class CommentManager {
}
/**
*
* @param target
* @return
* Count.
*
* @param target the target
* @return the long
*/
public Long count(Long target) {
return commentRepository.count(qComment.target.eq(target));
}
/**
*
* @param username
* @param comment
* Apply metadata.
*
* @param username the username
* @param comment the comment
*/
public void applyMetadata(String username, Comment comment) {
if (!comment.getMetadata().containsKey("comments")) {
@@ -114,21 +126,21 @@ public class CommentManager {
comment.getMetadata().put("points",
voteManager.getPoints(comment.getId(), Types.comment));
}
if (!comment.getMetadata().containsKey("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")) {
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")) {
comment.getMetadata().put("unvote",
voteRepository.exists(
@@ -138,8 +150,10 @@ public class CommentManager {
}
/**
*
* @param entries
* Apply metadata.
*
* @param username the username
* @param entries the entries
*/
public void applyMetadata(String username, List<Comment> entries) {
for (Comment comment : entries) {
@@ -148,33 +162,40 @@ public class CommentManager {
}
/**
* @param id
* @return
* Exists.
*
* @param id the id
* @return true, if successful
*/
public boolean exists(Long id) {
return commentRepository.existsById(id);
}
/**
* @param id
* @return
* Gets the.
*
* @param id the id
* @return the comment
*/
public Comment get(Long id) {
return commentRepository.findById(id).orElse(null);
}
/**
* @param comment
* @return
* Save.
*
* @param comment the comment
* @return the comment
*/
public Comment save(Comment comment) {
return commentRepository.save(comment);
}
/**
*
* @param commentId
* @return
* Gets the points.
*
* @param commentId the comment id
* @return the points
*/
public long getPoints(Long commentId) {
long upvotes = voteRepository.count(qVote.targetType.eq(Types.comment)
@@ -185,8 +206,9 @@ public class CommentManager {
}
/**
*
* @param comment
* Delete.
*
* @param comment the comment
*/
public void delete(Comment comment) {
for (Comment subcomment : commentRepository.findAll(qComment.parent.eq(comment.getId()))) {
@@ -199,8 +221,9 @@ public class CommentManager {
}
/**
*
* @param target
* Delete by target.
*
* @param target the target
*/
public void deleteByTarget(Long target) {
for (Comment comment : commentRepository.findAll(qComment.target.eq(target))) {
@@ -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)
@@ -12,48 +12,51 @@ import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
/**
* @author _bastler@bstly.de
*
* The Class InstantHelper.
*/
public class InstantHelper {
/**
*
* @param instant
* @param amount
* @return
* Plus.
*
* @param instant the instant
* @param amount the amount
* @return the instant
*/
public static Instant plus(Instant instant, TemporalAmount amount) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).plus(amount).toInstant();
}
/**
*
* @param instant
* @param amountToAdd
* @param unit
* @return
* Plus.
*
* @param instant the instant
* @param amountToAdd the amount to add
* @param unit the unit
* @return the instant
*/
public static Instant plus(Instant instant, long amountToAdd, TemporalUnit unit) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).plus(amountToAdd, unit).toInstant();
}
/**
*
* @param instant
* @param amount
* @return
* Minus.
*
* @param instant the instant
* @param amount the amount
* @return the instant
*/
public static Instant minus(Instant instant, TemporalAmount amount) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amount).toInstant();
}
/**
*
* @param instant
* @param amountToAdd
* @param unit
* @return
* Minus.
*
* @param instant the instant
* @param amountToAdd the amount to add
* @param unit the unit
* @return the instant
*/
public static Instant minus(Instant instant, long amountToAdd, TemporalUnit unit) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amountToAdd, unit)
@@ -61,10 +64,11 @@ public class InstantHelper {
}
/**
*
* @param instant
* @param unit
* @return
* Truncate.
*
* @param instant the instant
* @param unit the unit
* @return the instant
*/
public static Instant truncate(Instant instant, TemporalUnit unit) {
if (ChronoUnit.YEARS.equals(unit)) {
@@ -34,8 +34,7 @@ import de.bstly.board.repository.EntryRepository;
import de.bstly.board.repository.LocalUserRepository;
/**
* @author monitoring@bstly.de
*
* The Class UserManager.
*/
@Service
public class UserManager implements UserDetailsService, SmartInitializingSingleton {
@@ -44,18 +43,27 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
@Autowired
private LocalUserRepository localUserRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private EntryManager entryManager;
@Autowired
private EntryRepository entryRepository;
private QLocalUser qLocalUser = QLocalUser.localUser;
private QEntry qEntry = QEntry.entry;
@Value("${admin.password:}")
private String adminPassword;
/*
* @see org.springframework.security.core.userdetails.UserDetailsService#
* loadUserByUsername(java.lang.String)
*/
/*
* @see
* de.bstly.board.businesslogic.LocalUserManager#loadUserByUsername(java.lang.
@@ -79,6 +87,10 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
return new User(username, localUser.getPasswordHash(), authorities);
}
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
/*
*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
@@ -102,27 +114,30 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
}
/**
*
* @param username
* @return
* Gets the by username.
*
* @param username the username
* @return the by username
*/
public LocalUser getByUsername(String username) {
return localUserRepository.findById(username).orElse(null);
}
/**
*
* @param externalId
* @return
* Gets the by external id.
*
* @param externalId the external id
* @return the by external id
*/
public LocalUser getByExternalId(String externalId) {
return localUserRepository.findOne(qLocalUser.externalId.eq(externalId)).orElse(null);
}
/**
*
* @param authentication
* @return
* Gets the by auth.
*
* @param authentication the authentication
* @return the by auth
*/
public LocalUser getByAuth(Authentication authentication) {
if (authentication != null) {
@@ -181,8 +196,10 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
}
/**
*
* @param user
* Apply metadata.
*
* @param username the username
* @param user the user
*/
public void applyMetadata(String username, LocalUser user) {
if (user.getUsername().equals(username) && !user.getMetadata().containsKey("self")) {
@@ -199,17 +216,20 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
}
/**
*
* @param localUser
* @return
* Save.
*
* @param localUser the local user
* @return the local user
*/
public LocalUser save(LocalUser localUser) {
return localUserRepository.save(localUser);
}
/**
* @param username
* @return
* Exists.
*
* @param username the username
* @return true, if successful
*/
public boolean exists(String username) {
return localUserRepository.existsById(username);
@@ -13,22 +13,23 @@ import de.bstly.board.model.VoteType;
import de.bstly.board.repository.VoteRepository;
/**
* @author Lurkars
*
* The Class VoteManager.
*/
@Component
public class VoteManager {
@Autowired
private VoteRepository voteRepository;
private QVote qVote = QVote.vote;
/**
*
* @param author
* @param targetType
* @param target
* @return
* Gets the.
*
* @param author the author
* @param targetType the target type
* @param target the target
* @return the vote
*/
public Vote get(String author, Types targetType, Long target) {
return voteRepository.findOne(qVote.author.eq(author).and(qVote.targetType.eq(targetType))
@@ -36,25 +37,29 @@ public class VoteManager {
}
/**
*
* @param vote
* @return
* Save.
*
* @param vote the vote
* @return the vote
*/
public Vote save(Vote vote) {
return voteRepository.save(vote);
}
/**
*
* @param vote
* Delete.
*
* @param vote the vote
*/
public void delete(Vote vote) {
voteRepository.delete(vote);
}
/**
*
* @param vote
* Delete by target.
*
* @param target the target
* @param targetType the target type
*/
public void deleteByTarget(Long target, Types targetType) {
for (Vote vote : voteRepository
@@ -64,10 +69,11 @@ public class VoteManager {
}
/**
*
* @param target
* @param targetType
* @return
* Gets the points.
*
* @param target the target
* @param targetType the target type
* @return the points
*/
public Long getPoints(Long target, Types targetType) {
return voteRepository
@@ -11,49 +11,53 @@ import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
/**
* @author _bastler@bstly.de
*
* The Class InstantHelper.
*/
public class InstantHelper {
/**
*
* @param instant
* @param amount
* @return
* Plus.
*
* @param instant the instant
* @param amount the amount
* @return the instant
*/
public static Instant plus(Instant instant, TemporalAmount amount) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).plus(amount).toInstant();
}
/**
*
* @param instant
* @param amountToAdd
* @param unit
* @return
* Plus.
*
* @param instant the instant
* @param amountToAdd the amount to add
* @param unit the unit
* @return the instant
*/
public static Instant plus(Instant instant, long amountToAdd, TemporalUnit unit) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).plus(amountToAdd, unit).toInstant();
}
/**
*
* @param instant
* @param amount
* @return
* Minus.
*
* @param instant the instant
* @param amount the amount
* @return the instant
*/
public static Instant minus(Instant instant, TemporalAmount amount) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amount).toInstant();
}
/**
*
* @param instant
* @param amountToAdd
* @param unit
* @return
* Minus.
*
* @param instant the instant
* @param amountToAdd the amount to add
* @param unit the unit
* @return the instant
*/
public static Instant minus(Instant instant, long amountToAdd, TemporalUnit unit) {
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amountToAdd, unit)
@@ -61,10 +65,11 @@ public class InstantHelper {
}
/**
*
* @param instant
* @param unit
* @return
* Truncate.
*
* @param instant the instant
* @param unit the unit
* @return the instant
*/
public static Instant truncate(Instant instant, TemporalUnit unit) {
if (ChronoUnit.YEARS.equals(unit)) {