Skip to content

Commit

Permalink
[Storefront] [Category] Create api to get the top n categories (#1220)
Browse files Browse the repository at this point in the history
* Create api to get the top n category

* Configure variable name
  • Loading branch information
HnKnA authored Oct 23, 2024
1 parent d8b6d28 commit 9e83bcd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ void test_getAllCategoriesStorefront_shouldReturnListCategories() {
.log().ifValidationFails();
}

@Test
void test_getTopNthCategories_shouldReturnEmptyList() {
given(getRequestSpecification())
.when()
.get(CATEGORY_STOREFRONT_URL + "/suggestions")
.then()
.statusCode(HttpStatus.OK.value())
.body(".", hasSize(0))
.log().ifValidationFails();
}

@Test
void test_getCategoryById_shouldReturnCategory() {
Long categoryId = categoryOne.getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yas.product.controller;

import com.yas.commonlibrary.exception.BadRequestException;
import com.yas.product.constants.PageableConstant;
import com.yas.product.model.Category;
import com.yas.product.repository.CategoryRepository;
import com.yas.product.service.CategoryService;
Expand Down Expand Up @@ -43,6 +44,13 @@ public ResponseEntity<List<CategoryGetVm>> listCategories(
return ResponseEntity.ok(categoryService.getCategories(categoryName));
}

@GetMapping("/storefront/categories/suggestions")
public ResponseEntity<List<String>> listTopNthCategories(
@RequestParam(value = "limit", defaultValue = PageableConstant.DEFAULT_PAGE_SIZE, required = false)
final int limit) {
return ResponseEntity.ok(categoryService.getTopNthCategories(limit));
}

@GetMapping("/backoffice/categories/{id}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Ok",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.yas.product.model.Category;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -15,4 +16,18 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
Category findExistedName(String name, Long id);

List<Category> findByNameContainingIgnoreCase(String name);

/**
* Retrieves a list of category names ordered by the number of associated products in descending order.
* Limits the results based on the given {@link Pageable} parameter.
*
* @param pageable specifies the number of results and pagination details.
* @return a list of category names sorted by product count in descending order.
*/
@Query("SELECT c.name FROM Category c "
+ "JOIN c.productCategories pc "
+ "GROUP BY c.name "
+ "ORDER BY COUNT(pc.product.id) DESC")
List<String> findCategoriesOrderedByProductCount(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@ private boolean checkParent(Long id, Category category) {
public List<CategoryGetVm> getCategoryByIds(List<Long> ids) {
return categoryRepository.findAllById(ids).stream().map(CategoryGetVm::fromModel).toList();
}

public List<String> getTopNthCategories(int limit) {
// Use PageRequest.of(0, n) to limit the number of categories fetched to 'n'
Pageable pageable = PageRequest.of(0, limit);
return categoryRepository.findCategoriesOrderedByProductCount(pageable);
}
}

0 comments on commit 9e83bcd

Please sign in to comment.