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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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 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;
|
||||
|
||||
/**
|
||||
* @author Lurkars
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class EntryManager {
|
||||
|
||||
@Autowired
|
||||
private EntryRepository entryRepository;
|
||||
@Autowired
|
||||
private CommentManager commentManager;
|
||||
@Autowired
|
||||
private VoteManager voteManager;
|
||||
@Autowired
|
||||
private VoteRepository voteRepository;
|
||||
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
|
||||
*/
|
||||
public Page<Entry> fetchByRanking(int page, int size) {
|
||||
return entryRepository.findAll(
|
||||
PageRequest.of(page, size, Sort.by(Order.desc("ranking"), Order.desc("created"))));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param order
|
||||
* @return
|
||||
*/
|
||||
public Page<Entry> fetchByDate(Instant date, int page, int size) {
|
||||
return entryRepository.findAll(qEntry.created.before(date),
|
||||
PageRequest.of(page, size, Sort.by(Order.desc("created"))));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
public Page<RankedEntry> directFetchByRanking(Instant date, double gravity, int page,
|
||||
int size) {
|
||||
return entryRepository.findAllByRanking(date, gravity, PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param 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("vote")) {
|
||||
entry.getMetadata().put("vote",
|
||||
!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("unvote")) {
|
||||
entry.getMetadata().put("unvote",
|
||||
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("downvote")) {
|
||||
entry.getMetadata().put("downvote",
|
||||
!voteRepository.exists(qVote.target.eq(entry.getId())
|
||||
.and(qVote.targetType.eq(Types.entry)).and(qVote.author.eq(username))));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entries
|
||||
*/
|
||||
public void applyMetadata(String username, List<Entry> entries) {
|
||||
for (Entry entry : entries) {
|
||||
applyMetadata(username, entry);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(Long id) {
|
||||
return entryRepository.existsById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public Entry get(Long id) {
|
||||
return entryRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entry
|
||||
* @return
|
||||
*/
|
||||
public Entry save(Entry entry) {
|
||||
return entryRepository.save(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entryId
|
||||
* @return
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.board.businesslogic;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAmount;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
|
||||
/**
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
public class InstantHelper {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param amount
|
||||
* @return
|
||||
*/
|
||||
public static Instant plus(Instant instant, TemporalAmount amount) {
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).plus(amount).toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param amountToAdd
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public static Instant minus(Instant instant, TemporalAmount amount) {
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amount).toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param amountToAdd
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
public static Instant minus(Instant instant, long amountToAdd, TemporalUnit unit) {
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amountToAdd, unit)
|
||||
.toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
public static Instant truncate(Instant instant, TemporalUnit unit) {
|
||||
if (ChronoUnit.YEARS.equals(unit)) {
|
||||
instant = instant.truncatedTo(ChronoUnit.DAYS);
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC)
|
||||
.with(ChronoField.DAY_OF_YEAR, 1L).toInstant();
|
||||
} else if (ChronoUnit.MONTHS.equals(unit)) {
|
||||
instant = instant.truncatedTo(ChronoUnit.DAYS);
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC)
|
||||
.with(ChronoField.DAY_OF_MONTH, 1L).toInstant();
|
||||
}
|
||||
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).truncatedTo(unit).toInstant();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.board.businesslogic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import de.bstly.board.model.Entry;
|
||||
import de.bstly.board.model.LocalUser;
|
||||
import de.bstly.board.model.QEntry;
|
||||
import de.bstly.board.model.QLocalUser;
|
||||
import de.bstly.board.repository.EntryRepository;
|
||||
import de.bstly.board.repository.LocalUserRepository;
|
||||
|
||||
/**
|
||||
* @author monitoring@bstly.de
|
||||
*
|
||||
*/
|
||||
@Service
|
||||
public class UserManager implements UserDetailsService, SmartInitializingSingleton {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(UserManager.class);
|
||||
|
||||
@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
|
||||
* de.bstly.board.businesslogic.LocalUserManager#loadUserByUsername(java.lang.
|
||||
* String)
|
||||
*/
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
LocalUser localUser = getByUsername(username);
|
||||
|
||||
if (localUser == null) {
|
||||
throw new UsernameNotFoundException(username);
|
||||
}
|
||||
|
||||
List<GrantedAuthority> authorities = Lists.newArrayList();
|
||||
if (localUser.getRoles() != null) {
|
||||
for (String role : localUser.getRoles()) {
|
||||
authorities.add(new SimpleGrantedAuthority(role));
|
||||
}
|
||||
}
|
||||
|
||||
return new User(username, localUser.getPasswordHash(), authorities);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
if (!localUserRepository.exists(qLocalUser.roles.contains("ROLE_ADMIN"))) {
|
||||
if (!StringUtils.hasText(adminPassword)) {
|
||||
adminPassword = RandomStringUtils.random(24, true, true);
|
||||
logger.error("password for 'admin': "
|
||||
+ adminPassword);
|
||||
}
|
||||
LocalUser admin = new LocalUser();
|
||||
admin.setUsername("admin");
|
||||
admin.setRoles(Lists.newArrayList("ROLE_ADMIN"));
|
||||
admin.setPasswordHash(passwordEncoder.encode(adminPassword));
|
||||
localUserRepository.save(admin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
public LocalUser getByUsername(String username) {
|
||||
return localUserRepository.findById(username).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param externalId
|
||||
* @return
|
||||
*/
|
||||
public LocalUser getByExternalId(String externalId) {
|
||||
return localUserRepository.findOne(qLocalUser.externalId.eq(externalId)).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param authentication
|
||||
* @return
|
||||
*/
|
||||
public LocalUser getByAuth(Authentication authentication) {
|
||||
if (authentication != null) {
|
||||
if (authentication instanceof UsernamePasswordAuthenticationToken) {
|
||||
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
|
||||
return getByUsername(token.getName());
|
||||
} else if (authentication instanceof OAuth2AuthenticationToken) {
|
||||
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) authentication;
|
||||
String externalId = token.getAuthorizedClientRegistrationId()
|
||||
+ "-"
|
||||
+ token.getName();
|
||||
LocalUser localUser = getByExternalId(externalId);
|
||||
if (localUser == null) {
|
||||
localUser = new LocalUser();
|
||||
localUser.setExternalId(externalId);
|
||||
String tmpUsername = token.getPrincipal().getAttribute("preferred_username");
|
||||
if (!StringUtils.hasText(tmpUsername)) {
|
||||
tmpUsername = token.getPrincipal().getAttribute("username");
|
||||
}
|
||||
if (!StringUtils.hasText(tmpUsername)) {
|
||||
tmpUsername = token.getPrincipal().getAttribute("name");
|
||||
}
|
||||
if (!StringUtils.hasText(tmpUsername)) {
|
||||
tmpUsername = token.getName();
|
||||
}
|
||||
int count = 1;
|
||||
String username = tmpUsername;
|
||||
while (localUserRepository.existsById(username)) {
|
||||
username = tmpUsername
|
||||
+ "-"
|
||||
+ count;
|
||||
count++;
|
||||
}
|
||||
|
||||
localUser.setUsername(username);
|
||||
localUser.setEmail(token.getPrincipal().getAttribute("email"));
|
||||
|
||||
String locale = token.getPrincipal().getAttribute("locale");
|
||||
if (!StringUtils.hasText(locale)) {
|
||||
locale = "en";
|
||||
}
|
||||
localUser.setLocale(locale);
|
||||
|
||||
String darkTheme = token.getPrincipal().getAttribute("darkTheme");
|
||||
if (!StringUtils.hasText(darkTheme)) {
|
||||
darkTheme = "false";
|
||||
}
|
||||
localUser.setDarkTheme(Boolean.valueOf(darkTheme));
|
||||
|
||||
localUser = localUserRepository.save(localUser);
|
||||
}
|
||||
return localUser;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param user
|
||||
*/
|
||||
public void applyMetadata(String username, LocalUser user) {
|
||||
if (user.getUsername().equals(username) && !user.getMetadata().containsKey("self")) {
|
||||
user.getMetadata().put("self", true);
|
||||
}
|
||||
|
||||
if (!user.getMetadata().containsKey("points")) {
|
||||
long points = 0;
|
||||
for (Entry entry : entryRepository.findAll(qEntry.author.eq(username))) {
|
||||
points += entryManager.getPoints(entry.getId());
|
||||
}
|
||||
user.getMetadata().put("points", points);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param localUser
|
||||
* @return
|
||||
*/
|
||||
public LocalUser save(LocalUser localUser) {
|
||||
return localUserRepository.save(localUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(String username) {
|
||||
return localUserRepository.existsById(username);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.board.businesslogic;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import de.bstly.board.model.QVote;
|
||||
import de.bstly.board.model.Types;
|
||||
import de.bstly.board.model.Vote;
|
||||
import de.bstly.board.model.VoteType;
|
||||
import de.bstly.board.repository.VoteRepository;
|
||||
|
||||
/**
|
||||
* @author Lurkars
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class VoteManager {
|
||||
|
||||
@Autowired
|
||||
private VoteRepository voteRepository;
|
||||
private QVote qVote = QVote.vote;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param author
|
||||
* @param targetType
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
public Vote get(String author, Types targetType, Long target) {
|
||||
return voteRepository.findOne(qVote.author.eq(author).and(qVote.targetType.eq(targetType))
|
||||
.and(qVote.target.eq(target))).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param vote
|
||||
* @return
|
||||
*/
|
||||
public Vote save(Vote vote) {
|
||||
return voteRepository.save(vote);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param vote
|
||||
*/
|
||||
public void delete(Vote vote) {
|
||||
voteRepository.delete(vote);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @param targetType
|
||||
* @return
|
||||
*/
|
||||
public Long getPoints(Long target, Types targetType) {
|
||||
return voteRepository
|
||||
.count(qVote.target.eq(target)
|
||||
.and(qVote.targetType.eq(targetType).and(qVote.type.eq(VoteType.up))))
|
||||
- voteRepository.count(qVote.target.eq(target)
|
||||
.and(qVote.targetType.eq(targetType).and(qVote.type.eq(VoteType.down))));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.board.businesslogic.support;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAmount;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
|
||||
/**
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
public class InstantHelper {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param amount
|
||||
* @return
|
||||
*/
|
||||
public static Instant plus(Instant instant, TemporalAmount amount) {
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).plus(amount).toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param amountToAdd
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public static Instant minus(Instant instant, TemporalAmount amount) {
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amount).toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param amountToAdd
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
public static Instant minus(Instant instant, long amountToAdd, TemporalUnit unit) {
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).minus(amountToAdd, unit)
|
||||
.toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param instant
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
public static Instant truncate(Instant instant, TemporalUnit unit) {
|
||||
if (ChronoUnit.YEARS.equals(unit)) {
|
||||
instant = instant.truncatedTo(ChronoUnit.DAYS);
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC)
|
||||
.with(ChronoField.DAY_OF_YEAR, 1L).toInstant();
|
||||
} else if (ChronoUnit.MONTHS.equals(unit)) {
|
||||
instant = instant.truncatedTo(ChronoUnit.DAYS);
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC)
|
||||
.with(ChronoField.DAY_OF_MONTH, 1L).toInstant();
|
||||
}
|
||||
|
||||
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC).truncatedTo(unit).toInstant();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user