mirror of https://github.com/halo-dev/halo
Refactor TagService
parent
6687db022c
commit
e0ef35b0dd
|
@ -50,7 +50,7 @@ public class CommonTagDirective implements TemplateDirectiveModel {
|
|||
environment.setVariable("categories", builder.build().wrap(categoryService.listAll()));
|
||||
break;
|
||||
case "tags":
|
||||
environment.setVariable("tags", builder.build().wrap(tagService.findAll()));
|
||||
environment.setVariable("tags", builder.build().wrap(tagService.listAll()));
|
||||
break;
|
||||
case "links":
|
||||
environment.setVariable("links", builder.build().wrap(linkService.listAll()));
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Tag;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -13,38 +13,7 @@ import java.util.Optional;
|
|||
* @author : RYAN0UP
|
||||
* @date : 2018/1/12
|
||||
*/
|
||||
public interface TagService {
|
||||
|
||||
/**
|
||||
* 新增/修改标签
|
||||
*
|
||||
* @param tag tag
|
||||
* @return Tag
|
||||
*/
|
||||
Tag save(Tag tag);
|
||||
|
||||
/**
|
||||
* 根据编号移除标签
|
||||
*
|
||||
* @param tagId tagId
|
||||
* @return Tag
|
||||
*/
|
||||
Tag remove(Long tagId);
|
||||
|
||||
/**
|
||||
* 获取所有标签
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
List<Tag> findAll();
|
||||
|
||||
/**
|
||||
* 根据编号查询标签
|
||||
*
|
||||
* @param tagId tagId
|
||||
* @return Optional
|
||||
*/
|
||||
Optional<Tag> findByTagId(Long tagId);
|
||||
public interface TagService extends CrudService<Tag, Long> {
|
||||
|
||||
/**
|
||||
* 根据标签路径查询
|
||||
|
|
|
@ -3,13 +3,12 @@ package cc.ryanc.halo.service.impl;
|
|||
import cc.ryanc.halo.model.domain.Tag;
|
||||
import cc.ryanc.halo.repository.TagRepository;
|
||||
import cc.ryanc.halo.service.TagService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -20,12 +19,16 @@ import java.util.Optional;
|
|||
* @date : 2018/1/12
|
||||
*/
|
||||
@Service
|
||||
public class TagServiceImpl implements TagService {
|
||||
public class TagServiceImpl extends AbstractCrudService<Tag, Long> implements TagService {
|
||||
|
||||
private static final String POSTS_CACHE_NAME = "posts";
|
||||
|
||||
@Autowired
|
||||
private TagRepository tagRepository;
|
||||
private final TagRepository tagRepository;
|
||||
|
||||
public TagServiceImpl(TagRepository tagRepository) {
|
||||
super(tagRepository);
|
||||
this.tagRepository = tagRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增/修改标签
|
||||
|
@ -35,8 +38,8 @@ public class TagServiceImpl implements TagService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Tag save(Tag tag) {
|
||||
return tagRepository.save(tag);
|
||||
public Tag create(Tag tag) {
|
||||
return super.create(tag);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,31 +50,8 @@ public class TagServiceImpl implements TagService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Tag remove(Long tagId) {
|
||||
final Optional<Tag> tag = findByTagId(tagId);
|
||||
tagRepository.delete(tag.get());
|
||||
return tag.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有标签
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
@Override
|
||||
public List<Tag> findAll() {
|
||||
return tagRepository.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据编号查询标签
|
||||
*
|
||||
* @param tagId tagId
|
||||
* @return Optional
|
||||
*/
|
||||
@Override
|
||||
public Optional<Tag> findByTagId(Long tagId) {
|
||||
return tagRepository.findById(tagId);
|
||||
public Tag removeById(Long tagId) {
|
||||
return super.removeById(tagId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,7 +95,7 @@ public class TagServiceImpl implements TagService {
|
|||
nt = new Tag();
|
||||
nt.setTagName(tag);
|
||||
nt.setTagUrl(tag);
|
||||
tagsList.add(save(nt));
|
||||
tagsList.add(create(nt));
|
||||
}
|
||||
}
|
||||
return tagsList;
|
||||
|
|
|
@ -306,7 +306,7 @@ public class AdminController extends BaseController {
|
|||
tag = new Tag();
|
||||
tag.setTagName(ele);
|
||||
tag.setTagUrl(ele);
|
||||
tag = tagService.save(tag);
|
||||
tag = tagService.create(tag);
|
||||
}
|
||||
tags.add(tag);
|
||||
} else if ("categories".equals(key)) {
|
||||
|
|
|
@ -68,7 +68,7 @@ public class TagController {
|
|||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.url-is-exists"));
|
||||
}
|
||||
}
|
||||
tag = tagService.save(tag);
|
||||
tag = tagService.create(tag);
|
||||
if (null == tag) {
|
||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ public class TagController {
|
|||
@GetMapping(value = "/remove")
|
||||
public String removeTag(@RequestParam("tagId") Long tagId) {
|
||||
try {
|
||||
tagService.remove(tagId);
|
||||
tagService.removeById(tagId);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to delete tag: {}", e.getMessage());
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ public class TagController {
|
|||
*/
|
||||
@GetMapping(value = "/edit")
|
||||
public String toEditTag(Model model, @RequestParam("tagId") Long tagId) {
|
||||
final Tag tag = tagService.findByTagId(tagId).orElse(new Tag());
|
||||
final Tag tag = tagService.fetchById(tagId).orElse(new Tag());
|
||||
model.addAttribute("updateTag", tag);
|
||||
return "admin/admin_tag";
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class ApiTagController {
|
|||
*/
|
||||
@GetMapping
|
||||
public JsonResult tags() {
|
||||
final List<Tag> tags = tagService.findAll();
|
||||
final List<Tag> tags = tagService.listAll();
|
||||
if (null != tags && tags.size() > 0) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), tags);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue