Skip to content

Commit

Permalink
feat: votes and follows rest docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr4ll committed Mar 24, 2023
1 parent 3f8c954 commit a811ea2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public ResponseEntity<Post> deletePost(Authentication auth, @PathVariable long p
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@PutMapping("/{postId}/upvote")
@Operation(summary = "Current user upvote a post")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found and upvote the post", content = @Content),
@ApiResponse(responseCode = "403", description = "Current user not logged in", content = @Content),
@ApiResponse(responseCode = "404", description = "Post not found", content = @Content)
})
@PutMapping("/{postId}/upvotes")
public ResponseEntity<Post> upvoteAction(Authentication authentication, @PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if(!(authentication == null) && optionalPost.isPresent() ) {
Expand All @@ -108,7 +114,13 @@ else if (authentication==null)

}

@PutMapping("/{postId}/downvote")
@Operation(summary = "Current user downvote a post")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found and downvoted the post", content = @Content),
@ApiResponse(responseCode = "403", description = "Current user not logged in", content = @Content),
@ApiResponse(responseCode = "404", description = "Post not found", content = @Content)
})
@PutMapping("/{postId}/downvotes")
public ResponseEntity<Post> downvoteAction(Authentication authentication, @PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if(!(authentication == null) && optionalPost.isPresent() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,28 @@ public ResponseEntity<User> banUser(Authentication authentication, @PathVariable
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@Operation(summary = "Current user follows another user by its name")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found and followed/unfollowed state changed", content = @Content),
@ApiResponse(responseCode = "403", description = "Current user not logged in", content = @Content),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content)
})
@PutMapping("/{username}/follows")
public ResponseEntity<User> follow(Authentication authentication, @PathVariable String username){
Optional<User> optionalUser = (Optional<User>) userService.loadUserByUsername(username);
User userProfile = (User) userService.loadUserByUsername(username);
Optional<User> optionalUser = userService.findByUsername(username);
if(authentication == null)
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
User userSession = (User) authentication;
if (userProfile.getFollowers().contains(userSession))
userSession.unFollow(userProfile);
else
userSession.follow(userProfile);
userSession.follow(userProfile);
userService.saveUser(userSession);
return new ResponseEntity<>(HttpStatus.OK);
if( optionalUser.isPresent()) {
User userProfile = optionalUser.get();
if (userProfile.getFollowers().contains(userSession))
userSession.unFollow(userProfile);
else
userSession.follow(userProfile);
userService.saveUser(userSession);
return new ResponseEntity<>(HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

}
1 change: 1 addition & 0 deletions back/src/main/java/net/daw/alist/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundEx
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
}


public Optional<User> findByID(Long id){
return userRepository.findById(id);
}
Expand Down

0 comments on commit a811ea2

Please sign in to comment.