Skip to content

Commit a71bbfd

Browse files
authored
Merge pull request #1 from manirDev/pagination
Pagination
2 parents 7d43d6f + 76fd618 commit a71bbfd

File tree

10 files changed

+152
-54
lines changed

10 files changed

+152
-54
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.manir.springbootecommercerestapi.controller;
22

33
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
import com.manir.springbootecommercerestapi.response.CategoryResponse;
45
import com.manir.springbootecommercerestapi.service.CategoryService;
6+
import com.manir.springbootecommercerestapi.utils.Constant;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.http.HttpStatus;
79
import org.springframework.http.ResponseEntity;
@@ -26,9 +28,12 @@ public ResponseEntity<CategoryDto> createCategory(@RequestBody CategoryDto categ
2628

2729
//get all categories api
2830
@GetMapping("/getAllCategory")
29-
public List<CategoryDto> getAllCategory(){
30-
List<CategoryDto> categoryDtoList = categoryService.getAllCategory();
31-
return categoryDtoList;
31+
public CategoryResponse getAllCategory(@RequestParam(value = "pageNo", defaultValue = Constant.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
32+
@RequestParam(value = "pageSize", defaultValue = Constant.DEFAULT_PAGE_SIZE, required = false) int pageSize,
33+
@RequestParam(value = "sortBy", defaultValue = Constant.DEFAULT_SORT_BY, required = false) String sortBy,
34+
@RequestParam(value = "sortDir", defaultValue = Constant.DEFAULT_SORT_DIRECTION, required = false) String sortDir){
35+
CategoryResponse categoryResponse = categoryService.getAllCategory(pageNo, pageSize, sortBy, sortDir);
36+
return categoryResponse;
3237
}
3338

3439
//get category by id api

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.manir.springbootecommercerestapi.controller;
22

33
import com.manir.springbootecommercerestapi.dto.ProductDto;
4+
import com.manir.springbootecommercerestapi.response.ProductResponse;
45
import com.manir.springbootecommercerestapi.service.ProductService;
6+
import com.manir.springbootecommercerestapi.utils.Constant;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.http.HttpStatus;
79
import org.springframework.http.MediaType;
810
import org.springframework.http.ResponseEntity;
911
import org.springframework.web.bind.annotation.*;
1012
import org.springframework.web.multipart.MultipartFile;
1113

12-
import java.io.IOException;
1314
import java.util.List;
1415

1516
@RestController
@@ -39,8 +40,11 @@ public ResponseEntity<ProductDto> saveProductByCategoryId(@PathVariable Long cat
3940

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

src/main/java/com/manir/springbootecommercerestapi/repository/CategoryRepository.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99

1010
public interface CategoryRepository extends JpaRepository<Category, Long> {
1111

12-
@Query(
13-
value = "WITH RECURSIVE ancestors(id, parent_id, name, lvl) AS ("
14-
+ " SELECT cat.id, cat.parent_id, cat.name, 1 AS lvl "
15-
+ " FROM categories cat "
16-
+ " WHERE cat.id = :categoryId "
17-
+ " UNION ALL "
18-
+ " SELECT parent.id, parent.parent_id, parent.name, child.lvl + 1 AS lvl "
19-
+ " FROM categories parent "
20-
+ " JOIN ancestors child "
21-
+ " ON parent.id = child.parent_id "
22-
+ " )"
23-
+ "SELECT category_name from ancestors ORDER BY lvl DESC"
24-
, nativeQuery = true)
25-
List<String> findAncestry(@Param("categoryId") Long categoryId);
12+
// @Query(
13+
// value = "WITH RECURSIVE ancestors(id, parent_id, name, lvl) AS ("
14+
// + " SELECT cat.id, cat.parent_id, cat.name, 1 AS lvl "
15+
// + " FROM categories cat "
16+
// + " WHERE cat.id = :categoryId "
17+
// + " UNION ALL "
18+
// + " SELECT parent.id, parent.parent_id, parent.name, child.lvl + 1 AS lvl "
19+
// + " FROM categories parent "
20+
// + " JOIN ancestors child "
21+
// + " ON parent.id = child.parent_id "
22+
// + " )"
23+
// + "SELECT category_name from ancestors ORDER BY lvl DESC"
24+
// , nativeQuery = true)
25+
// List<String> findAncestry(@Param("categoryId") Long categoryId);
2626

2727
// @Query("select distinct c from Category c " +
2828
// "left join fetch c.parent cc " +
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.manir.springbootecommercerestapi.response;
2+
3+
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class CategoryResponse {
14+
15+
private List<CategoryDto> content;
16+
private int pageNo;
17+
private int pageSize;
18+
private long totalElements;
19+
private int totalPages;
20+
private boolean last;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.manir.springbootecommercerestapi.response;
2+
3+
import com.manir.springbootecommercerestapi.dto.ProductDto;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class ProductResponse {
14+
15+
private List<ProductDto> content;
16+
private int pageNo;
17+
private int pageSize;
18+
private long totalElements;
19+
private int totalPages;
20+
private boolean last;
21+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.manir.springbootecommercerestapi.service;
22

33
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
import com.manir.springbootecommercerestapi.response.CategoryResponse;
45

56
import java.util.List;
67

78
public interface CategoryService {
89

910
CategoryDto createCategory(CategoryDto categoryDto);
10-
List<CategoryDto> getAllCategory();
11+
CategoryResponse getAllCategory(int pageNo, int pageSize, String sortBy, String sortDir);
1112
CategoryDto getCategoryById(Long categoryId);
1213
CategoryDto updateCategory(CategoryDto categoryDto, Long categoryId);
1314
void deleteCategory(Long categoryId);

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

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
55
import com.manir.springbootecommercerestapi.repository.CategoryRepository;
66
import com.manir.springbootecommercerestapi.model.Category;
7+
import com.manir.springbootecommercerestapi.response.CategoryResponse;
78
import com.manir.springbootecommercerestapi.service.CategoryService;
89
import org.hibernate.Session;
910
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;
1015
import org.springframework.stereotype.Service;
1116

1217
import javax.annotation.Resource;
@@ -35,15 +40,26 @@ public CategoryDto createCategory(CategoryDto categoryDto) {
3540
}
3641

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

40-
List<Category> categories = categoryRepository.findAll();
48+
List<Category> categoryList = categories.getContent();
4149
//map all categories to dto
42-
List<CategoryDto> categoryDtoList = categories.stream()
50+
List<CategoryDto> categoryDtoList = categoryList.stream()
4351
.map(category -> mapToDto(category))
4452
.collect(Collectors.toList());
4553

46-
return categoryDtoList;
54+
CategoryResponse categoryResponse = new CategoryResponse();
55+
categoryResponse.setContent(categoryDtoList);
56+
categoryResponse.setPageNo(categories.getNumber());
57+
categoryResponse.setPageSize(categories.getSize());
58+
categoryResponse.setTotalPages(categories.getTotalPages());
59+
categoryResponse.setTotalElements(categories.getTotalElements());
60+
categoryResponse.setLast(categories.isLast());
61+
62+
return categoryResponse;
4763
}
4864

4965
@Override
@@ -96,28 +112,28 @@ private Category mapToEntity(CategoryDto categoryDto){
96112
return category;
97113
}
98114

99-
private static void listCategories(Session session) {
100-
Category electronics = session.get(Category.class, 1);
101-
102-
Set<Category> children = electronics.getChildren();
103-
104-
System.out.println(electronics.getTitle());
105-
106-
for (Category child : children) {
107-
System.out.println("--" + child.getTitle());
108-
printChildren(child, 1);
109-
}
110-
}
111-
112-
private static void printChildren(Category parent, int subLevel) {
113-
Set<Category> children = parent.getChildren();
114-
115-
for (Category child : children) {
116-
for (int i = 0; i <= subLevel; i++) System.out.print("--");
117-
118-
System.out.println(child.getTitle());
119-
120-
printChildren(child, subLevel + 1);
121-
}
122-
}
115+
// private static void listCategories(Session session) {
116+
// Category electronics = session.get(Category.class, 1);
117+
//
118+
// Set<Category> children = electronics.getChildren();
119+
//
120+
// System.out.println(electronics.getTitle());
121+
//
122+
// for (Category child : children) {
123+
// System.out.println("--" + child.getTitle());
124+
// printChildren(child, 1);
125+
// }
126+
// }
127+
//
128+
// private static void printChildren(Category parent, int subLevel) {
129+
// Set<Category> children = parent.getChildren();
130+
//
131+
// for (Category child : children) {
132+
// for (int i = 0; i <= subLevel; i++) System.out.print("--");
133+
//
134+
// System.out.println(child.getTitle());
135+
//
136+
// printChildren(child, subLevel + 1);
137+
// }
138+
// }
123139
}

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
import com.manir.springbootecommercerestapi.repository.ProductRepository;
77
import com.manir.springbootecommercerestapi.model.Category;
88
import com.manir.springbootecommercerestapi.model.Product;
9+
import com.manir.springbootecommercerestapi.response.ProductResponse;
910
import com.manir.springbootecommercerestapi.service.ProductService;
1011
import org.modelmapper.ModelMapper;
12+
import org.springframework.data.domain.Page;
13+
import org.springframework.data.domain.PageRequest;
14+
import org.springframework.data.domain.Pageable;
15+
import org.springframework.data.domain.Sort;
1116
import org.springframework.stereotype.Service;
1217
import org.springframework.util.StringUtils;
1318
import org.springframework.web.multipart.MultipartFile;
@@ -40,15 +45,28 @@ public ProductDto createProduct(ProductDto productDto, MultipartFile file) {
4045
}
4146

4247
@Override
43-
public List<ProductDto> getAllProduct() {
48+
public ProductResponse getAllProduct(int pageNo, int pageSize, String sortBy, String sortDir) {
4449

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

51-
return productDtoList;
61+
ProductResponse productResponse = new ProductResponse();
62+
productResponse.setContent(productDtoList);
63+
productResponse.setPageNo(products.getNumber());
64+
productResponse.setPageSize(products.getSize());
65+
productResponse.setTotalPages(products.getTotalPages());
66+
productResponse.setTotalElements(products.getTotalElements());
67+
productResponse.setLast(products.isLast());
68+
69+
return productResponse;
5270
}
5371

5472
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.manir.springbootecommercerestapi.service;
22

33
import com.manir.springbootecommercerestapi.dto.ProductDto;
4+
import com.manir.springbootecommercerestapi.response.ProductResponse;
45
import org.springframework.web.multipart.MultipartFile;
56

67
import java.util.List;
78

89
public interface ProductService {
910
ProductDto createProduct(ProductDto productDto, MultipartFile file);
10-
List<ProductDto> getAllProduct();
11+
ProductResponse getAllProduct(int pageNo, int pageSize, String sortBy, String sortDir);
1112
ProductDto getProductById(Long productId);
1213
ProductDto updateProduct(Long categoryId, ProductDto productDto, Long productId);
1314
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)