Skip to content

Commit

Permalink
feat: documented methods on CommentRestController
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente1215 committed Mar 22, 2023
1 parent ccfa433 commit 09a32de
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package net.daw.alist.controllers.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import net.daw.alist.models.Comment;
import net.daw.alist.models.Post;
import net.daw.alist.models.User;
Expand All @@ -8,68 +14,98 @@
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.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/comments")
public class CommentRestController {
@Autowired
private PostService postService;
@Autowired
private PostService postService;

@Autowired
private UserService userService;
@Autowired
private UserService userService;

@PostMapping("/{postId}")
@ResponseStatus(HttpStatus.CREATED)
public Comment createComment(Comment comment, @PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if(optionalPost.isPresent()) {
Post post = optionalPost.get();
post.addComment(comment);
postService.save(post);
return comment;
}
return null;

@PostMapping("/{postId}")
@ResponseStatus(HttpStatus.CREATED)
public Comment createComment(@RequestBody String content, @PathVariable long postId, Authentication auth) throws SQLException, IOException {
User user = (User) auth.getPrincipal();
Comment comment = new Comment(user, content, null);
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
post.addComment(comment);
postService.save(post);
return comment;
}
return null;
}

@GetMapping("/post/{postId}")
public ResponseEntity<List<Comment>> getPostComments(@PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
List<Comment> comments = post.getComments();
return new ResponseEntity<>(comments, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
@Operation(summary = "Get all the comments of a post")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Post found", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Comment.class)))}),
@ApiResponse(responseCode = "404", description = "Post not found", content = @Content)
})
@GetMapping("/post/{postId}")
public ResponseEntity<List<Comment>> getPostComments(@PathVariable long postId) {
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
List<Comment> comments = post.getComments();
return new ResponseEntity<>(comments, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@GetMapping("/user/{userId}")
public ResponseEntity<List<Comment>> getUserComments(@PathVariable long userId) {
Optional<User> optionalUser = userService.findByID(userId);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
List<Comment> comments = user.getComments();
return new ResponseEntity<>(comments, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
@Operation(summary = "Get all the comments of an user")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Comment.class)))}),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content)
})
@GetMapping("/user/{userId}")
public ResponseEntity<List<Comment>> getUserComments(@PathVariable long userId) {
Optional<User> optionalUser = userService.findByID(userId);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
List<Comment> comments = user.getComments();
return new ResponseEntity<>(comments, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@DeleteMapping("/{postId}/{commentId}")
public ResponseEntity<Post> deletePost(@PathVariable long postId, @PathVariable long commentId) {
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
Optional<Comment> optionalComment = post.getCommentByID(commentId);
if(optionalComment.isPresent()){
Comment comment = optionalComment.get();
post.getComments().remove(comment);
postService.save(post);
return new ResponseEntity<>(HttpStatus.OK);
}
@Operation(summary = "Delete a comment of a post")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Comment deleted", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Comment.class)))}),
@ApiResponse(responseCode = "403", description = "Can´t delete other one comment", content = @Content),
@ApiResponse(responseCode = "404", description = "Post or comment not found", content = @Content)
})
@DeleteMapping("/{postId}/{commentId}")
public ResponseEntity<Post> deletePost(@PathVariable long postId, @PathVariable long commentId, Authentication auth) {
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
User user = (User) auth.getPrincipal();
Optional<Comment> optionalComment = postService.getCommentByID(post, commentId);
if (optionalComment.isPresent()) {
Comment comment = optionalComment.get();
if (user.getId().equals(comment.getAuthor().getId()) || user.isAdmin()) {
post.getComments().remove(comment);
postService.save(post);
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
11 changes: 0 additions & 11 deletions back/src/main/java/net/daw/alist/models/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,4 @@ public void addComment(Comment comment){
public void addDownVote(User user){ this.downVotes.add(user); }

public void removeDownVote(User user) { this.downVotes.remove(user); }


public Optional<Comment> getCommentByID(long commentId) {
for (Comment comment:comments) {
if(comment.getId() == commentId){
Optional<Comment> optionalComment = Optional.of(comment);
return optionalComment;
}
}
return null;
}
}
10 changes: 10 additions & 0 deletions back/src/main/java/net/daw/alist/services/PostService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.daw.alist.services;

import lombok.AllArgsConstructor;
import net.daw.alist.models.Comment;
import net.daw.alist.models.Post;
import net.daw.alist.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -53,4 +54,13 @@ public long count() {
return postRepository.count();
}

public Optional<Comment> getCommentByID(Post post, long commentId) {
List<Comment> comments = post.getComments();
for (Comment comment : comments) {
if (comment.getId() == commentId) {
return Optional.of(comment);
}
}
return Optional.empty();
}
}

0 comments on commit 09a32de

Please sign in to comment.