Browse Source

custom pagination

David Thornley 6 years ago
parent
commit
cff05d697f

+ 23
- 3
src/main/java/com/ziplinegreen/vault/Controller/PostController.java View File

3
 import com.ziplinegreen.vault.Model.Post;
3
 import com.ziplinegreen.vault.Model.Post;
4
 import com.ziplinegreen.vault.Service.PostService;
4
 import com.ziplinegreen.vault.Service.PostService;
5
 import org.springframework.beans.factory.annotation.Autowired;
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
 import org.springframework.http.ResponseEntity;
10
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.messaging.handler.annotation.MessageMapping;
11
 import org.springframework.messaging.handler.annotation.MessageMapping;
8
 import org.springframework.messaging.handler.annotation.SendTo;
12
 import org.springframework.messaging.handler.annotation.SendTo;
9
 import org.springframework.web.bind.annotation.*;
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
 @CrossOrigin
20
 @CrossOrigin
14
 @RestController
21
 @RestController
33
     }
40
     }
34
 
41
 
35
 
42
 
43
+//    @GetMapping("/posts/all")
44
+//    public Iterable<Post> getAllPosts() {
45
+//        return postService.getAllPosts();
46
+//    }
47
+
36
     @GetMapping("/posts/all")
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
     @GetMapping("/posts/id/{userId}")
61
     @GetMapping("/posts/id/{userId}")

+ 3
- 0
src/main/java/com/ziplinegreen/vault/Repository/PostRepository.java View File

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

+ 9
- 1
src/main/java/com/ziplinegreen/vault/Service/PostService.java View File

4
 import com.ziplinegreen.vault.Model.Post;
4
 import com.ziplinegreen.vault.Model.Post;
5
 import com.ziplinegreen.vault.Repository.PostRepository;
5
 import com.ziplinegreen.vault.Repository.PostRepository;
6
 import org.springframework.beans.factory.annotation.Autowired;
6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.data.domain.Page;
8
+import org.springframework.data.domain.Pageable;
7
 import org.springframework.http.HttpStatus;
9
 import org.springframework.http.HttpStatus;
8
 import org.springframework.http.ResponseEntity;
10
 import org.springframework.http.ResponseEntity;
9
 import org.springframework.stereotype.Service;
11
 import org.springframework.stereotype.Service;
12
+import org.springframework.transaction.annotation.Transactional;
10
 
13
 
11
 import java.util.Collection;
14
 import java.util.Collection;
12
 
15
 
13
 @Service
16
 @Service
17
+@Transactional
14
 public class PostService {
18
 public class PostService {
15
 
19
 
16
-    @Autowired
20
+
17
     private PostRepository postRepository;
21
     private PostRepository postRepository;
18
 
22
 
19
     @Autowired
23
     @Autowired
52
         return postRepository.save(post);
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