Refactor freemarker directive.

pull/146/head
ruibaby 2019-04-29 15:33:18 +08:00
parent 0873c59b71
commit 683444b66f
2 changed files with 19 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import freemarker.template.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.HaloConst;
import run.halo.app.service.CategoryService; import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -20,9 +21,11 @@ public class CategoryTagDirective implements TemplateDirectiveModel {
private final CategoryService categoryService; private final CategoryService categoryService;
public CategoryTagDirective(Configuration configuration, CategoryService categoryService) { private final PostCategoryService postCategoryService;
this.categoryService = categoryService;
public CategoryTagDirective(Configuration configuration, CategoryService categoryService, PostCategoryService postCategoryService) {
this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
configuration.setSharedVariable("categoryTag", this); configuration.setSharedVariable("categoryTag", this);
} }
@ -32,10 +35,13 @@ public class CategoryTagDirective implements TemplateDirectiveModel {
if (params.containsKey(HaloConst.METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(HaloConst.METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
Integer postId = Integer.parseInt(params.get("postId").toString());
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("categories", builder.build().wrap(categoryService.listAll())); env.setVariable("categories", builder.build().wrap(categoryService.listAll()));
break; break;
case "listByPostId":
env.setVariable("categories", builder.build().wrap(postCategoryService.listCategoryBy(postId)));
case "count": case "count":
env.setVariable("count", builder.build().wrap(categoryService.count())); env.setVariable("count", builder.build().wrap(categoryService.count()));
break; break;

View File

@ -1,10 +1,11 @@
package run.halo.app.model.freemarker.tag; 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.core.Environment;
import freemarker.template.*; import freemarker.template.*;
import org.springframework.stereotype.Component; 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.io.IOException;
import java.util.Map; import java.util.Map;
@ -20,8 +21,11 @@ public class TagTagDirective implements TemplateDirectiveModel {
private final TagService tagService; 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.tagService = tagService;
this.postTagService = postTagService;
configuration.setSharedVariable("tagTag", this); configuration.setSharedVariable("tagTag", this);
} }
@ -31,10 +35,14 @@ public class TagTagDirective implements TemplateDirectiveModel {
if (params.containsKey(HaloConst.METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(HaloConst.METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
Integer postId = Integer.parseInt(params.get("postId").toString());
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("tags", builder.build().wrap(tagService.listAll())); env.setVariable("tags", builder.build().wrap(tagService.listAll()));
break; break;
case "listByPostId":
env.setVariable("tags", builder.build().wrap(postTagService.listTagsBy(postId)));
break;
case "count": case "count":
env.setVariable("count", builder.build().wrap(tagService.count())); env.setVariable("count", builder.build().wrap(tagService.count()));
break; break;