|
@@ -10,6 +10,12 @@ import java.util.Optional;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.web.PagedResourcesAssembler;
|
|
|
+import org.springframework.hateoas.EntityModel;
|
|
|
+import org.springframework.hateoas.PagedModel;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
@@ -31,33 +37,71 @@ public class CommentController {
|
|
|
private final static Logger LOGGER = LoggerFactory.getLogger(CommentController.class);
|
|
|
|
|
|
@Autowired private CommentService cs;
|
|
|
+ @Autowired private PagedResourcesAssembler<Comment> pagedResourcesAssembler;
|
|
|
|
|
|
@GetMapping("")
|
|
|
- public ResponseEntity<List<Comment>> findComments(@RequestParam(value="videoId", required = false) Optional<String> videoId,
|
|
|
- @RequestParam(value="userId", required = false) Optional<String> userId) {
|
|
|
+ public ResponseEntity<?> findComments(@RequestParam(value="videoId", required = false) Optional<String> videoId,
|
|
|
+ @RequestParam(value="userId", required = false) Optional<String> userId,
|
|
|
+ @RequestParam(value="page", defaultValue = "0", required = false) int page,
|
|
|
+ @RequestParam(value="size", defaultValue = "1", required = false) int size) {
|
|
|
|
|
|
List<Comment> comments = new ArrayList<Comment>();
|
|
|
+ Page<Comment> page_comments = Page.empty();
|
|
|
+ Pageable pageable;
|
|
|
+
|
|
|
+ if(size == 0) {
|
|
|
+ pageable = PageRequest.of(0, 1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ pageable = PageRequest.of(page, size);
|
|
|
+ }
|
|
|
|
|
|
if(!videoId.isPresent() && !userId.isPresent()) {
|
|
|
LOGGER.debug("Get all comments");
|
|
|
- comments = cs.findAll();
|
|
|
+ if(size == 0) {
|
|
|
+ comments = cs.findAll();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ page_comments = cs.findAll(pageable);
|
|
|
+ }
|
|
|
}
|
|
|
else if(videoId.isPresent()) {
|
|
|
LOGGER.debug("Get comments by video id");
|
|
|
- comments = cs.findByVideoId(videoId.get());
|
|
|
+ if(size == 0) {
|
|
|
+ comments = cs.findByVideoId(videoId.get());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ page_comments = cs.findByVideoId(videoId.get(), pageable);
|
|
|
+ }
|
|
|
}
|
|
|
else if(userId.isPresent()) {
|
|
|
LOGGER.debug("Get comments by user id");
|
|
|
- comments = cs.findByUserId(userId.get());
|
|
|
+ if(size == 0) {
|
|
|
+ comments = cs.findByUserId(userId.get());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ page_comments = cs.findByUserId(userId.get(), pageable);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- 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"));
|
|
|
+ if(size == 0) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ for (Comment c : page_comments.getContent()) {
|
|
|
+ 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<PagedModel<EntityModel<Comment>>>(pagedResourcesAssembler.toModel(page_comments), HttpStatus.OK);
|
|
|
}
|
|
|
|
|
|
- return new ResponseEntity<List<Comment>>(comments, HttpStatus.OK);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@GetMapping("{id}")
|