mirror of https://github.com/halo-dev/halo
pull/137/head
parent
8ec7cef1e5
commit
fa9cf826ee
|
@ -3,6 +3,7 @@ package cc.ryanc.halo.model.domain;
|
||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import org.hibernate.annotations.ColumnDefault;
|
import org.hibernate.annotations.ColumnDefault;
|
||||||
|
@ -68,6 +69,7 @@ public class Post implements Serializable {
|
||||||
* 文章内容 html格式
|
* 文章内容 html格式
|
||||||
*/
|
*/
|
||||||
@Lob
|
@Lob
|
||||||
|
@JsonIgnore
|
||||||
private String postContent;
|
private String postContent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
package cc.ryanc.halo.web.controller.api;
|
package cc.ryanc.halo.web.controller.api;
|
||||||
|
|
||||||
import cc.ryanc.halo.exception.NotFoundException;
|
import cc.ryanc.halo.exception.NotFoundException;
|
||||||
|
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.dto.JsonResult;
|
import cc.ryanc.halo.model.dto.JsonResult;
|
||||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
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.PostService;
|
import cc.ryanc.halo.service.PostService;
|
||||||
|
import cc.ryanc.halo.service.TagService;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -38,6 +42,12 @@ public class ApiPostController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PostService postService;
|
private PostService postService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CategoryService categoryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TagService tagService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文章列表 分页
|
* 获取文章列表 分页
|
||||||
*
|
*
|
||||||
|
@ -163,4 +173,53 @@ public class ApiPostController {
|
||||||
|
|
||||||
return post;
|
return post;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据分类目录查询所有文章 分页
|
||||||
|
*
|
||||||
|
* @param cateUrl 分类目录路径
|
||||||
|
* @param page 页码
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/categories/{cateUrl}/{page}")
|
||||||
|
public JsonResult categories(@PathVariable("cateUrl") String cateUrl,
|
||||||
|
@PathVariable("page") Integer page,
|
||||||
|
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
||||||
|
final Category category = categoryService.findByCateUrl(cateUrl);
|
||||||
|
int size = 10;
|
||||||
|
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);
|
||||||
|
if (null == posts) {
|
||||||
|
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
|
||||||
|
}
|
||||||
|
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据标签路径查询所有文章 分页
|
||||||
|
*
|
||||||
|
* @param tagUrl 标签路径
|
||||||
|
* @param page 页码
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/tags/{tagUrl}/{page}")
|
||||||
|
public JsonResult tags(@PathVariable("tagUrl") String tagUrl,
|
||||||
|
@PathVariable("page") Integer page,
|
||||||
|
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
|
||||||
|
final Tag tag = tagService.findByTagUrl(tagUrl);
|
||||||
|
int size = 10;
|
||||||
|
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);
|
||||||
|
if (null == posts) {
|
||||||
|
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
|
||||||
|
}
|
||||||
|
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue