From 1fc18fdeb286514d86188673bb2f4542da42905e Mon Sep 17 00:00:00 2001 From: _Bastler Date: Mon, 4 Oct 2021 11:40:51 +0200 Subject: [PATCH] change voting --- pom.xml | 2 +- .../board/businesslogic/CommentManager.java | 26 +++++++------ .../board/businesslogic/EntryManager.java | 24 +++++++----- .../board/controller/VoteController.java | 38 +++++++++++++++++-- 4 files changed, 63 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index 4edc28a..6b766af 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 11 - 0.3.0-SNAPSHOT + 0.3.1-SNAPSHOT diff --git a/src/main/java/de/bstly/board/businesslogic/CommentManager.java b/src/main/java/de/bstly/board/businesslogic/CommentManager.java index bc0dde5..2b6c94e 100644 --- a/src/main/java/de/bstly/board/businesslogic/CommentManager.java +++ b/src/main/java/de/bstly/board/businesslogic/CommentManager.java @@ -112,25 +112,27 @@ public class CommentManager { } if (!comment.getMetadata().containsKey("points")) { comment.getMetadata().put("points", - voteManager.getPoints(comment.getId(), Types.entry)); + voteManager.getPoints(comment.getId(), Types.comment)); } - if (!comment.getMetadata().containsKey("vote")) { - comment.getMetadata().put("vote", - !voteRepository.exists(qVote.target.eq(comment.getId()) + + 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("unvote")) { - comment.getMetadata().put("unvote", + + 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.up).and(qVote.author.eq(username))))); + .and(qVote.type.eq(VoteType.down)).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)) + + if (!comment.getMetadata().containsKey("unvote")) { + comment.getMetadata().put("unvote", + voteRepository.exists( + qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment)) .and(qVote.author.eq(username)))); } } diff --git a/src/main/java/de/bstly/board/businesslogic/EntryManager.java b/src/main/java/de/bstly/board/businesslogic/EntryManager.java index 370e163..c051457 100644 --- a/src/main/java/de/bstly/board/businesslogic/EntryManager.java +++ b/src/main/java/de/bstly/board/businesslogic/EntryManager.java @@ -89,22 +89,26 @@ public class EntryManager { 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()) + + 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("unvote")) { - entry.getMetadata().put("unvote", + + 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.up).and(qVote.author.eq(username))))); + .and(qVote.type.eq(VoteType.down)).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)))); + + 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)))); } } diff --git a/src/main/java/de/bstly/board/controller/VoteController.java b/src/main/java/de/bstly/board/controller/VoteController.java index fb5a907..b4a0f9c 100644 --- a/src/main/java/de/bstly/board/controller/VoteController.java +++ b/src/main/java/de/bstly/board/controller/VoteController.java @@ -47,7 +47,7 @@ public class VoteController extends BaseController { } @PreAuthorize("isAuthenticated()") - @PutMapping("/e/{id}") + @PutMapping("/e/{id}/up") public void voteEntryUp(@PathVariable("id") Long id) { if (!entryManager.exists(id)) { throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); @@ -68,7 +68,7 @@ public class VoteController extends BaseController { } @PreAuthorize("isAuthenticated()") - @DeleteMapping("/e/{id}") + @PutMapping("/e/{id}/down") public void voteEntryDown(@PathVariable("id") Long id) { if (!entryManager.exists(id)) { throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); @@ -88,6 +88,21 @@ public class VoteController extends BaseController { } } + @PreAuthorize("isAuthenticated()") + @DeleteMapping("/e/{id}") + public void unvoteEntry(@PathVariable("id") Long id) { + if (!entryManager.exists(id)) { + throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); + } + + Vote vote = voteManager.get(getCurrentUsername(), Types.entry, id); + if (vote == null) { + throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED); + } + + voteManager.delete(vote); + } + @PreAuthorize("isAuthenticated()") @GetMapping("/c/{id}") public long getCommentPoints(@PathVariable("id") Long id) { @@ -99,7 +114,7 @@ public class VoteController extends BaseController { } @PreAuthorize("isAuthenticated()") - @PutMapping("/c/{id}") + @PutMapping("/c/{id}/up") public void voteCommentUp(@PathVariable("id") Long id) { if (!commentManager.exists(id)) { throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); @@ -120,7 +135,7 @@ public class VoteController extends BaseController { } @PreAuthorize("isAuthenticated()") - @DeleteMapping("/c/{id}") + @PutMapping("/c/{id}/down") public void voteCommentDown(@PathVariable("id") Long id) { if (!commentManager.exists(id)) { throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); @@ -140,4 +155,19 @@ public class VoteController extends BaseController { } } + @PreAuthorize("isAuthenticated()") + @DeleteMapping("/c/{id}") + public void unvoteComment(@PathVariable("id") Long id) { + if (!commentManager.exists(id)) { + throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); + } + + Vote vote = voteManager.get(getCurrentUsername(), Types.comment, id); + if (vote == null) { + throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED); + } + + voteManager.delete(vote); + } + }