Refactor TagService

pull/98/head
johnniang 2019-02-20 11:26:27 +08:00
parent 6687db022c
commit e0ef35b0dd
6 changed files with 21 additions and 72 deletions

View File

@ -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()));

View File

@ -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> {
/**
*

View File

@ -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;

View File

@ -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)) {

View File

@ -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";
}

View File

@ -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 {