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())); environment.setVariable("categories", builder.build().wrap(categoryService.listAll()));
break; break;
case "tags": case "tags":
environment.setVariable("tags", builder.build().wrap(tagService.findAll())); environment.setVariable("tags", builder.build().wrap(tagService.listAll()));
break; break;
case "links": case "links":
environment.setVariable("links", builder.build().wrap(linkService.listAll())); environment.setVariable("links", builder.build().wrap(linkService.listAll()));

View File

@ -1,9 +1,9 @@
package cc.ryanc.halo.service; package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Tag; import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.service.base.CrudService;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* <pre> * <pre>
@ -13,38 +13,7 @@ import java.util.Optional;
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2018/1/12 * @date : 2018/1/12
*/ */
public interface TagService { public interface TagService extends CrudService<Tag, Long> {
/**
* /
*
* @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);
/** /**
* *

View File

@ -3,13 +3,12 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.Tag; import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.repository.TagRepository; import cc.ryanc.halo.repository.TagRepository;
import cc.ryanc.halo.service.TagService; 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.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* <pre> * <pre>
@ -20,12 +19,16 @@ import java.util.Optional;
* @date : 2018/1/12 * @date : 2018/1/12
*/ */
@Service @Service
public class TagServiceImpl implements TagService { public class TagServiceImpl extends AbstractCrudService<Tag, Long> implements TagService {
private static final String POSTS_CACHE_NAME = "posts"; private static final String POSTS_CACHE_NAME = "posts";
@Autowired private final TagRepository tagRepository;
private TagRepository tagRepository;
public TagServiceImpl(TagRepository tagRepository) {
super(tagRepository);
this.tagRepository = tagRepository;
}
/** /**
* / * /
@ -35,8 +38,8 @@ public class TagServiceImpl implements TagService {
*/ */
@Override @Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Tag save(Tag tag) { public Tag create(Tag tag) {
return tagRepository.save(tag); return super.create(tag);
} }
/** /**
@ -47,31 +50,8 @@ public class TagServiceImpl implements TagService {
*/ */
@Override @Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Tag remove(Long tagId) { public Tag removeById(Long tagId) {
final Optional<Tag> tag = findByTagId(tagId); return super.removeById(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);
} }
/** /**
@ -115,7 +95,7 @@ public class TagServiceImpl implements TagService {
nt = new Tag(); nt = new Tag();
nt.setTagName(tag); nt.setTagName(tag);
nt.setTagUrl(tag); nt.setTagUrl(tag);
tagsList.add(save(nt)); tagsList.add(create(nt));
} }
} }
return tagsList; return tagsList;

View File

@ -306,7 +306,7 @@ public class AdminController extends BaseController {
tag = new Tag(); tag = new Tag();
tag.setTagName(ele); tag.setTagName(ele);
tag.setTagUrl(ele); tag.setTagUrl(ele);
tag = tagService.save(tag); tag = tagService.create(tag);
} }
tags.add(tag); tags.add(tag);
} else if ("categories".equals(key)) { } 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")); 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) { if (null == tag) {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed")); return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
} }
@ -84,7 +84,7 @@ public class TagController {
@GetMapping(value = "/remove") @GetMapping(value = "/remove")
public String removeTag(@RequestParam("tagId") Long tagId) { public String removeTag(@RequestParam("tagId") Long tagId) {
try { try {
tagService.remove(tagId); tagService.removeById(tagId);
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to delete tag: {}", e.getMessage()); log.error("Failed to delete tag: {}", e.getMessage());
} }
@ -100,7 +100,7 @@ public class TagController {
*/ */
@GetMapping(value = "/edit") @GetMapping(value = "/edit")
public String toEditTag(Model model, @RequestParam("tagId") Long tagId) { 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); model.addAttribute("updateTag", tag);
return "admin/admin_tag"; return "admin/admin_tag";
} }

View File

@ -49,7 +49,7 @@ public class ApiTagController {
*/ */
@GetMapping @GetMapping
public JsonResult tags() { public JsonResult tags() {
final List<Tag> tags = tagService.findAll(); final List<Tag> tags = tagService.listAll();
if (null != tags && tags.size() > 0) { if (null != tags && tags.size() > 0) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), tags); return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), tags);
} else { } else {