From 76dd2b2c7265590ab5ed7952d3ef3a8bdb7af5ea Mon Sep 17 00:00:00 2001 From: ruibaby Date: Wed, 8 Jan 2020 12:28:25 +0800 Subject: [PATCH] feat: create get by date api for post. --- .../controller/content/model/PostModel.java | 14 +++- .../run/halo/app/model/dto/CategoryDTO.java | 5 +- .../java/run/halo/app/model/dto/TagDTO.java | 5 +- .../model/dto/post/BasePostMinimalDTO.java | 4 + .../run/halo/app/model/vo/PostListVO.java | 2 - .../halo/app/repository/PostRepository.java | 52 +++++++++++++ .../run/halo/app/service/PostService.java | 57 +++++++++++--- .../app/service/impl/PostServiceImpl.java | 75 +++++++++++++------ .../templates/themes/anatole/archives.ftl | 2 +- .../themes/anatole/module/post_entry.ftl | 2 +- .../templates/themes/anatole/post.ftl | 8 +- .../templates/themes/anatole/sheet.ftl | 4 +- 12 files changed, 183 insertions(+), 47 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/model/PostModel.java b/src/main/java/run/halo/app/controller/content/model/PostModel.java index 493d2e88d..2f0ed1ab0 100644 --- a/src/main/java/run/halo/app/controller/content/model/PostModel.java +++ b/src/main/java/run/halo/app/controller/content/model/PostModel.java @@ -40,10 +40,14 @@ public class PostModel { private final PostCategoryService postCategoryService; - private final PostMetaService postMetaService; + private final CategoryService categoryService; private final PostTagService postTagService; + private final TagService tagService; + + private final PostMetaService postMetaService; + private final OptionService optionService; private final StringCacheStore cacheStore; @@ -51,15 +55,19 @@ public class PostModel { public PostModel(PostService postService, ThemeService themeService, PostCategoryService postCategoryService, + CategoryService categoryService, PostMetaService postMetaService, PostTagService postTagService, + TagService tagService, OptionService optionService, StringCacheStore cacheStore) { this.postService = postService; this.themeService = themeService; this.postCategoryService = postCategoryService; + this.categoryService = categoryService; this.postMetaService = postMetaService; this.postTagService = postTagService; + this.tagService = tagService; this.optionService = optionService; this.cacheStore = cacheStore; } @@ -89,8 +97,8 @@ public class PostModel { model.addAttribute("is_post", true); model.addAttribute("post", postService.convertToDetailVo(post)); - model.addAttribute("categories", categories); - model.addAttribute("tags", tags); + model.addAttribute("categories", categoryService.convertTo(categories)); + model.addAttribute("tags", tagService.convertTo(tags)); model.addAttribute("metas", postMetaService.convertToMap(metas)); // TODO,Will be deprecated diff --git a/src/main/java/run/halo/app/model/dto/CategoryDTO.java b/src/main/java/run/halo/app/model/dto/CategoryDTO.java index 2f835c05c..d16ea1a2d 100644 --- a/src/main/java/run/halo/app/model/dto/CategoryDTO.java +++ b/src/main/java/run/halo/app/model/dto/CategoryDTO.java @@ -12,7 +12,8 @@ import java.util.Date; * Category output dto. * * @author johnniang - * @date 3/19/19 + * @author ryanwang + * @date 2019-03-19 */ @Data @ToString @@ -30,4 +31,6 @@ public class CategoryDTO implements OutputConverter { private Integer parentId; private Date createTime; + + private String fullPath; } diff --git a/src/main/java/run/halo/app/model/dto/TagDTO.java b/src/main/java/run/halo/app/model/dto/TagDTO.java index 2e871c7f8..4498b6d18 100644 --- a/src/main/java/run/halo/app/model/dto/TagDTO.java +++ b/src/main/java/run/halo/app/model/dto/TagDTO.java @@ -10,7 +10,8 @@ import java.util.Date; * Tag output dto. * * @author johnniang - * @date 3/19/19 + * @author ryanwang + * @date 2019-03-19 */ @Data public class TagDTO implements OutputConverter { @@ -22,4 +23,6 @@ public class TagDTO implements OutputConverter { private String slugName; private Date createTime; + + private String fullPath; } diff --git a/src/main/java/run/halo/app/model/dto/post/BasePostMinimalDTO.java b/src/main/java/run/halo/app/model/dto/post/BasePostMinimalDTO.java index a9b482484..cc3619cfe 100644 --- a/src/main/java/run/halo/app/model/dto/post/BasePostMinimalDTO.java +++ b/src/main/java/run/halo/app/model/dto/post/BasePostMinimalDTO.java @@ -13,6 +13,8 @@ import java.util.Date; * Base post minimal output dto. * * @author johnniang + * @author ryanwang + * @date 2019-03-19 */ @Data @ToString @@ -32,4 +34,6 @@ public class BasePostMinimalDTO implements OutputConverter tags; diff --git a/src/main/java/run/halo/app/repository/PostRepository.java b/src/main/java/run/halo/app/repository/PostRepository.java index 6064bf491..76628a72b 100644 --- a/src/main/java/run/halo/app/repository/PostRepository.java +++ b/src/main/java/run/halo/app/repository/PostRepository.java @@ -2,15 +2,20 @@ package run.halo.app.repository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 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.enums.PostStatus; import run.halo.app.repository.base.BasePostRepository; +import java.util.Optional; + /** * Post repository. * * @author johnniang * @author ryanwang + * @date 2019-03-19 */ public interface PostRepository extends BasePostRepository, JpaSpecificationExecutor { @@ -32,4 +37,51 @@ public interface PostRepository extends BasePostRepository, JpaSpecificati @Query("select sum(p.likes) from Post p") 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 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 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 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 findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day, @Param("url") String url, @Param("status") PostStatus status); } diff --git a/src/main/java/run/halo/app/service/PostService.java b/src/main/java/run/halo/app/service/PostService.java index dbb978cf2..93bc4761f 100755 --- a/src/main/java/run/halo/app/service/PostService.java +++ b/src/main/java/run/halo/app/service/PostService.java @@ -3,7 +3,6 @@ package run.halo.app.service; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; 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.PostMeta; import run.halo.app.model.enums.PostStatus; @@ -96,6 +95,54 @@ public interface PostService extends BasePostService { @Override 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. * @@ -167,14 +214,6 @@ public interface PostService extends BasePostService { @NonNull Page convertToListVo(@NonNull Page 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 convertToDetailDto(@NonNull Page postPage); - /** * Converts to a page of detail vo. * diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index d98452449..e9a74efa9 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -18,10 +18,8 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import run.halo.app.event.logger.LogEvent; 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.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.enums.LogType; import run.halo.app.model.enums.PostPermalinkType; @@ -160,9 +158,55 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe @Override 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 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 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 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 postOptional = postRepository.findBy(year, month, day, url, status); + + return postOptional.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(url)); } @Override @@ -175,9 +219,7 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe @Override public Post getByUrl(String url) { - Post post = super.getByUrl(url); - - return post; + return super.getByUrl(url); } @Override @@ -472,7 +514,7 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe .orElseGet(LinkedList::new) .stream() .filter(Objects::nonNull) - .map(tag -> (TagDTO) new TagDTO().convertFrom(tag)) + .map(tagService::convertTo) .collect(Collectors.toList())); // Set categories @@ -480,7 +522,7 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe .orElseGet(LinkedList::new) .stream() .filter(Objects::nonNull) - .map(category -> (CategoryDTO) new CategoryDTO().convertFrom(category)) + .map(categoryService::convertTo) .collect(Collectors.toList())); // Set post metas @@ -512,19 +554,6 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe }); } - @Override - public Page convertToDetailDto(Page 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 public Page convertToDetailVo(Page postPage) { Assert.notNull(postPage, "Post page must not be null"); diff --git a/src/main/resources/templates/themes/anatole/archives.ftl b/src/main/resources/templates/themes/anatole/archives.ftl index 1fe62fc40..307ea7295 100644 --- a/src/main/resources/templates/themes/anatole/archives.ftl +++ b/src/main/resources/templates/themes/anatole/archives.ftl @@ -14,7 +14,7 @@ <#list archive.posts?sort_by("createTime")?reverse as post>
- ${post.title!} + ${post.title!}
${post.createTime?string("yyyy-MM-dd")}
diff --git a/src/main/resources/templates/themes/anatole/module/post_entry.ftl b/src/main/resources/templates/themes/anatole/module/post_entry.ftl index e9f90b98e..88ead067b 100644 --- a/src/main/resources/templates/themes/anatole/module/post_entry.ftl +++ b/src/main/resources/templates/themes/anatole/module/post_entry.ftl @@ -19,7 +19,7 @@ ${post.createTime?string("yyyy-MM-dd")} - Comments + Comments <#if post.tags?size gt 0> <#list post.tags as tag> diff --git a/src/main/resources/templates/themes/anatole/post.ftl b/src/main/resources/templates/themes/anatole/post.ftl index 78baafa2c..90f57bd77 100644 --- a/src/main/resources/templates/themes/anatole/post.ftl +++ b/src/main/resources/templates/themes/anatole/post.ftl @@ -21,7 +21,7 @@ ${post.createTime?string("yyyy-MM-dd")} - Comments + Comments <#if tags?size gt 0> <#list tags as tag> @@ -42,7 +42,7 @@ class="fa fa-weibo">
@@ -50,12 +50,12 @@ diff --git a/src/main/resources/templates/themes/anatole/sheet.ftl b/src/main/resources/templates/themes/anatole/sheet.ftl index c6c5b6c29..38fc535ee 100644 --- a/src/main/resources/templates/themes/anatole/sheet.ftl +++ b/src/main/resources/templates/themes/anatole/sheet.ftl @@ -21,7 +21,7 @@ ${sheet.createTime?string("yyyy-MM-dd")} - Comments + Comments @@ -36,7 +36,7 @@ class="fa fa-weibo">