Explorar el Código

custom pagination

David Thornley hace 6 años
padre
commit
cff05d697f

+ 23
- 3
src/main/java/com/ziplinegreen/vault/Controller/PostController.java Ver fichero

@@ -3,12 +3,19 @@ package com.ziplinegreen.vault.Controller;
3 3
 import com.ziplinegreen.vault.Model.Post;
4 4
 import com.ziplinegreen.vault.Service.PostService;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.data.domain.Page;
7
+import org.springframework.data.domain.PageImpl;
8
+import org.springframework.data.domain.PageRequest;
9
+import org.springframework.data.domain.Pageable;
6 10
 import org.springframework.http.ResponseEntity;
7 11
 import org.springframework.messaging.handler.annotation.MessageMapping;
8 12
 import org.springframework.messaging.handler.annotation.SendTo;
9 13
 import org.springframework.web.bind.annotation.*;
10 14
 
11
-import java.util.Collection;
15
+
16
+import java.util.*;
17
+import java.util.stream.Collectors;
18
+import java.util.stream.StreamSupport;
12 19
 
13 20
 @CrossOrigin
14 21
 @RestController
@@ -33,9 +40,22 @@ public class PostController {
33 40
     }
34 41
 
35 42
 
43
+//    @GetMapping("/posts/all")
44
+//    public Iterable<Post> getAllPosts() {
45
+//        return postService.getAllPosts();
46
+//    }
47
+
36 48
     @GetMapping("/posts/all")
37
-    public Iterable<Post> getAllPosts() {
38
-        return postService.getAllPosts();
49
+    Page<Post> pageAllPosts(Pageable pageable) {
50
+       Iterable<Post> posts = postService.getAllPosts();
51
+       List<Post> postList = StreamSupport.stream(posts.spliterator(), false)
52
+               .collect(Collectors.toList());
53
+       Collections.reverse(postList);
54
+
55
+        int start = (int)pageable.getOffset();
56
+        int end = (start + pageable.getPageSize()) > postList.size() ? postList.size() : (start + pageable.getPageSize());
57
+
58
+        return new PageImpl<Post>(postList.subList(start,end),pageable, postList.size());
39 59
     }
40 60
 
41 61
     @GetMapping("/posts/id/{userId}")

+ 3
- 0
src/main/java/com/ziplinegreen/vault/Repository/PostRepository.java Ver fichero

@@ -1,6 +1,8 @@
1 1
 package com.ziplinegreen.vault.Repository;
2 2
 
3 3
 import com.ziplinegreen.vault.Model.Post;
4
+import org.springframework.data.domain.Page;
5
+import org.springframework.data.domain.Pageable;
4 6
 import org.springframework.data.jpa.repository.JpaRepository;
5 7
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
6 8
 import java.util.Collection;
@@ -8,4 +10,5 @@ import java.util.Collection;
8 10
 @RepositoryRestResource
9 11
 public interface PostRepository extends JpaRepository<Post,Long> {
10 12
     Collection<Post> findByUserId(Long userId);
13
+
11 14
 }

+ 9
- 1
src/main/java/com/ziplinegreen/vault/Service/PostService.java Ver fichero

@@ -4,16 +4,20 @@ package com.ziplinegreen.vault.Service;
4 4
 import com.ziplinegreen.vault.Model.Post;
5 5
 import com.ziplinegreen.vault.Repository.PostRepository;
6 6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.data.domain.Page;
8
+import org.springframework.data.domain.Pageable;
7 9
 import org.springframework.http.HttpStatus;
8 10
 import org.springframework.http.ResponseEntity;
9 11
 import org.springframework.stereotype.Service;
12
+import org.springframework.transaction.annotation.Transactional;
10 13
 
11 14
 import java.util.Collection;
12 15
 
13 16
 @Service
17
+@Transactional
14 18
 public class PostService {
15 19
 
16
-    @Autowired
20
+
17 21
     private PostRepository postRepository;
18 22
 
19 23
     @Autowired
@@ -52,5 +56,9 @@ public class PostService {
52 56
         return postRepository.save(post);
53 57
     }
54 58
 
59
+    public Page<Post> listAllByPage(Pageable pageable) {
60
+
61
+       return postRepository.findAll(pageable);
62
+    }
55 63
 }
56 64