diff --git a/src/main/java/de/bstly/board/businesslogic/CommentManager.java b/src/main/java/de/bstly/board/businesslogic/CommentManager.java index 8005e8a..bc0dde5 100644 --- a/src/main/java/de/bstly/board/businesslogic/CommentManager.java +++ b/src/main/java/de/bstly/board/businesslogic/CommentManager.java @@ -182,4 +182,28 @@ public class CommentManager { return upvotes - downvotes; } + /** + * + * @param comment + */ + public void delete(Comment comment) { + for (Comment subcomment : commentRepository.findAll(qComment.parent.eq(comment.getId()))) { + delete(subcomment); + } + + voteManager.deleteByTarget(comment.getId(), Types.comment); + + commentRepository.delete(comment); + } + + /** + * + * @param target + */ + public void deleteByTarget(Long target) { + for (Comment comment : commentRepository.findAll(qComment.target.eq(target))) { + delete(comment); + } + } + } diff --git a/src/main/java/de/bstly/board/businesslogic/EntryManager.java b/src/main/java/de/bstly/board/businesslogic/EntryManager.java index 45d4fdf..370e163 100644 --- a/src/main/java/de/bstly/board/businesslogic/EntryManager.java +++ b/src/main/java/de/bstly/board/businesslogic/EntryManager.java @@ -142,6 +142,16 @@ public class EntryManager { return entryRepository.save(entry); } + /** + * + * @param entry + */ + public void delete(Entry entry) { + commentManager.deleteByTarget(entry.getId()); + voteManager.deleteByTarget(entry.getId(), Types.entry); + entryRepository.delete(entry); + } + /** * * @param entryId diff --git a/src/main/java/de/bstly/board/businesslogic/VoteManager.java b/src/main/java/de/bstly/board/businesslogic/VoteManager.java index 74b161f..2ea35e1 100644 --- a/src/main/java/de/bstly/board/businesslogic/VoteManager.java +++ b/src/main/java/de/bstly/board/businesslogic/VoteManager.java @@ -52,6 +52,17 @@ public class VoteManager { voteRepository.delete(vote); } + /** + * + * @param vote + */ + public void deleteByTarget(Long target, Types targetType) { + for (Vote vote : voteRepository + .findAll(qVote.target.eq(target).and(qVote.targetType.eq(targetType)))) { + delete(vote); + } + } + /** * * @param target diff --git a/src/main/java/de/bstly/board/controller/DebugController.java b/src/main/java/de/bstly/board/controller/DebugController.java index 3e57c37..99f9180 100644 --- a/src/main/java/de/bstly/board/controller/DebugController.java +++ b/src/main/java/de/bstly/board/controller/DebugController.java @@ -12,6 +12,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -87,6 +88,7 @@ public class DebugController extends BaseController { * * @return */ + @PreAuthorize("hasRole('ROLE_ADMIN')") @GetMapping("/random") public void random() { logger.warn("start random generation"); @@ -96,17 +98,23 @@ public class DebugController extends BaseController { for (long i = userCount; i < userCount + users; i++) { LocalUser localUser = new LocalUser(); - String username = "user" + i; + String username = "user" + + i; localUser.setUsername(username); localUser.setPasswordHash(passwordEncoder.encode(username)); localUserRepository.save(localUser); - logger.trace("Created user: '" + username + "'"); + logger.trace("Created user: '" + + username + + "'"); } - logger.info("Created " + users + " users"); + logger.info("Created " + + users + + " users"); for (long id = 0; id <= userCount; id++) { - entries("user" + id, userCount); + entries("user" + + id, userCount); } logger.warn("finished random generation"); @@ -124,27 +132,40 @@ public class DebugController extends BaseController { entry.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500))); entry.setEntryStatus(EntryStatus.NORMAL); entry = entryManager.save(entry); - logger.trace("Created entry: '" + entry.getId() + "'"); + logger.trace("Created entry: '" + + entry.getId() + + "'"); comments(entry.getId(), entry.getCreated(), userCount); votes(entry.getId(), Types.entry, userCount); } - logger.info("Created " + numEntries + " entries of '" + username + "'"); + logger.info("Created " + + numEntries + + " entries of '" + + username + + "'"); } protected void comments(Long target, Instant date, long userCount) { long numComments = RandomUtils.nextLong(minComments, maxComments); - logger.debug("Create " + numComments + " comments for '" + target + "'"); + logger.debug("Create " + + numComments + + " comments for '" + + target + + "'"); for (int i = 0; i < numComments; i++) { Comment comment = new Comment(); comment.setTarget(target); - comment.setAuthor("user" + RandomUtils.nextLong(0, userCount)); + comment.setAuthor("user" + + RandomUtils.nextLong(0, userCount)); comment.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500))); comment.setCreated(Instant.now() .minus(RandomUtils.nextLong(0, (Instant.now().toEpochMilli() - date.toEpochMilli()) / 1000), ChronoUnit.SECONDS)); comment = commentRepository.save(comment); - logger.trace("Created comment: '" + comment.getId() + "'"); + logger.trace("Created comment: '" + + comment.getId() + + "'"); subComments(target, comment.getId(), comment.getCreated(), subCommentsFactor, subCommentsThresh, 0, userCount); } @@ -154,19 +175,26 @@ public class DebugController extends BaseController { int depth, long userCount) { if (depth < subCommentsDepth && RandomUtils.nextDouble(0, 1) < thresh) { long numSubComments = RandomUtils.nextLong(0, Math.round(maxComments * factor)); - logger.debug("Create " + numSubComments + " subComments for '" + parent + "'"); + logger.debug("Create " + + numSubComments + + " subComments for '" + + parent + + "'"); for (int i = 0; i < numSubComments; i++) { Comment comment = new Comment(); comment.setTarget(target); comment.setParent(parent); - comment.setAuthor("user" + RandomUtils.nextLong(0, userCount)); + comment.setAuthor("user" + + RandomUtils.nextLong(0, userCount)); comment.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500))); comment.setCreated(Instant.now() .minus(RandomUtils.nextLong(0, (Instant.now().toEpochMilli() - date.toEpochMilli()) / 1000), ChronoUnit.SECONDS)); comment = commentRepository.save(comment); - logger.trace("Created subComment: '" + comment.getId() + "'"); + logger.trace("Created subComment: '" + + comment.getId() + + "'"); subComments(target, comment.getId(), comment.getCreated(), factor * 0.5, thresh * 0.5, depth++, userCount); } @@ -175,27 +203,41 @@ public class DebugController extends BaseController { protected void votes(Long target, Types targetType, long userCount) { long numUpvotes = RandomUtils.nextLong(minUpvotes, maxUpvotes); - logger.debug("Create " + numUpvotes + " upvotes for '" + target + "'"); + logger.debug("Create " + + numUpvotes + + " upvotes for '" + + target + + "'"); for (int i = 0; i < numUpvotes; i++) { Vote upvote = new Vote(); upvote.setTarget(target); upvote.setType(VoteType.up); upvote.setTargetType(targetType); - upvote.setAuthor("user" + RandomUtils.nextLong(0, userCount)); + upvote.setAuthor("user" + + RandomUtils.nextLong(0, userCount)); upvote = voteRepository.save(upvote); - logger.trace("Created upvote: '" + upvote.getId() + "'"); + logger.trace("Created upvote: '" + + upvote.getId() + + "'"); } long numDownvotes = RandomUtils.nextLong(minDownvotes, maxDownvotes); - logger.debug("Create " + numDownvotes + " downvotes for '" + target + "'"); + logger.debug("Create " + + numDownvotes + + " downvotes for '" + + target + + "'"); for (int i = 0; i < numDownvotes; i++) { Vote downvote = new Vote(); downvote.setTarget(target); downvote.setType(VoteType.down); downvote.setTargetType(targetType); - downvote.setAuthor("user" + RandomUtils.nextLong(0, userCount)); + downvote.setAuthor("user" + + RandomUtils.nextLong(0, userCount)); downvote = voteRepository.save(downvote); - logger.trace("Created downvote: '" + downvote.getId() + "'"); + logger.trace("Created downvote: '" + + downvote.getId() + + "'"); } } diff --git a/src/main/java/de/bstly/board/controller/ModerationController.java b/src/main/java/de/bstly/board/controller/ModerationController.java new file mode 100644 index 0000000..acd7a27 --- /dev/null +++ b/src/main/java/de/bstly/board/controller/ModerationController.java @@ -0,0 +1,50 @@ +/** + * + */ +package de.bstly.board.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import de.bstly.board.businesslogic.CommentManager; +import de.bstly.board.businesslogic.EntryManager; +import de.bstly.board.controller.support.EntityResponseStatusException; + +/** + * @author Lurkars + * + */ +@RestController +@RequestMapping("/m") +public class ModerationController { + + @Autowired + private CommentManager commentManager; + @Autowired + private EntryManager entryManager; + + @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')") + @DeleteMapping("/c/{id}") + public void deleteComment(@PathVariable("id") Long id) { + if (!commentManager.exists(id)) { + throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); + } + + commentManager.delete(commentManager.get(id)); + } + + @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')") + @DeleteMapping("/e/{id}") + public void deleteEntry(@PathVariable("id") Long id) { + if (!entryManager.exists(id)) { + throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); + } + + entryManager.delete(entryManager.get(id)); + } +}