Skip to content

Commit 76fd618

Browse files
committed
Category and Product pagination finished...
1 parent d86366d commit 76fd618

File tree

9 files changed

+119
-55
lines changed

9 files changed

+119
-55
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
56
import com.manir.springbootecommercerestapi.utils.Constant;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,12 +28,12 @@ public ResponseEntity<CategoryDto> createCategory(@RequestBody CategoryDto categ
2728

2829
//get all categories api
2930
@GetMapping("/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);
35-
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;
3637
}
3738

3839
//get category by id api

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
56
import com.manir.springbootecommercerestapi.utils.Constant;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,11 +40,11 @@ public ResponseEntity<ProductDto> saveProductByCategoryId(@PathVariable Long cat
3940

4041
//get all products api
4142
@GetMapping("/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);
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);
4748
return responseProductDtoList;
4849
}
4950

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(int pageNo, int pageSize, String sortBy, String sortDir);
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: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
@@ -39,7 +40,7 @@ public CategoryDto createCategory(CategoryDto categoryDto) {
3940
}
4041

4142
@Override
42-
public List<CategoryDto> getAllCategory(int pageNo, int pageSize, String sortBy, String sortDir) {
43+
public CategoryResponse getAllCategory(int pageNo, int pageSize, String sortBy, String sortDir) {
4344
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending();
4445
Pageable pageable = PageRequest.of(pageNo, pageSize, sort);
4546
Page<Category> categories = categoryRepository.findAll(pageable);
@@ -50,7 +51,15 @@ public List<CategoryDto> getAllCategory(int pageNo, int pageSize, String sortBy,
5051
.map(category -> mapToDto(category))
5152
.collect(Collectors.toList());
5253

53-
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;
5463
}
5564

5665
@Override
@@ -103,28 +112,28 @@ private Category mapToEntity(CategoryDto categoryDto){
103112
return category;
104113
}
105114

106-
private static void listCategories(Session session) {
107-
Category electronics = session.get(Category.class, 1);
108-
109-
Set<Category> children = electronics.getChildren();
110-
111-
System.out.println(electronics.getTitle());
112-
113-
for (Category child : children) {
114-
System.out.println("--" + child.getTitle());
115-
printChildren(child, 1);
116-
}
117-
}
118-
119-
private static void printChildren(Category parent, int subLevel) {
120-
Set<Category> children = parent.getChildren();
121-
122-
for (Category child : children) {
123-
for (int i = 0; i <= subLevel; i++) System.out.print("--");
124-
125-
System.out.println(child.getTitle());
126-
127-
printChildren(child, subLevel + 1);
128-
}
129-
}
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+
// }
130139
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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;
1112
import org.springframework.data.domain.Page;
@@ -44,7 +45,7 @@ public ProductDto createProduct(ProductDto productDto, MultipartFile file) {
4445
}
4546

4647
@Override
47-
public List<ProductDto> getAllProduct(int pageNo, int pageSize, String sortBy, String sortDir) {
48+
public ProductResponse getAllProduct(int pageNo, int pageSize, String sortBy, String sortDir) {
4849

4950
Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending();
5051

@@ -57,7 +58,15 @@ public List<ProductDto> getAllProduct(int pageNo, int pageSize, String sortBy, S
5758
.map(product -> mapToDto(product))
5859
.collect(Collectors.toList());
5960

60-
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;
6170
}
6271

6372
@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(int pageNo, int pageSize, String sortBy, String sortDir);
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);

0 commit comments

Comments
 (0)