Skip to content

Commit

Permalink
feat: votes restcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr4ll committed Mar 19, 2023
1 parent 808597e commit 76f7d68
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package net.daw.alist.controllers.rest;

import net.daw.alist.models.Post;
import net.daw.alist.models.User;
import net.daw.alist.services.PostService;
import net.daw.alist.services.UserService;
import net.daw.alist.services.VotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
Expand All @@ -16,6 +20,11 @@ public class PostRestController {
@Autowired
private PostService postService;

@Autowired
private UserService userService;

@Autowired
private VotesService votesService;
@PostMapping("/")
@ResponseStatus(HttpStatus.CREATED)
public Post createPost(Post post) {
Expand Down Expand Up @@ -44,4 +53,39 @@ public ResponseEntity<Post> deletePost(@PathVariable long id) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@PutMapping("/{id}/upvote")
public ResponseEntity<Post> upvoteAction(Authentication authentication, @PathVariable long id) {
Optional<Post> optionalPost = postService.findByID(id);
if(!(authentication == null) && optionalPost.isPresent() ) {
User userSession = (User) userService.loadUserByUsername(((User) authentication
.getPrincipal())
.getUsername());
votesService.actionUpVote(id, userSession);
return new ResponseEntity<>(HttpStatus.OK);
}
else if (authentication==null)
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
else
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

}

@PutMapping("/{id}/downvote")
public ResponseEntity<Post> downvoteAction(Authentication authentication, @PathVariable long id) {
Optional<Post> optionalPost = postService.findByID(id);
if(!(authentication == null) && optionalPost.isPresent() ) {
User userSession = (User) userService.loadUserByUsername(((User) authentication
.getPrincipal())
.getUsername());
votesService.actionDownVote(id, userSession);
return new ResponseEntity<>(HttpStatus.OK);
}
else if (authentication==null)
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
else
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

}


}
4 changes: 4 additions & 0 deletions back/src/main/java/net/daw/alist/models/Post.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.daw.alist.models;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import net.bytebuddy.dynamic.DynamicType;

Expand All @@ -21,9 +22,11 @@ public class Post {
private Date date;
private String title;

@JsonIgnore
@ManyToMany
private Set<User> upVotes = new HashSet<>();

@JsonIgnore
@ManyToMany

private Set<User> downVotes = new HashSet<>();
Expand All @@ -41,6 +44,7 @@ public class Post {
@OneToMany (orphanRemoval = true)
private List<PostItem> items = new ArrayList<>();

@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

Expand Down

0 comments on commit 76f7d68

Please sign in to comment.