change voting

This commit is contained in:
_Bastler 2021-10-04 11:40:51 +02:00
parent 1ba9ca32e4
commit 1fc18fdeb2
4 changed files with 63 additions and 27 deletions

View File

@ -10,7 +10,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version> <java.version>11</java.version>
<revision>0.3.0-SNAPSHOT</revision> <revision>0.3.1-SNAPSHOT</revision>
</properties> </properties>
<parent> <parent>

View File

@ -112,25 +112,27 @@ public class CommentManager {
} }
if (!comment.getMetadata().containsKey("points")) { if (!comment.getMetadata().containsKey("points")) {
comment.getMetadata().put("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", if (!comment.getMetadata().containsKey("upvoted")) {
!voteRepository.exists(qVote.target.eq(comment.getId()) 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.targetType.eq(Types.comment)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.eq(username)))); .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()) voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment)) .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() if (!comment.getMetadata().containsKey("unvote")) {
.put("downvote", comment.getMetadata().put("unvote",
!voteRepository.exists(qVote.target.eq(comment.getId()) voteRepository.exists(
.and(qVote.targetType.eq(Types.comment)) qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment))
.and(qVote.author.eq(username)))); .and(qVote.author.eq(username))));
} }
} }

View File

@ -89,22 +89,26 @@ public class EntryManager {
if (!entry.getMetadata().containsKey("points")) { if (!entry.getMetadata().containsKey("points")) {
entry.getMetadata().put("points", voteManager.getPoints(entry.getId(), Types.entry)); entry.getMetadata().put("points", voteManager.getPoints(entry.getId(), Types.entry));
} }
if (!entry.getMetadata().containsKey("vote")) {
entry.getMetadata().put("vote", if (!entry.getMetadata().containsKey("upvoted")) {
!voteRepository.exists(qVote.target.eq(entry.getId()) 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.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.eq(username)))); .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()) voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)) .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", if (!entry.getMetadata().containsKey("unvote")) {
!voteRepository.exists(qVote.target.eq(entry.getId()) entry.getMetadata().put("unvote",
.and(qVote.targetType.eq(Types.entry)).and(qVote.author.eq(username)))); voteRepository.exists(
qVote.target.eq(entry.getId()).and(qVote.targetType.eq(Types.entry))
.and(qVote.author.eq(username))));
} }
} }

View File

@ -47,7 +47,7 @@ public class VoteController extends BaseController {
} }
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@PutMapping("/e/{id}") @PutMapping("/e/{id}/up")
public void voteEntryUp(@PathVariable("id") Long id) { public void voteEntryUp(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) { if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -68,7 +68,7 @@ public class VoteController extends BaseController {
} }
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@DeleteMapping("/e/{id}") @PutMapping("/e/{id}/down")
public void voteEntryDown(@PathVariable("id") Long id) { public void voteEntryDown(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) { if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); 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()") @PreAuthorize("isAuthenticated()")
@GetMapping("/c/{id}") @GetMapping("/c/{id}")
public long getCommentPoints(@PathVariable("id") Long id) { public long getCommentPoints(@PathVariable("id") Long id) {
@ -99,7 +114,7 @@ public class VoteController extends BaseController {
} }
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@PutMapping("/c/{id}") @PutMapping("/c/{id}/up")
public void voteCommentUp(@PathVariable("id") Long id) { public void voteCommentUp(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) { if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -120,7 +135,7 @@ public class VoteController extends BaseController {
} }
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@DeleteMapping("/c/{id}") @PutMapping("/c/{id}/down")
public void voteCommentDown(@PathVariable("id") Long id) { public void voteCommentDown(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) { if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); 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);
}
} }