feat: 文章固定链接类型增加年型 #1026 (#1095)

pull/1097/head
Wh1te 2020-10-01 10:29:20 +08:00 committed by GitHub
parent e58d8fbe73
commit a4da9a7d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package run.halo.app.controller.content;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@ -127,6 +128,9 @@ public class ContentContentController {
if (postPermalinkType.equals(PostPermalinkType.DEFAULT) && optionService.getArchivesPrefix().equals(prefix)) {
Post post = postService.getBySlug(slug);
return postModel.content(post, token, model);
} else if (postPermalinkType.equals(PostPermalinkType.YEAR) && prefix.length() == 4 && StringUtils.isNumeric(prefix)) {
Post post = postService.getBy(Integer.parseInt(prefix), slug);
return postModel.content(post, token, model);
} else if (optionService.getSheetPrefix().equals(prefix)) {
Sheet sheet = sheetService.getBySlug(slug);
return sheetModel.content(sheet, token, model);

View File

@ -26,7 +26,12 @@ public enum PostPermalinkType implements ValueEnum<Integer> {
/**
* /?p=${id}
*/
ID(3);
ID(3),
/**
* /1970/${slug}
*/
YEAR(4);
private final Integer value;

View File

@ -48,6 +48,17 @@ public interface PostRepository extends BasePostRepository<Post>, JpaSpecificati
@Query("select post from Post post where year(post.createTime) = :year and month(post.createTime) = :month and post.slug = :slug")
Optional<Post> findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("slug") String slug);
/**
* Find by post year and slug.
*
* @param year post create year
* @param slug post slug
* @return a optional of post
*/
@Query("select post from Post post where year(post.createTime) = :year and post.slug = :slug")
Optional<Post> findBy(@Param("year") Integer year, @Param("slug") String slug);
/**
* Find by post year and month and slug and status.
*

View File

@ -109,6 +109,16 @@ public interface PostService extends BasePostService<Post> {
@NonNull
Post getBy(@NonNull Integer year, @NonNull Integer month, @NonNull String slug);
/**
* Gets post by post year and slug.
*
* @param year post create year.
* @param slug post slug.
* @return post info
*/
@NonNull
Post getBy(@NonNull Integer year, @NonNull String slug);
/**
* Gets post by post year and month and slug.
*

View File

@ -153,6 +153,11 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
.append(URL_SEPARATOR)
.append(post.getSlug())
.append(pathSuffix);
} else if (permalinkType.equals(PostPermalinkType.YEAR)) {
fullPath.append(DateUtil.year(post.getCreateTime()))
.append(URL_SEPARATOR)
.append(post.getSlug())
.append(pathSuffix);
}
post.setFullPath(fullPath.toString());

View File

@ -185,6 +185,18 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(slug));
}
@NonNull
@Override
public Post getBy(@NonNull Integer year, @NonNull String slug) {
Assert.notNull(year, "Post create year must not be null");
Assert.notNull(slug, "Post slug must not be null");
Optional<Post> postOptional = postRepository.findBy(year, slug);
return postOptional
.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(slug));
}
@Override
public Post getBy(Integer year, Integer month, String slug, PostStatus status) {
Assert.notNull(year, "Post create year must not be null");
@ -855,6 +867,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
.append(URL_SEPARATOR)
.append(post.getSlug())
.append(pathSuffix);
} else if (permalinkType.equals(PostPermalinkType.YEAR)) {
fullPath.append(DateUtil.year(post.getCreateTime()))
.append(URL_SEPARATOR)
.append(post.getSlug())
.append(pathSuffix);
}
return fullPath.toString();
}