Browse Source

Hateoas pagination links

dagarcos 2 years ago
parent
commit
f83482c440

+ 54 - 10
src/main/java/es/uv/garcosda/controllers/CommentController.java

@@ -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}")

+ 4 - 0
src/main/java/es/uv/garcosda/repositories/CommentRepository.java

@@ -2,6 +2,8 @@ package es.uv.garcosda.repositories;
 
 import java.util.List;
 
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 import es.uv.garcosda.domain.Comment;
@@ -9,5 +11,7 @@ import es.uv.garcosda.domain.Comment;
 public interface CommentRepository extends JpaRepository<Comment, String> {
 
 	public List<Comment> findByVideoId(String videoId);
+	public Page<Comment> findByVideoId(String videoId, Pageable pageable);
 	public List<Comment> findByUserId(String UserId);
+	public Page<Comment> findByUserId(String UserId, Pageable pageable);
 }

+ 14 - 0
src/main/java/es/uv/garcosda/services/CommentService.java

@@ -4,6 +4,8 @@ import java.util.List;
 import java.util.Optional;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -19,6 +21,10 @@ public class CommentService {
 	public List<Comment> findAll(){
 		return cr.findAll();
 	}
+	
+	public Page<Comment> findAll(Pageable pageable){
+		return cr.findAll(pageable);
+	}
 
 	public Optional<Comment> findById(String id) {
 		return cr.findById(id);
@@ -28,10 +34,18 @@ public class CommentService {
 		return cr.findByVideoId(firstname);
 	}
 	
+	public Page<Comment> findByVideoId(String firstname, Pageable pageable){
+		return cr.findByVideoId(firstname, pageable);
+	}
+	
 	public List<Comment> findByUserId(String lastname){
 		return cr.findByUserId(lastname);
 	}
 	
+	public Page<Comment> findByUserId(String lastname, Pageable pageable){
+		return cr.findByUserId(lastname, pageable);
+	}
+	
 	public Comment update(Comment comment) {
 		return cr.save(comment);
 	}