From 683444b66feedea14c1dc730bc9f3091c0b75ffc Mon Sep 17 00:00:00 2001 From: ruibaby Date: Mon, 29 Apr 2019 15:33:18 +0800 Subject: [PATCH] Refactor freemarker directive. --- .../model/freemarker/tag/CategoryTagDirective.java | 10 ++++++++-- .../app/model/freemarker/tag/TagTagDirective.java | 14 +++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java index af26e755b..b8ee1f22f 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java @@ -5,6 +5,7 @@ import freemarker.template.*; import org.springframework.stereotype.Component; import run.halo.app.model.support.HaloConst; import run.halo.app.service.CategoryService; +import run.halo.app.service.PostCategoryService; import java.io.IOException; import java.util.Map; @@ -20,9 +21,11 @@ public class CategoryTagDirective implements TemplateDirectiveModel { private final CategoryService categoryService; - public CategoryTagDirective(Configuration configuration, CategoryService categoryService) { - this.categoryService = categoryService; + private final PostCategoryService postCategoryService; + public CategoryTagDirective(Configuration configuration, CategoryService categoryService, PostCategoryService postCategoryService) { + this.categoryService = categoryService; + this.postCategoryService = postCategoryService; configuration.setSharedVariable("categoryTag", this); } @@ -32,10 +35,13 @@ public class CategoryTagDirective implements TemplateDirectiveModel { if (params.containsKey(HaloConst.METHOD_KEY)) { String method = params.get(HaloConst.METHOD_KEY).toString(); + Integer postId = Integer.parseInt(params.get("postId").toString()); switch (method) { case "list": env.setVariable("categories", builder.build().wrap(categoryService.listAll())); break; + case "listByPostId": + env.setVariable("categories", builder.build().wrap(postCategoryService.listCategoryBy(postId))); case "count": env.setVariable("count", builder.build().wrap(categoryService.count())); break; diff --git a/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java index ca2da570b..79c0ed825 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java @@ -1,10 +1,11 @@ package run.halo.app.model.freemarker.tag; -import run.halo.app.model.support.HaloConst; -import run.halo.app.service.TagService; import freemarker.core.Environment; import freemarker.template.*; import org.springframework.stereotype.Component; +import run.halo.app.model.support.HaloConst; +import run.halo.app.service.PostTagService; +import run.halo.app.service.TagService; import java.io.IOException; import java.util.Map; @@ -20,8 +21,11 @@ public class TagTagDirective implements TemplateDirectiveModel { private final TagService tagService; - public TagTagDirective(Configuration configuration, TagService tagService) { + private final PostTagService postTagService; + + public TagTagDirective(Configuration configuration, TagService tagService, PostTagService postTagService) { this.tagService = tagService; + this.postTagService = postTagService; configuration.setSharedVariable("tagTag", this); } @@ -31,10 +35,14 @@ public class TagTagDirective implements TemplateDirectiveModel { if (params.containsKey(HaloConst.METHOD_KEY)) { String method = params.get(HaloConst.METHOD_KEY).toString(); + Integer postId = Integer.parseInt(params.get("postId").toString()); switch (method) { case "list": env.setVariable("tags", builder.build().wrap(tagService.listAll())); break; + case "listByPostId": + env.setVariable("tags", builder.build().wrap(postTagService.listTagsBy(postId))); + break; case "count": env.setVariable("count", builder.build().wrap(tagService.count())); break;