feat: create get by date api for post.

pull/484/head
ruibaby 2020-01-08 12:28:25 +08:00
parent bb58dd5ec4
commit 76dd2b2c72
12 changed files with 183 additions and 47 deletions

View File

@ -40,10 +40,14 @@ public class PostModel {
private final PostCategoryService postCategoryService; private final PostCategoryService postCategoryService;
private final PostMetaService postMetaService; private final CategoryService categoryService;
private final PostTagService postTagService; private final PostTagService postTagService;
private final TagService tagService;
private final PostMetaService postMetaService;
private final OptionService optionService; private final OptionService optionService;
private final StringCacheStore cacheStore; private final StringCacheStore cacheStore;
@ -51,15 +55,19 @@ public class PostModel {
public PostModel(PostService postService, public PostModel(PostService postService,
ThemeService themeService, ThemeService themeService,
PostCategoryService postCategoryService, PostCategoryService postCategoryService,
CategoryService categoryService,
PostMetaService postMetaService, PostMetaService postMetaService,
PostTagService postTagService, PostTagService postTagService,
TagService tagService,
OptionService optionService, OptionService optionService,
StringCacheStore cacheStore) { StringCacheStore cacheStore) {
this.postService = postService; this.postService = postService;
this.themeService = themeService; this.themeService = themeService;
this.postCategoryService = postCategoryService; this.postCategoryService = postCategoryService;
this.categoryService = categoryService;
this.postMetaService = postMetaService; this.postMetaService = postMetaService;
this.postTagService = postTagService; this.postTagService = postTagService;
this.tagService = tagService;
this.optionService = optionService; this.optionService = optionService;
this.cacheStore = cacheStore; this.cacheStore = cacheStore;
} }
@ -89,8 +97,8 @@ public class PostModel {
model.addAttribute("is_post", true); model.addAttribute("is_post", true);
model.addAttribute("post", postService.convertToDetailVo(post)); model.addAttribute("post", postService.convertToDetailVo(post));
model.addAttribute("categories", categories); model.addAttribute("categories", categoryService.convertTo(categories));
model.addAttribute("tags", tags); model.addAttribute("tags", tagService.convertTo(tags));
model.addAttribute("metas", postMetaService.convertToMap(metas)); model.addAttribute("metas", postMetaService.convertToMap(metas));
// TODO,Will be deprecated // TODO,Will be deprecated

View File

@ -12,7 +12,8 @@ import java.util.Date;
* Category output dto. * Category output dto.
* *
* @author johnniang * @author johnniang
* @date 3/19/19 * @author ryanwang
* @date 2019-03-19
*/ */
@Data @Data
@ToString @ToString
@ -30,4 +31,6 @@ public class CategoryDTO implements OutputConverter<CategoryDTO, Category> {
private Integer parentId; private Integer parentId;
private Date createTime; private Date createTime;
private String fullPath;
} }

View File

@ -10,7 +10,8 @@ import java.util.Date;
* Tag output dto. * Tag output dto.
* *
* @author johnniang * @author johnniang
* @date 3/19/19 * @author ryanwang
* @date 2019-03-19
*/ */
@Data @Data
public class TagDTO implements OutputConverter<TagDTO, Tag> { public class TagDTO implements OutputConverter<TagDTO, Tag> {
@ -22,4 +23,6 @@ public class TagDTO implements OutputConverter<TagDTO, Tag> {
private String slugName; private String slugName;
private Date createTime; private Date createTime;
private String fullPath;
} }

View File

@ -13,6 +13,8 @@ import java.util.Date;
* Base post minimal output dto. * Base post minimal output dto.
* *
* @author johnniang * @author johnniang
* @author ryanwang
* @date 2019-03-19
*/ */
@Data @Data
@ToString @ToString
@ -32,4 +34,6 @@ public class BasePostMinimalDTO implements OutputConverter<BasePostMinimalDTO, B
private Date createTime; private Date createTime;
private Date editTime; private Date editTime;
private String fullPath;
} }

View File

@ -21,8 +21,6 @@ import java.util.List;
@Data @Data
public class PostListVO extends BasePostSimpleDTO { public class PostListVO extends BasePostSimpleDTO {
private String fullPath;
private Long commentCount; private Long commentCount;
private List<TagDTO> tags; private List<TagDTO> tags;

View File

@ -2,15 +2,20 @@ package run.halo.app.repository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.repository.base.BasePostRepository; import run.halo.app.repository.base.BasePostRepository;
import java.util.Optional;
/** /**
* Post repository. * Post repository.
* *
* @author johnniang * @author johnniang
* @author ryanwang * @author ryanwang
* @date 2019-03-19
*/ */
public interface PostRepository extends BasePostRepository<Post>, JpaSpecificationExecutor<Post> { public interface PostRepository extends BasePostRepository<Post>, JpaSpecificationExecutor<Post> {
@ -32,4 +37,51 @@ public interface PostRepository extends BasePostRepository<Post>, JpaSpecificati
@Query("select sum(p.likes) from Post p") @Query("select sum(p.likes) from Post p")
Long countLike(); Long countLike();
/**
* Find by post year and month and url.
*
* @param year post create year
* @param month post create month
* @param url post url
* @return a optional of post
*/
@Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and post.url = :url")
Optional<Post> findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("url") String url);
/**
* Find by post year and month and url and status.
*
* @param year post create year
* @param month post create month
* @param url post url
* @param status post status
* @return a optional of post
*/
@Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and post.url = :url and post.status = :status")
Optional<Post> findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("url") String url, @Param("status") PostStatus status);
/**
* Find by post year and month and day and url.
*
* @param year post create year
* @param month post create month
* @param day post create day
* @param url post url
* @return a optional of post
*/
@Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and DateUtil.day(post.createTime) = :day and post.url = :url")
Optional<Post> findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day, @Param("url") String url);
/**
* Find by post year and month and day and url and status.
*
* @param year post create year
* @param month post create month
* @param day post create day
* @param url post url
* @param status post status
* @return a optional of post
*/
@Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and DateUtil.day(post.createTime) = :day and post.url = :url and post.status = :status")
Optional<Post> findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day, @Param("url") String url, @Param("status") PostStatus status);
} }

