-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CommentRestController implemented
- Loading branch information
1 parent
31ff5a8
commit 9298bab
Showing
3 changed files
with
66 additions
and
7 deletions.
There are no files selected for viewing
56 changes: 54 additions & 2 deletions
56
back/src/main/java/net/daw/alist/controllers/rest/CommentRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,4 +78,5 @@ public String getImagePath() { | |
return imagePath; | ||
} | ||
|
||
public Long getId() {return id;} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters