Halo开源项目(2.1)---分类模块---GET--->`“/api/admin/categories“`

请求路径: GET—>"/api/admin/categories"
前排提示: 需要了解Java8新特性

[ //请求返回数据, List<CategoryWithPostCountDTO>
  {
    "createTime": "2021-07-08T15:35:31.270Z", // from categories
    "description": "string", // from categories
    "fullPath": "string", ---> // 相应的请求路径
    "id": 0, // from categories
    "name": "string", // from categories
    "parentId": 0, // from categories
    "password": "string", // from categories
    "slug": "string", // from categories
    "thumbnail": "string" // from categories
  }
]

@GetMapping //文章---所有文章(列表  ---> "/api/admin/categories"
@ApiOperation("Lists all categories")
public List<? extends CategoryDTO> listAll(
    @SortDefault(sort = "createTime", direction = DESC) Sort sort,
    @RequestParam(name = "more", required = false, defaultValue = "false") boolean more) { // 1.点击文章---所有文章  默认携带参数more=true
    // 2. more = true 
    if (more) {
        // 3. 进入listCategoryWithPostCountDto(sort = "createTime" ,true) 
        return postCategoryService.listCategoryWithPostCountDto(sort, true);
    }
    return categoryService.convertTo(categoryService.listAll(sort, true));
}

@Override
public List<CategoryWithPostCountDTO> listCategoryWithPostCountDto(
    Sort sort, boolean queryEncryptCategory) {
    // sort参数不为空
    Assert.notNull(sort, "Sort info must not be null");
    // 4. 进入listAll方法 ---> 获取categories分类的所有信息  --> 返回List<Category>
    List<Category> categories = categoryService.listAll(sort, queryEncryptCategory);

    // Query category post count  // 8. ::双冒号(方法引用) 和 stream API 是java8新特性,去补一波课 --> 进入convertToMap
    Map<Integer, Long> categoryPostCountMap = ServiceUtils
        .convertToMap(postCategoryRepository.findPostCount(), // 找出postCategory所有数据,返回一个List
            CategoryPostCountProjection::getCategoryId,     // lambda表达式 --> 方法引用 --> Projection是Spring Data JPA的一种用法
            CategoryPostCountProjection::getPostCount);  // XXXProjection即为接口类,直接使用这个接口类接受返回的数据

    // Convert and return
    return categories.stream()
        .map(category -> {
            // 10. 创建 CategoryWithPostCountDTO , 由实体类category数据new CategoryWithPostCountDTO
            CategoryWithPostCountDTO categoryWithPostCountDTO =
                new CategoryWithPostCountDTO().convertFrom(category);
            // 11. Set postcount
            categoryWithPostCountDTO  // getOrDefault得到输入key对应的value值,若不存在,则value值为默认值0L
                .setPostCount(categoryPostCountMap.getOrDefault(category.getId(), 0L));

            StringBuilder fullPath = new StringBuilder();

            if (optionService.isEnabledAbsolutePath()) {
                fullPath.append(optionService.getBlogBaseUrl());
            }

            fullPath.append(URL_SEPARATOR)  // "/"
                        .append(optionService.getCategoriesPrefix()) //categories
                        .append(URL_SEPARATOR)  // "/"
                        .append(category.getSlug())  //  slug
                        .append(optionService.getPathSuffix());

            return categoryWithPostCountDTO;
        })
        .collect(Collectors.toList());
}

@Override
public List<Category> listAll(Sort sort, boolean queryEncryptCategory) {
    // 5. queryEncryptCategory =true
    if (queryEncryptCategory) {
        // 6. 执行父类的listAll方法
        return super.listAll(sort);
    } else {
        return this.listAll(sort);
    }
}

@Override
public List<DOMAIN> listAll(Sort sort) {
    // sort不为空
    Assert.notNull(sort, "Sort info must not be null");
    // 7.执行repository的findAll方法,并排序返回一个List<Category>
    return repository.findAll(sort);
}

@NonNull
public static <I, D, V> Map<I, V> convertToMap(@Nullable Collection<D> list,
    @NonNull Function<D, I> keyFunction, @NonNull Function<D, V> valueFunction) {
    Assert.notNull(keyFunction, "Key function must not be null");
    Assert.notNull(valueFunction, "Value function must not be null");

    if (CollectionUtils.isEmpty(list)) {
        return Collections.emptyMap();
    }

    Map<I, V> resultMap = new HashMap<>();
    //遍历postCategoryList
    list.forEach(
        // 9. putIfAbsent-->若value不存在将data放入resultMap  ---> lambda表达式  --> key为CategoryId , value为PostCount
        data -> resultMap.putIfAbsent(keyFunction.apply(data), valueFunction.apply(data))); 

    return resultMap;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值