mirror of https://github.com/halo-dev/halo
Refactor cc.ryanc.halo.web.controller.api.ApiPostController(issue: #106) again
parent
c293379b66
commit
1c17fe5a33
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.utils;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.BackupDto;
|
import cc.ryanc.halo.model.dto.BackupDto;
|
||||||
import cc.ryanc.halo.model.dto.Theme;
|
import cc.ryanc.halo.model.dto.Theme;
|
||||||
|
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||||
import cc.ryanc.halo.model.enums.CommonParamsEnum;
|
import cc.ryanc.halo.model.enums.CommonParamsEnum;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.text.StrBuilder;
|
||||||
|
@ -24,6 +25,8 @@ import java.nio.file.attribute.BasicFileAttributeView;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 常用工具
|
* 常用工具
|
||||||
|
@ -35,6 +38,21 @@ import java.util.*;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HaloUtils {
|
public class HaloUtils {
|
||||||
|
|
||||||
|
private final static int DEFAULT_PAGE_SIZE = 10;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets default page size.
|
||||||
|
*
|
||||||
|
* @return default page size
|
||||||
|
*/
|
||||||
|
public static int getDefaultPageSize() {
|
||||||
|
if (StrUtil.isNotBlank(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
|
||||||
|
return Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return DEFAULT_PAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取备份文件信息
|
* 获取备份文件信息
|
||||||
*
|
*
|
||||||
|
@ -48,7 +66,7 @@ public class HaloUtils {
|
||||||
final File srcPath = new File(srcPathStr.toString());
|
final File srcPath = new File(srcPathStr.toString());
|
||||||
final File[] files = srcPath.listFiles();
|
final File[] files = srcPath.listFiles();
|
||||||
final List<BackupDto> backupDtos = new ArrayList<>();
|
final List<BackupDto> backupDtos = new ArrayList<>();
|
||||||
BackupDto backupDto = null;
|
BackupDto backupDto;
|
||||||
// 遍历文件
|
// 遍历文件
|
||||||
if (null != files) {
|
if (null != files) {
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
|
@ -107,8 +125,7 @@ public class HaloUtils {
|
||||||
BasicFileAttributes attr;
|
BasicFileAttributes attr;
|
||||||
try {
|
try {
|
||||||
attr = basicview.readAttributes();
|
attr = basicview.readAttributes();
|
||||||
final Date createDate = new Date(attr.creationTime().toMillis());
|
return new Date(attr.creationTime().toMillis());
|
||||||
return createDate;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -147,7 +164,7 @@ public class HaloUtils {
|
||||||
final File themesPath = new File(basePath.getAbsolutePath(), "templates/themes");
|
final File themesPath = new File(basePath.getAbsolutePath(), "templates/themes");
|
||||||
final File[] files = themesPath.listFiles();
|
final File[] files = themesPath.listFiles();
|
||||||
if (null != files) {
|
if (null != files) {
|
||||||
Theme theme = null;
|
Theme theme;
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
if (StrUtil.equals("__MACOSX", file.getName())) {
|
if (StrUtil.equals("__MACOSX", file.getName())) {
|
||||||
|
|
|
@ -5,7 +5,6 @@ import cc.ryanc.halo.model.domain.Category;
|
||||||
import cc.ryanc.halo.model.domain.Post;
|
import cc.ryanc.halo.model.domain.Post;
|
||||||
import cc.ryanc.halo.model.domain.Tag;
|
import cc.ryanc.halo.model.domain.Tag;
|
||||||
import cc.ryanc.halo.model.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
|
||||||
import cc.ryanc.halo.model.enums.PostStatusEnum;
|
import cc.ryanc.halo.model.enums.PostStatusEnum;
|
||||||
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
||||||
import cc.ryanc.halo.service.CategoryService;
|
import cc.ryanc.halo.service.CategoryService;
|
||||||
|
@ -19,12 +18,9 @@ import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.web.SortDefault;
|
import org.springframework.data.web.SortDefault;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
|
import static cc.ryanc.halo.utils.HaloUtils.getDefaultPageSize;
|
||||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -114,14 +110,27 @@ public class ApiPostController {
|
||||||
* @return JsonResult
|
* @return JsonResult
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/page/{page}")
|
@GetMapping(value = "/page/{page}")
|
||||||
public JsonResult posts(@PathVariable(value = "page") Integer page, @SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
public JsonResult posts(@PathVariable(value = "page") Integer page,
|
||||||
int size = 10;
|
@SortDefault(sort = "postDate", direction = DESC) Sort sort,
|
||||||
if (StrUtil.isNotBlank(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
|
@RequestParam(value = "cateUrl", required = false) String cateUrl,
|
||||||
size = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
|
@RequestParam(value = "tagUrl", required = false) String tagUrl) {
|
||||||
|
// Build page info
|
||||||
|
Pageable pageable = PageRequest.of(page - 1, getDefaultPageSize(), sort);
|
||||||
|
|
||||||
|
Page<Post> postPage;
|
||||||
|
|
||||||
|
if (StrUtil.isNotBlank(cateUrl)) {
|
||||||
|
// Query by category url
|
||||||
|
postPage = postService.findPostByCategories(categoryService.findByCateUrl(cateUrl), pageable);
|
||||||
|
} else if (StrUtil.isNotBlank(tagUrl)) {
|
||||||
|
// Query by tag url
|
||||||
|
postPage = postService.findPostsByTags(tagService.findByTagUrl(tagUrl), pageable);
|
||||||
|
} else {
|
||||||
|
// Query default
|
||||||
|
postPage = postService.findPostByStatus(PostStatusEnum.PUBLISHED.getCode(), PostTypeEnum.POST_TYPE_POST.getDesc(), pageable);
|
||||||
}
|
}
|
||||||
final Pageable pageable = PageRequest.of(page - 1, size, sort);
|
|
||||||
final Page<Post> posts = postService.findPostByStatus(PostStatusEnum.PUBLISHED.getCode(), PostTypeEnum.POST_TYPE_POST.getDesc(), pageable);
|
return JsonResult.ok(HttpStatus.OK.getReasonPhrase(), postPage);
|
||||||
return JsonResult.ok(HttpStatus.OK.getReasonPhrase(), posts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,20 +191,15 @@ public class ApiPostController {
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/categories/{cateUrl}/{page}")
|
@GetMapping(value = "/categories/{cateUrl}/{page}")
|
||||||
|
@Deprecated
|
||||||
public JsonResult categories(@PathVariable("cateUrl") String cateUrl,
|
public JsonResult categories(@PathVariable("cateUrl") String cateUrl,
|
||||||
@PathVariable("page") Integer page,
|
@PathVariable("page") Integer page,
|
||||||
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
||||||
final Category category = categoryService.findByCateUrl(cateUrl);
|
final Category category = categoryService.findByCateUrl(cateUrl);
|
||||||
int size = 10;
|
final Pageable pageable = PageRequest.of(page - 1, getDefaultPageSize(), sort);
|
||||||
if (StrUtil.isNotBlank(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
|
|
||||||
size = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
|
|
||||||
}
|
|
||||||
final Pageable pageable = PageRequest.of(page - 1, size, sort);
|
|
||||||
final Page<Post> posts = postService.findPostByCategories(category, pageable);
|
final Page<Post> posts = postService.findPostByCategories(category, pageable);
|
||||||
if (null == posts) {
|
|
||||||
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
|
return JsonResult.ok(HttpStatus.OK.getReasonPhrase(), posts);
|
||||||
}
|
|
||||||
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -207,19 +211,15 @@ public class ApiPostController {
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/tags/{tagUrl}/{page}")
|
@GetMapping(value = "/tags/{tagUrl}/{page}")
|
||||||
|
@Deprecated
|
||||||
public JsonResult tags(@PathVariable("tagUrl") String tagUrl,
|
public JsonResult tags(@PathVariable("tagUrl") String tagUrl,
|
||||||
@PathVariable("page") Integer page,
|
@PathVariable("page") Integer page,
|
||||||
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
||||||
final Tag tag = tagService.findByTagUrl(tagUrl);
|
final Tag tag = tagService.findByTagUrl(tagUrl);
|
||||||
int size = 10;
|
final Pageable pageable = PageRequest.of(page - 1, getDefaultPageSize(), sort);
|
||||||
if (StrUtil.isNotBlank(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
|
|
||||||
size = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
|
|
||||||
}
|
|
||||||
final Pageable pageable = PageRequest.of(page - 1, size, sort);
|
|
||||||
final Page<Post> posts = postService.findPostsByTags(tag, pageable);
|
final Page<Post> posts = postService.findPostsByTags(tag, pageable);
|
||||||
if (null == posts) {
|
|
||||||
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
|
return JsonResult.ok(HttpStatus.OK.getReasonPhrase(), posts);
|
||||||
}
|
|
||||||
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue