java – An efficient way to order content in controller [Spring Boot]

My code works as intended. CategoryController class has getAllCategories method, which tells ContentOrderMap class to map differently ordered categories to their respective OrderType enum.

The intention is whenever @RequestParam(defaultValue = "DEFAULT") OrderType categoriesOrderType changes from DEFAULT, to lets say, POST_COUNT_DESC, I would get differently ordered categories in my view.

My concern is if this is an efficient way of dealing with multiple ways of ordering my content. Previously I had a bunch of if statements in my controller and they would take care of getting and assigning information to model based on request param. This didn’t seem very scalable since my controller method would get very big and would contain logic, which I think should not be there.

I’m having trouble correctly wording my problem and issues since I am new to this, if anything is not clear enough, I will gladly reword or explain.


public class CategoryController {

private CategoryService categoryService;
private ContentOrderMap orderMap;

public CategoryController(CategoryService categoryService, ContentOrderMap orderMap) {
    this.categoryService = categoryService;
    this.orderMap = orderMap;

    public String getAllCategories(@RequestParam(defaultValue = "0") int pageNumber, Model model, @RequestParam(defaultValue = "DEFAULT") OrderType categoriesOrderType) {
        Page<Category> categories = orderMap.categoriesByOrderType.get(categoriesOrderType);
        int pageCount = categoryService.getAllCategories(pageNumber).getTotalPages();
        model.addAttribute("orderType", categoriesOrderType);
        model.addAttribute("categories", categories.getContent());
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("hasNextPage", categories.hasNext());
        model.addAttribute("pageCount", pageCount);
        return "categories";

ContentOrderMap class

public Map<OrderType, Page<Category>> categoriesByOrderType = new HashMap<>();
public void mapCategoriesToOrderType(int pageNumber) {
        categoriesByOrderType.put(OrderType.DEFAULT, categoryService.getAllCategories(pageNumber));
        categoriesByOrderType.put(OrderType.POST_COUNT_DESC, categoryService.getAllCategoriesByPostCountDesc(pageNumber));
        categoriesByOrderType.put(OrderType.POST_COUNT_ASC, categoryService.getAllCategoriesByPostCountAsc(pageNumber));
        categoriesByOrderType.put(OrderType.FOLLOWER_COUNT_DESC, categoryService.getAllCategoriesByFollowersCountDesc(pageNumber));
        categoriesByOrderType.put(OrderType.FOLLOWER_COUNT_ASC, categoryService.getAllCategoriesByFollowersCountAsc(pageNumber));
        categoriesByOrderType.put(OrderType.POST_DATE_DESC, categoryService.getAllCategoriesByNewestPost(pageNumber));
        categoriesByOrderType.put(OrderType.POST_DATE_ASC, categoryService.getAllCategoriesByOldestPost(pageNumber));


all of the methods seen above are more or less identical in service class, they only differ in their naming, so I am going to add only one of them for brevity’s sake

public Page<Category> getAllCategoriesByPostCountDesc(int pageNumber) {
        Pageable pageable = PageRequest.of(pageNumber, 4);
        return categoryRepository.findAllOrderByPostCountDesc(pageable);

In case its needed, I add an example of how categoryRepository looks

@Query(value = "SELECT c.* FROM category c LEFT JOIN review r ON = r.category_id GROUP BY ORDER BY COUNT(*) DESC",
            countQuery = "SELECT COUNT(*) FROM CATEGORY",
            nativeQuery = true)
    Page<Category> findAllOrderByPostCountDesc(Pageable pageable);