请求路径: 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;
}