Skip to content

Commit

Permalink
feat: CommentRestController implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente1215 committed Mar 16, 2023
1 parent 31ff5a8 commit 9298bab
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
package net.daw.alist.controllers.rest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.daw.alist.models.Comment;
import net.daw.alist.models.Post;
import net.daw.alist.services.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

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

@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;
}

@GetMapping("/{postId}/{commentId}")
public ResponseEntity<Comment> getComment(@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();
return new ResponseEntity<>(comment, 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);
}
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
1 change: 1 addition & 0 deletions back/src/main/java/net/daw/alist/models/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ public String getImagePath() {
return imagePath;
}

public Long getId() {return id;}
}
16 changes: 11 additions & 5 deletions back/src/main/java/net/daw/alist/models/Post.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package net.daw.alist.models;

import java.util.List;
import net.bytebuddy.dynamic.DynamicType;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

import javax.persistence.*;

Expand Down Expand Up @@ -140,4 +137,13 @@ public void addComment(Comment comment){
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;
}
}

0 comments on commit 9298bab

Please sign in to comment.