mirror of https://github.com/halo-dev/halo
parent
e58d8fbe73
commit
a4da9a7d58
|
@ -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);
|
||||
|
|
|
@ -26,7 +26,12 @@ public enum PostPermalinkType implements ValueEnum<Integer> {
|
|||
/**
|
||||
* /?p=${id}
|
||||
*/
|
||||
ID(3);
|
||||
ID(3),
|
||||
|
||||
/**
|
||||
* /1970/${slug}
|
||||
*/
|
||||
YEAR(4);
|
||||
|
||||
private final Integer value;
|
||||
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue