diff --git a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java index b317d36ca..0a21e3084 100644 --- a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java +++ b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java @@ -3,10 +3,8 @@ package cc.ryanc.halo.config; import cc.ryanc.halo.model.freemarker.method.RandomMethod; import cc.ryanc.halo.model.freemarker.method.RecentCommentsMethod; import cc.ryanc.halo.model.freemarker.method.RecentPostsMethod; -import cc.ryanc.halo.model.freemarker.tag.ArticleTagDirective; -import cc.ryanc.halo.model.freemarker.tag.CommonTagDirective; +import cc.ryanc.halo.model.freemarker.tag.*; import cc.ryanc.halo.service.OptionService; -import cc.ryanc.halo.service.UserService; import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -33,13 +31,22 @@ public class FreeMarkerAutoConfiguration { private OptionService optionsService; @Autowired - private UserService userService; + private PostTagDirective postTagDirective; @Autowired - private CommonTagDirective commonTagDirective; + private CategoryTagDirective categoryTagDirective; @Autowired - private ArticleTagDirective articleTagDirective; + private CommentTagDirective commentTagDirective; + + @Autowired + private LinkTagDirective linkTagDirective; + + @Autowired + private MenuTagDirective menuTagDirective; + + @Autowired + private TagTagDirective tagTagDirective; @Autowired private RandomMethod randomMethod; @@ -53,11 +60,14 @@ public class FreeMarkerAutoConfiguration { @PostConstruct public void setSharedVariable() { try { - //自定义标签 - configuration.setSharedVariable("commonTag", commonTagDirective); - configuration.setSharedVariable("articleTag", articleTagDirective); configuration.setSharedVariable("options", optionsService.listOptions()); -// configuration.setSharedVariable("user", userService.findUser()); + //自定义标签 + configuration.setSharedVariable("categoryTag", categoryTagDirective); + configuration.setSharedVariable("commentTag", commentTagDirective); + configuration.setSharedVariable("linkTag", linkTagDirective); + configuration.setSharedVariable("menuTag", menuTagDirective); + configuration.setSharedVariable("tagTag", tagTagDirective); + configuration.setSharedVariable("postTag", postTagDirective); configuration.setSharedVariable("randomMethod", randomMethod); configuration.setSharedVariable("recentPostsMethod", recentPostsMethod); configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod); diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CategoryTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CategoryTagDirective.java new file mode 100644 index 000000000..88bab1b8f --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CategoryTagDirective.java @@ -0,0 +1,44 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.CategoryService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class CategoryTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final CategoryService categoryService; + + public CategoryTagDirective(CategoryService categoryService) { + this.categoryService = categoryService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("categories", builder.build().wrap(categoryService.listAll())); + break; + case "count": + env.setVariable("count", builder.build().wrap(categoryService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommentTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommentTagDirective.java new file mode 100644 index 000000000..b64371c35 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommentTagDirective.java @@ -0,0 +1,43 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.CommentService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class CommentTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final CommentService commentService; + + public CommentTagDirective(CommentService commentService) { + this.commentService = commentService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + int top = Integer.parseInt(params.get("top").toString()); + switch (method) { + case "latest": + env.setVariable("categories", builder.build().wrap(builder.build().wrap(builder.build().wrap(commentService.pageLatest(top))))); + break; + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java deleted file mode 100644 index 143e2594f..000000000 --- a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java +++ /dev/null @@ -1,75 +0,0 @@ -package cc.ryanc.halo.model.freemarker.tag; - -import cc.ryanc.halo.service.*; -import freemarker.core.Environment; -import freemarker.template.*; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.util.Map; - -/** - *
- *     FreeMarker自定义标签
- * 
- * - * @author : RYAN0UP - * @date : 2018/4/26 - */ -@Component -public class CommonTagDirective implements TemplateDirectiveModel { - - private static final String METHOD_KEY = "method"; - - private final MenuService menuService; - - private final CategoryService categoryService; - - private final TagService tagService; - - private final LinkService linkService; - - private final CommentService commentService; - - public CommonTagDirective(MenuService menuService, - CategoryService categoryService, - TagService tagService, - LinkService linkService, - CommentService commentService) { - this.menuService = menuService; - this.categoryService = categoryService; - this.tagService = tagService; - this.linkService = linkService; - this.commentService = commentService; - } - - @Override - public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { - final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); - - if (params.containsKey(METHOD_KEY)) { - String method = params.get(METHOD_KEY).toString(); - switch (method) { - case "menus": - env.setVariable("menus", builder.build().wrap(menuService.listAll())); - break; - case "categories": - env.setVariable("categories", builder.build().wrap(categoryService.listAll())); - break; - case "tags": - env.setVariable("tags", builder.build().wrap(tagService.listAll())); - break; - case "links": - env.setVariable("links", builder.build().wrap(linkService.listAll())); - break; - case "newComments": - env.setVariable("newComments", builder.build().wrap(commentService.listAll())); - break; - default: - break; - } - } - body.render(env.getOut()); - } - -} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/LinkTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/LinkTagDirective.java new file mode 100644 index 000000000..d5ad108d8 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/LinkTagDirective.java @@ -0,0 +1,47 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.LinkService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class LinkTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final LinkService linkService; + + public LinkTagDirective(LinkService linkService) { + this.linkService = linkService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("links", builder.build().wrap(linkService.listAll())); + break; + case "listTeam": + env.setVariable("links", builder.build().wrap(null)); + break; + case "count": + env.setVariable("count", builder.build().wrap(linkService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/MenuTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/MenuTagDirective.java new file mode 100644 index 000000000..8b179c25c --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/MenuTagDirective.java @@ -0,0 +1,44 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.MenuService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class MenuTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final MenuService menuService; + + public MenuTagDirective(MenuService menuService) { + this.menuService = menuService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("menus", builder.build().wrap(menuService.listAll())); + break; + case "count": + env.setVariable("count", builder.build().wrap(menuService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/ArticleTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/PostTagDirective.java similarity index 91% rename from src/main/java/cc/ryanc/halo/model/freemarker/tag/ArticleTagDirective.java rename to src/main/java/cc/ryanc/halo/model/freemarker/tag/PostTagDirective.java index 3945e4792..a9f1f368a 100644 --- a/src/main/java/cc/ryanc/halo/model/freemarker/tag/ArticleTagDirective.java +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/PostTagDirective.java @@ -19,7 +19,7 @@ import java.util.Map; * @date : 2018/4/26 */ @Component -public class ArticleTagDirective implements TemplateDirectiveModel { +public class PostTagDirective implements TemplateDirectiveModel { private static final String METHOD_KEY = "method"; diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/TagTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/TagTagDirective.java new file mode 100644 index 000000000..12d847b1b --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/TagTagDirective.java @@ -0,0 +1,44 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.TagService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class TagTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final TagService tagService; + + public TagTagDirective(TagService tagService) { + this.tagService = tagService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("tags", builder.build().wrap(tagService.listAll())); + break; + case "count": + env.setVariable("count", builder.build().wrap(tagService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/support/HaloConst.java b/src/main/java/cc/ryanc/halo/model/support/HaloConst.java index 0196a336f..968e91c7e 100644 --- a/src/main/java/cc/ryanc/halo/model/support/HaloConst.java +++ b/src/main/java/cc/ryanc/halo/model/support/HaloConst.java @@ -36,7 +36,7 @@ public class HaloConst { /** * All of the options */ - public static Map OPTIONS = new HashMap<>(); + public static Map OPTIONS; /** * All of the Owo @@ -46,7 +46,7 @@ public class HaloConst { /** * All of the themes */ - public static List THEMES = new ArrayList<>(); + public static List THEMES; /** * user_session @@ -56,7 +56,7 @@ public class HaloConst { /** * 文章阅读数缓存 */ - public static Map POSTS_VIEWS = new HashMap<>(); + public static Map POSTS_VIEWS; /** * 网易云音乐短代码前缀 diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/CategoryController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/CategoryController.java index 423cd9fba..4f6fc1f31 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/CategoryController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/CategoryController.java @@ -5,6 +5,7 @@ import cc.ryanc.halo.model.entity.Category; import cc.ryanc.halo.model.params.CategoryParam; import cc.ryanc.halo.model.vo.CategoryVO; import cc.ryanc.halo.service.CategoryService; +import cc.ryanc.halo.service.PostCategoryService; import io.swagger.annotations.ApiOperation; import org.springframework.data.domain.Sort; import org.springframework.data.web.SortDefault; @@ -27,8 +28,11 @@ public class CategoryController { private final CategoryService categoryService; - public CategoryController(CategoryService categoryService) { + private final PostCategoryService postCategoryService; + + public CategoryController(CategoryService categoryService, PostCategoryService postCategoryService) { this.categoryService = categoryService; + this.postCategoryService = postCategoryService; } @GetMapping("tree") @@ -67,5 +71,6 @@ public class CategoryController { @ApiOperation("Delete category by id") public void deletePermanently(@PathVariable("id") Integer id) { categoryService.removeById(id); + postCategoryService.removeByCategoryId(id); } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java index eeaf212b7..1781d2c02 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java @@ -7,7 +7,9 @@ import cc.ryanc.halo.model.enums.PostStatus; import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.params.PostParam; import cc.ryanc.halo.model.vo.PostDetailVO; +import cc.ryanc.halo.service.PostCategoryService; import cc.ryanc.halo.service.PostService; +import cc.ryanc.halo.service.PostTagService; import io.swagger.annotations.ApiOperation; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -31,8 +33,14 @@ public class PostController { private final PostService postService; - public PostController(PostService postService) { + private final PostCategoryService postCategoryService; + + private final PostTagService postTagService; + + public PostController(PostService postService, PostCategoryService postCategoryService, PostTagService postTagService) { this.postService = postService; + this.postCategoryService = postCategoryService; + this.postTagService = postTagService; } @GetMapping("latest") @@ -80,5 +88,7 @@ public class PostController { public void deletePermanently(@PathVariable("postId") Integer postId) { // Remove it postService.removeById(postId); + postCategoryService.removeByPostId(postId); + postTagService.removeByPostId(postId); } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/TagController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/TagController.java index e2f8d3b08..7f3a7f49d 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/TagController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/TagController.java @@ -78,5 +78,6 @@ public class TagController { @ApiOperation("Delete tag by id") public void deletePermanently(@PathVariable("id") Integer id) { tagService.removeById(id); + postTagService.removeByTagId(id); } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/content/ContentTagController.java b/src/main/java/cc/ryanc/halo/web/controller/content/ContentTagController.java index a3aba0538..5915c705b 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/content/ContentTagController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/content/ContentTagController.java @@ -5,6 +5,7 @@ import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.vo.PostListVO; import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.TagService; +import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cn.hutool.core.util.StrUtil; import org.springframework.data.domain.Page; @@ -80,10 +81,7 @@ public class ContentTagController extends BaseContentController { if (null == tag) { return this.renderNotFound(); } - int size = 10; - if (StrUtil.isNotBlank(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()))) { - size = Integer.parseInt(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue())); - } + int size = HaloUtils.getDefaultPageSize(); final Pageable pageable = PageRequest.of(page - 1, size, sort); // TODO get posts by tag