From a8c5515c11845fe6b953d5f81a04deb6066d0390 Mon Sep 17 00:00:00 2001 From: _Bastler Date: Thu, 7 Oct 2021 14:35:15 +0200 Subject: [PATCH] improve flagging --- .../board/businesslogic/CommentManager.java | 20 +++++++++++++-- .../board/businesslogic/EntryManager.java | 25 ++++++++++++++++--- .../board/businesslogic/UserManager.java | 6 +++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/bstly/board/businesslogic/CommentManager.java b/src/main/java/de/bstly/board/businesslogic/CommentManager.java index 5a86686..b8db280 100644 --- a/src/main/java/de/bstly/board/businesslogic/CommentManager.java +++ b/src/main/java/de/bstly/board/businesslogic/CommentManager.java @@ -8,17 +8,21 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; 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.querydsl.core.types.OrderSpecifier; +import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import de.bstly.board.model.Comment; import de.bstly.board.model.FlaggedStatus; import de.bstly.board.model.QComment; import de.bstly.board.model.QEntry; +import de.bstly.board.model.QFlag; import de.bstly.board.model.QVote; import de.bstly.board.model.Types; import de.bstly.board.model.VoteType; @@ -44,6 +48,7 @@ public class CommentManager { private QComment qComment = QComment.comment; private QVote qVote = QVote.vote; + private QFlag qFlag = QFlag.flag; private QEntry qEntry = QEntry.entry; /** @@ -86,8 +91,14 @@ public class CommentManager { */ public Page fetchFlagged(int page, int size, boolean asc) { Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created")); - return commentRepository.findAll(qComment.flaggedStatus.eq(FlaggedStatus.FLAGGED), - PageRequest.of(page, size, sort)); + JPAQuery query = jpaQueryFactory.selectFrom(qComment).leftJoin(qFlag) + .on(qComment.id.eq(qFlag.target)).where(qFlag.targetType.eq(Types.comment)) + .groupBy(qFlag.target); + return new PageImpl(query + .orderBy(new OrderSpecifier<>(asc ? com.querydsl.core.types.Order.ASC + : com.querydsl.core.types.Order.DESC, qComment.created)) + .limit(size).offset(page * size).fetch(), PageRequest.of(page, size, sort), + query.fetchCount()); } /** @@ -193,6 +204,11 @@ public class CommentManager { flagManager.get(username, comment.getId(), Types.comment) != null); } + if (!ignore.contains("flagged")) { + comment.getMetadata().put("flagged", + flagManager.getFlags(comment.getId(), Types.comment) > 0); + } + if (!ignore.contains("entry")) { comment.getMetadata().put("entry", jpaQueryFactory.selectFrom(qEntry) .where(qEntry.id.eq(comment.getTarget())).select(qEntry.title).fetchOne()); diff --git a/src/main/java/de/bstly/board/businesslogic/EntryManager.java b/src/main/java/de/bstly/board/businesslogic/EntryManager.java index f78ca70..d25d7f7 100644 --- a/src/main/java/de/bstly/board/businesslogic/EntryManager.java +++ b/src/main/java/de/bstly/board/businesslogic/EntryManager.java @@ -8,18 +8,22 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; 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 com.querydsl.core.types.OrderSpecifier; +import com.querydsl.jpa.impl.JPAQuery; +import com.querydsl.jpa.impl.JPAQueryFactory; import de.bstly.board.model.Bookmarks; import de.bstly.board.model.Entry; import de.bstly.board.model.EntryStatus; -import de.bstly.board.model.FlaggedStatus; import de.bstly.board.model.QEntry; +import de.bstly.board.model.QFlag; import de.bstly.board.model.QVote; import de.bstly.board.model.RankedEntry; import de.bstly.board.model.Types; @@ -47,10 +51,12 @@ public class EntryManager { private SettingsManager settingsManager; @Autowired private FlagManager flagManager; + @Autowired + private JPAQueryFactory jpaQueryFactory; private QEntry qEntry = QEntry.entry; - private QVote qVote = QVote.vote; + private QFlag qFlag = QFlag.flag; /** * Fetch by ranking. @@ -114,8 +120,14 @@ public class EntryManager { */ public Page fetchFlagged(int page, int size, boolean asc) { Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created")); - return entryRepository.findAll(qEntry.flaggedStatus.ne(FlaggedStatus.NORMAL), - PageRequest.of(page, size, sort)); + JPAQuery query = jpaQueryFactory.selectFrom(qEntry).leftJoin(qFlag) + .on(qEntry.id.eq(qFlag.target)).where(qFlag.targetType.eq(Types.entry)) + .groupBy(qFlag.target); + return new PageImpl(query + .orderBy(new OrderSpecifier<>(asc ? com.querydsl.core.types.Order.ASC + : com.querydsl.core.types.Order.DESC, qEntry.created)) + .limit(size).offset(page * size).fetch(), PageRequest.of(page, size, sort), + query.fetchCount()); } /** @@ -208,6 +220,11 @@ public class EntryManager { flagManager.get(username, entry.getId(), Types.entry) != null); } + if (!ignore.contains("flagged")) { + entry.getMetadata().put("flagged", + flagManager.getFlags(entry.getId(), Types.entry) > 0); + } + if (voteRepository .exists(qVote.target.eq(entry.getId()).and(qVote.targetType.eq(Types.entry)) .and(qVote.author.equalsIgnoreCase(username)))) { diff --git a/src/main/java/de/bstly/board/businesslogic/UserManager.java b/src/main/java/de/bstly/board/businesslogic/UserManager.java index 8e5996c..4099cdc 100644 --- a/src/main/java/de/bstly/board/businesslogic/UserManager.java +++ b/src/main/java/de/bstly/board/businesslogic/UserManager.java @@ -104,6 +104,9 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet /* * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String) */ + /* + * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String) + */ /* * @see org.springframework.security.core.userdetails.UserDetailsService# * loadUserByUsername(java.lang.String) @@ -182,6 +185,9 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet /* * @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated() */ + /* + * @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated() + */ /* * @see org.springframework.beans.factory.SmartInitializingSingleton# * afterSingletonsInstantiated()