From 0121fd4bcbd17116e59a602ddb057404d7e11ec6 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 17 Jun 2020 20:22:26 +0700 Subject: [PATCH] Just updated for demo --- .../com/cubetiqs/demo/rest/PostController.java | 16 +++++++++++----- .../com/cubetiqs/demo/service/BaseService.java | 4 ++++ .../cubetiqs/demo/service/BaseServiceImpl.java | 7 +++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/cubetiqs/demo/rest/PostController.java b/src/main/java/com/cubetiqs/demo/rest/PostController.java index 6c38946..bf4618c 100644 --- a/src/main/java/com/cubetiqs/demo/rest/PostController.java +++ b/src/main/java/com/cubetiqs/demo/rest/PostController.java @@ -1,15 +1,14 @@ package com.cubetiqs.demo.rest; -import com.cubetiqs.demo.domain.PostEntity; import com.cubetiqs.demo.service.PostService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.List; - @RestController @RequestMapping(path = {"/posts"}) public class PostController { @@ -21,7 +20,14 @@ public class PostController { } @GetMapping - public List getAllPosts(Pageable pageable) { - return postService.findAll(); + public ResponseEntity getAllPosts( + Pageable pageable, + @RequestParam(value = "view", defaultValue = "list") String viewType, + @RequestParam(value = "q", defaultValue = "") String q + ) { + if ("list".equalsIgnoreCase(viewType)) { + return ResponseEntity.ok(postService.findAll()); + } + return ResponseEntity.ok(postService.findAll(pageable)); } } diff --git a/src/main/java/com/cubetiqs/demo/service/BaseService.java b/src/main/java/com/cubetiqs/demo/service/BaseService.java index 946b31d..4925b52 100644 --- a/src/main/java/com/cubetiqs/demo/service/BaseService.java +++ b/src/main/java/com/cubetiqs/demo/service/BaseService.java @@ -1,6 +1,8 @@ package com.cubetiqs.demo.service; import com.cubetiqs.demo.repository.BaseRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.NoRepositoryBean; import java.io.Serializable; @@ -9,5 +11,7 @@ import java.util.List; @NoRepositoryBean public interface BaseService { BaseRepository getRepository(); + Page findAll(Pageable pageable, String q); + Page findAll(Pageable pageable); List findAll(); } diff --git a/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java index f980f90..a7f9ea3 100644 --- a/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java +++ b/src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java @@ -1,5 +1,7 @@ package com.cubetiqs.demo.service; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.NoRepositoryBean; import java.io.Serializable; @@ -11,4 +13,9 @@ public abstract class BaseServiceImpl implements Bas public List findAll() { return getRepository().findAll(); } + + @Override + public Page findAll(Pageable pageable) { + return getRepository().findAll(pageable); + } }