|
@@ -0,0 +1,103 @@
|
|
|
+package es.uv.garcosda.controllers;
|
|
|
+
|
|
|
+import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
|
|
+import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import es.uv.garcosda.domain.Comment;
|
|
|
+import es.uv.garcosda.services.CommentService;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/v1/comments")
|
|
|
+public class CommentController {
|
|
|
+
|
|
|
+ private final static Logger LOGGER = LoggerFactory.getLogger(CommentController.class);
|
|
|
+
|
|
|
+ @Autowired private CommentService cs;
|
|
|
+
|
|
|
+ @GetMapping("")
|
|
|
+ public ResponseEntity<List<Comment>> findComments(@RequestParam(value="videoId", required = false) Optional<String> videoId,
|
|
|
+ @RequestParam(value="userId", required = false) Optional<String> userId) {
|
|
|
+
|
|
|
+ List<Comment> comments = new ArrayList<Comment>();
|
|
|
+
|
|
|
+ if(!videoId.isPresent() && !userId.isPresent()) {
|
|
|
+ LOGGER.debug("Get all comments");
|
|
|
+ comments = cs.findAll();
|
|
|
+ }
|
|
|
+ else if(videoId.isPresent()) {
|
|
|
+ LOGGER.debug("Get comments by video id");
|
|
|
+ comments = cs.findByVideoId(videoId.get());
|
|
|
+ }
|
|
|
+ else if(userId.isPresent()) {
|
|
|
+ LOGGER.debug("Get comments by user id");
|
|
|
+ comments = cs.findByUserId(userId.get());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Comment c : comments) {
|
|
|
+ c.add(linkTo(methodOn(CommentController.class).findCommentById(c.getId())).withSelfRel());
|
|
|
+ c.add(linkTo(methodOn(VideoController.class).findVideoById(c.getVideoId())).withRel("video"));
|
|
|
+ c.add(linkTo(methodOn(UserController.class).findUserById(c.getUserId())).withRel("user"));
|
|
|
+ }
|
|
|
+
|
|
|
+ return new ResponseEntity<List<Comment>>(comments, HttpStatus.OK);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("{id}")
|
|
|
+ public ResponseEntity<Comment> findCommentById(@PathVariable("id") String id) {
|
|
|
+ LOGGER.debug("Get comment id: "+id);
|
|
|
+ Optional<Comment> comment = cs.findById(id);
|
|
|
+ if(!comment.isEmpty()) {
|
|
|
+ Comment c = comment.get();
|
|
|
+ c.add(linkTo(methodOn(UserController.class).findUserById(id)).withSelfRel());
|
|
|
+ c.add(linkTo(methodOn(VideoController.class).findVideoById(c.getVideoId())).withRel("video"));
|
|
|
+ c.add(linkTo(methodOn(UserController.class).findUserById(c.getUserId())).withRel("user"));
|
|
|
+ return new ResponseEntity<Comment>(c, HttpStatus.OK);
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return new ResponseEntity<Comment>(new Comment(), HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("")
|
|
|
+ public ResponseEntity<Comment> createComment(@RequestBody Comment user) {
|
|
|
+ LOGGER.debug("Create comment");
|
|
|
+ Comment createdComment = cs.create(user);
|
|
|
+
|
|
|
+ if(createdComment.getId() != null) {
|
|
|
+ createdComment.add(linkTo(methodOn(UserController.class).findUserById(createdComment.getId())).withSelfRel());
|
|
|
+ createdComment.add(linkTo(methodOn(VideoController.class).findVideoById(createdComment.getVideoId())).withRel("video"));
|
|
|
+ createdComment.add(linkTo(methodOn(UserController.class).findUserById(createdComment.getUserId())).withRel("user"));
|
|
|
+ return new ResponseEntity<Comment>(createdComment, HttpStatus.OK);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return new ResponseEntity<Comment>(new Comment(), HttpStatus.BAD_REQUEST);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("{id}")
|
|
|
+ public ResponseEntity<String> deleteCommentById(@PathVariable("id") String id) {
|
|
|
+ LOGGER.debug("Delete comment id: "+id);
|
|
|
+ cs.deleteById(id);
|
|
|
+ return new ResponseEntity<String>("", HttpStatus.OK);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|