Skip to content

Commit d86366d

Browse files
committed
Category and Product pagination started...
1 parent 7d43d6f commit d86366d

File tree

7 files changed

+47
-13
lines changed

7 files changed

+47
-13
lines changed

src/main/java/com/manir/springbootecommercerestapi/controller/CategoryController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.manir.springbootecommercerestapi.dto.CategoryDto;
44
import com.manir.springbootecommercerestapi.service.CategoryService;
5+
import com.manir.springbootecommercerestapi.utils.Constant;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.HttpStatus;
78
import org.springframework.http.ResponseEntity;
@@ -26,8 +27,11 @@ public ResponseEntity<CategoryDto> createCategory(@RequestBody CategoryDto categ
2627

2728
//get all categories api
2829
@GetMapping("/getAllCategory")
29-
public List<CategoryDto> getAllCategory(){
30-
List<CategoryDto> categoryDtoList = categoryService.getAllCategory();
30+
public List<CategoryDto> getAllCategory(@RequestParam(value = "pageNo", defaultValue = Constant.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
31+
@RequestParam(value = "pageSize", defaultValue = Constant.DEFAULT_PAGE_SIZE, required = false) int pageSize,
32+
@RequestParam(value = "sortBy", defaultValue = Constant.DEFAULT_SORT_BY, required = false) String sortBy,
33+
@RequestParam(value = "sortDir", defaultValue = Constant.DEFAULT_SORT_DIRECTION, required = false) String sortDir){
34+
List<CategoryDto> categoryDtoList = categoryService.getAllCategory(pageNo, pageSize, sortBy, sortDir);
3135
return categoryDtoList;
3236
}
3337

src/main/java/com/manir/springbootecommercerestapi/controller/ProductController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import com.manir.springbootecommercerestapi.dto.ProductDto;
44
import com.manir.springbootecommercerestapi.service.ProductService;
5+
import com.manir.springbootecommercerestapi.utils.Constant;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.HttpStatus;
78
import org.springframework.http.MediaType;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.annotation.*;
1011
import org.springframework.web.multipart.MultipartFile;
1112

12-
import java.io.IOException;
1313
import java.util.List;
1414

1515
@RestController
@@ -39,8 +39,11 @@ public ResponseEntity<ProductDto> saveProductByCategoryId(@PathVariable Long cat
3939

4040
//get all products api
4141
@GetMapping("/getAllProduct")
42-
public List<ProductDto> getAllProduct(){
43-
List<ProductDto> responseProductDtoList = productService.getAllProduct();
42+
public List<ProductDto> getAllProduct(@RequestParam(value = "pageNo", defaultValue = Constant.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
43+
@RequestParam(value = "pageSize", defaultValue = Constant.DEFAULT_PAGE_SIZE, required = false) int pageSize,
44+
@RequestParam(value = "sortBy", defaultValue = Constant.DEFAULT_SORT_BY, required = false) String sortBy,
45+
@RequestParam(value = "sortDir", defaultValue = Constant.DEFAULT_SORT_DIRECTION, required = false) String sortDir){
46+
List<ProductDto> responseProductDtoList = productService.getAllProduct(pageNo, pageSize, sortBy, sortDir);
4447
return responseProductDtoList;
4548
}
4649

src/main/java/com/manir/springbootecommercerestapi/service/CategoryService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public interface CategoryService {
88

99
CategoryDto createCategory(CategoryDto categoryDto);
10-
List<CategoryDto> getAllCategory();
10+
List<CategoryDto> getAllCategory(int pageNo, int pageSize, String sortBy, String sortDir);
1111
CategoryDto getCategoryById(Long categoryId);
1212
CategoryDto updateCategory(CategoryDto categoryDto, Long categoryId);
1313
void deleteCategory(Long categoryId);

src/main/java/com/manir/springbootecommercerestapi/service/Impl/CategoryServiceImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import com.manir.springbootecommercerestapi.service.CategoryService;
88
import org.hibernate.Session;
99
import org.modelmapper.ModelMapper;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.PageRequest;
12+
import org.springframework.data.domain.Pageable;
13+
import org.springframework.data.domain.Sort;
1014
import org.springframework.stereotype.Service;
1115

1216
import javax.annotation.Resource;
@@ -35,11 +39,14 @@ public CategoryDto createCategory(CategoryDto categoryDto) {
3539
}
3640

3741
@Override
38-
public List<CategoryDto> getAllCategory() {
42+
public List<CategoryDto> getAllCategory(int pageNo, int pageSize, String sortBy, String sortDir) {
43+
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending();
44+
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
45+
Page<Category> categories = categoryRepository.findAll(pageable);
3946

40-
List<Category> categories = categoryRepository.findAll();
47+
List<Category> categoryList = categories.getContent();
4148
//map all categories to dto
42-
List<CategoryDto> categoryDtoList = categories.stream()
49+
List<CategoryDto> categoryDtoList = categoryList.stream()
4350
.map(category -> mapToDto(category))
4451
.collect(Collectors.toList());
4552

src/main/java/com/manir/springbootecommercerestapi/service/Impl/ProductServiceImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import com.manir.springbootecommercerestapi.model.Product;
99
import com.manir.springbootecommercerestapi.service.ProductService;
1010
import org.modelmapper.ModelMapper;
11+
import org.springframework.data.domain.Page;
12+
import org.springframework.data.domain.PageRequest;
13+
import org.springframework.data.domain.Pageable;
14+
import org.springframework.data.domain.Sort;
1115
import org.springframework.stereotype.Service;
1216
import org.springframework.util.StringUtils;
1317
import org.springframework.web.multipart.MultipartFile;
@@ -40,11 +44,16 @@ public ProductDto createProduct(ProductDto productDto, MultipartFile file) {
4044
}
4145

4246
@Override
43-
public List<ProductDto> getAllProduct() {
47+
public List<ProductDto> getAllProduct(int pageNo, int pageSize, String sortBy, String sortDir) {
4448

45-
List<Product> products = productRepository.findAll();
49+
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending();
50+
51+
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
52+
Page<Product> products = productRepository.findAll(pageable);
53+
54+
List<Product> productList = products.getContent();
4655
//map to dtos
47-
List<ProductDto> productDtoList = products.stream()
56+
List<ProductDto> productDtoList = productList.stream()
4857
.map(product -> mapToDto(product))
4958
.collect(Collectors.toList());
5059

src/main/java/com/manir/springbootecommercerestapi/service/ProductService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public interface ProductService {
99
ProductDto createProduct(ProductDto productDto, MultipartFile file);
10-
List<ProductDto> getAllProduct();
10+
List<ProductDto> getAllProduct(int pageNo, int pageSize, String sortBy, String sortDir);
1111
ProductDto getProductById(Long productId);
1212
ProductDto updateProduct(Long categoryId, ProductDto productDto, Long productId);
1313
void deleteProduct(Long productId);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.manir.springbootecommercerestapi.utils;
2+
3+
public class Constant {
4+
5+
//pagination properties
6+
public static final String DEFAULT_PAGE_NUMBER = "0";
7+
public static final String DEFAULT_PAGE_SIZE = "10";
8+
public static final String DEFAULT_SORT_BY = "id";
9+
public static final String DEFAULT_SORT_DIRECTION = "asc";
10+
11+
}

0 commit comments

Comments
 (0)