View File

@ -3,7 +3,6 @@ package run.halo.app.service;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import run.halo.app.model.dto.post.BasePostDetailDTO;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta; import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
@ -96,6 +95,54 @@ public interface PostService extends BasePostService<Post> {
@Override @Override
Post getBy(@NonNull PostStatus status, @NonNull String url); Post getBy(@NonNull PostStatus status, @NonNull String url);
/**
* Gets post by post year and month and url.
*
* @param year post create year.
* @param month post create month.
* @param url post url.
* @return post info
*/
@NonNull
Post getBy(@NonNull Integer year, @NonNull Integer month, @NonNull String url);
/**
* Gets post by post year and month and url.
*
* @param year post create year.
* @param month post create month.
* @param url post url.
* @param status post status.
* @return post info
*/
@NonNull
Post getBy(@NonNull Integer year, @NonNull Integer month, @NonNull String url, @NonNull PostStatus status);
/**
* Gets post by post year and month and url.
*
* @param year post create year.
* @param month post create month.
* @param day post create day.
* @param url post url.
* @return post info
*/
@NonNull
Post getBy(@NonNull Integer year, @NonNull Integer month, @NonNull Integer day, @NonNull String url);
/**
* Gets post by post year and month and url.
*
* @param year post create year.
* @param month post create month.
* @param day post create day.
* @param url post url.
* @param status post status.
* @return post info
*/
@NonNull
Post getBy(@NonNull Integer year, @NonNull Integer month, @NonNull Integer day, @NonNull String url, @NonNull PostStatus status);
/** /**
* Removes posts in batch. * Removes posts in batch.
* *
@ -167,14 +214,6 @@ public interface PostService extends BasePostService<Post> {
@NonNull @NonNull
Page<PostListVO> convertToListVo(@NonNull Page<Post> postPage); Page<PostListVO> convertToListVo(@NonNull Page<Post> postPage);
/**
* Converts to a page of post detail dto.
*
* @param postPage post page must not be null
* @return a page of post detail dto
*/
Page<BasePostDetailDTO> convertToDetailDto(@NonNull Page<Post> postPage);
/** /**
* Converts to a page of detail vo. * Converts to a page of detail vo.
* *

View File

@ -18,10 +18,8 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import run.halo.app.event.logger.LogEvent; import run.halo.app.event.logger.LogEvent;
import run.halo.app.event.post.PostVisitEvent; import run.halo.app.event.post.PostVisitEvent;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.BaseMetaDTO; import run.halo.app.model.dto.BaseMetaDTO;
import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.dto.post.BasePostDetailDTO;
import run.halo.app.model.entity.*; import run.halo.app.model.entity.*;
import run.halo.app.model.enums.LogType; import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostPermalinkType; import run.halo.app.model.enums.PostPermalinkType;
@ -160,9 +158,55 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
@Override @Override
public Post getBy(PostStatus status, String url) { public Post getBy(PostStatus status, String url) {
Post post = super.getBy(status, url); return super.getBy(status, url);
}
return post; @Override
public Post getBy(Integer year, Integer month, String url) {
Assert.notNull(year, "Post create year must not be null");
Assert.notNull(month, "Post create month must not be null");
Assert.notNull(url, "Post url must not be null");
Optional<Post> postOptional = postRepository.findBy(year, month, url);
return postOptional.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(url));
}
@Override
public Post getBy(Integer year, Integer month, String url, PostStatus status) {
Assert.notNull(year, "Post create year must not be null");
Assert.notNull(month, "Post create month must not be null");
Assert.notNull(url, "Post url must not be null");
Assert.notNull(status, "Post status must not be null");
Optional<Post> postOptional = postRepository.findBy(year, month, url, status);
return postOptional.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(url));
}
@Override
public Post getBy(Integer year, Integer month, Integer day, String url) {
Assert.notNull(year, "Post create year must not be null");
Assert.notNull(month, "Post create month must not be null");
Assert.notNull(day, "Post create day must not be null");
Assert.notNull(url, "Post url must not be null");
Optional<Post> postOptional = postRepository.findBy(year, month, day, url);
return postOptional.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(url));
}
@Override
public Post getBy(Integer year, Integer month, Integer day, String url, PostStatus status) {
Assert.notNull(year, "Post create year must not be null");
Assert.notNull(month, "Post create month must not be null");
Assert.notNull(day, "Post create day must not be null");
Assert.notNull(url, "Post url must not be null");
Assert.notNull(status, "Post status must not be null");
Optional<Post> postOptional = postRepository.findBy(year, month, day, url, status);
return postOptional.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(url));
} }
@Override @Override
@ -175,9 +219,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
@Override @Override
public Post getByUrl(String url) { public Post getByUrl(String url) {
Post post = super.getByUrl(url); return super.getByUrl(url);
return post;
} }
@Override @Override
@ -472,7 +514,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
.orElseGet(LinkedList::new) .orElseGet(LinkedList::new)
.stream() .stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(tag -> (TagDTO) new TagDTO().convertFrom(tag)) .map(tagService::convertTo)
.collect(Collectors.toList())); .collect(Collectors.toList()));
// Set categories // Set categories
@ -480,7 +522,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
.orElseGet(LinkedList::new) .orElseGet(LinkedList::new)
.stream() .stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(category -> (CategoryDTO) new CategoryDTO().convertFrom(category)) .map(categoryService::convertTo)
.collect(Collectors.toList())); .collect(Collectors.toList()));
// Set post metas // Set post metas
@ -512,19 +554,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
}); });
} }
@Override
public Page<BasePostDetailDTO> convertToDetailDto(Page<Post> postPage) {
Assert.notNull(postPage, "Post page must not be null");
return postPage.map(post -> {
BasePostDetailDTO postDetailDTO = new BasePostDetailDTO().convertFrom(post);
if (StringUtils.isBlank(postDetailDTO.getSummary())) {
postDetailDTO.setSummary(generateSummary(post.getFormatContent()));
}
return postDetailDTO;
});
}
@Override @Override
public Page<PostDetailVO> convertToDetailVo(Page<Post> postPage) { public Page<PostDetailVO> convertToDetailVo(Page<Post> postPage) {
Assert.notNull(postPage, "Post page must not be null"); Assert.notNull(postPage, "Post page must not be null");

View File

@ -14,7 +14,7 @@
<#list archive.posts?sort_by("createTime")?reverse as post> <#list archive.posts?sort_by("createTime")?reverse as post>
<div class="listing-item"> <div class="listing-item">
<div class="listing-post"> <div class="listing-post">
<a href="${context!}/archives/${post.url!}" title="${post.title!}">${post.title!}</a> <a href="${post.fullPath!}" title="${post.title!}">${post.title!}</a>
<div class="post-time"> <div class="post-time">
<span class="date">${post.createTime?string("yyyy-MM-dd")}</span> <span class="date">${post.createTime?string("yyyy-MM-dd")}</span>
</div> </div>

View File

@ -19,7 +19,7 @@
<i class="fa fa-sun-o"></i> <i class="fa fa-sun-o"></i>
<span class="date">${post.createTime?string("yyyy-MM-dd")}</span> <span class="date">${post.createTime?string("yyyy-MM-dd")}</span>
<i class="fa fa-comment-o"></i> <i class="fa fa-comment-o"></i>
<a href="${context!}/archives/${post.url}#comment_widget">Comments</a> <a href="${post.fullPath!}#comment_widget">Comments</a>
<#if post.tags?size gt 0> <#if post.tags?size gt 0>
<i class="fa fa-tag"></i> <i class="fa fa-tag"></i>
<#list post.tags as tag> <#list post.tags as tag>

View File

@ -21,7 +21,7 @@
<i class="fa fa-sun-o"></i> <i class="fa fa-sun-o"></i>
<span class="date">${post.createTime?string("yyyy-MM-dd")}</span> <span class="date">${post.createTime?string("yyyy-MM-dd")}</span>
<i class="fa fa-comment-o"></i> <i class="fa fa-comment-o"></i>
<a href="${context!}/archives/${post.url}#comment_widget">Comments</a> <a href="${post.fullPath!}#comment_widget">Comments</a>
<#if tags?size gt 0> <#if tags?size gt 0>
<i class="fa fa-tag"></i> <i class="fa fa-tag"></i>
<#list tags as tag> <#list tags as tag>
@ -42,7 +42,7 @@
class="fa fa-weibo"></a> class="fa fa-weibo"></a>
</div> </div>
<div class="twitter"> <div class="twitter">
<a href="http://twitter.com/home?status=${context!}/archives/${post.url} ,${options.blog_title!},${post.title},;" <a href="http://twitter.com/home?status=${post.fullPath!} ,${options.blog_title!},${post.title},;"
class="fa fa-twitter"></a> class="fa fa-twitter"></a>
</div> </div>
</div> </div>
@ -50,12 +50,12 @@
<ul class="clearfix"> <ul class="clearfix">
<#if nextPost??> <#if nextPost??>
<li class="next pagbuttons"> <li class="next pagbuttons">
<a class="btn" role="navigation" href="${context!}/archives/${nextPost.url}" title="${nextPost.title}">下一篇</a> <a class="btn" role="navigation" href="${nextPost.fullPath!}" title="${nextPost.title}">下一篇</a>
</li> </li>
</#if> </#if>
<#if prePost??> <#if prePost??>
<li class="pre pagbuttons"> <li class="pre pagbuttons">
<a class="btn" role="navigation" href="${context!}/archives/${prePost.url}" title="${prePost.title}">上一篇</a> <a class="btn" role="navigation" href="${prePost.fullPath!}" title="${prePost.title}">上一篇</a>
</li> </li>
</#if> </#if>
</ul> </ul>

View File

@ -21,7 +21,7 @@
<i class="fa fa-sun-o"></i> <i class="fa fa-sun-o"></i>
<span class="date">${sheet.createTime?string("yyyy-MM-dd")}</span> <span class="date">${sheet.createTime?string("yyyy-MM-dd")}</span>
<i class="fa fa-comment-o"></i> <i class="fa fa-comment-o"></i>
<a href="${context!}/archives/${sheet.url!}#comment_widget">Comments</a> <a href="${sheet.fullPath!}#comment_widget">Comments</a>
</div> </div>
</div> </div>
</div> </div>
@ -36,7 +36,7 @@
class="fa fa-weibo"></a> class="fa fa-weibo"></a>
</div> </div>
<div class="twitter"> <div class="twitter">
<a href="http://twitter.com/home?status=${context!}/archives/${sheet.url!} ,${options.blog_title!},${sheet.title!},;" <a href="http://twitter.com/home?status=${sheet.fullPath!} ,${options.blog_title!},${sheet.title!},;"
class="fa fa-twitter"></a> class="fa fa-twitter"></a>
</div> </div>
</div> </div>