mirror of https://github.com/halo-dev/halo
Refactor LinkService
parent
385306ffd9
commit
f3db672213
|
@ -53,7 +53,7 @@ public class CommonTagDirective implements TemplateDirectiveModel {
|
|||
environment.setVariable("tags", builder.build().wrap(tagService.findAll()));
|
||||
break;
|
||||
case "links":
|
||||
environment.setVariable("links", builder.build().wrap(linkService.findAll()));
|
||||
environment.setVariable("links", builder.build().wrap(linkService.listAll()));
|
||||
break;
|
||||
case "newComments":
|
||||
environment.setVariable("newComments", builder.build().wrap(commentService.findAll(1)));
|
||||
|
|
|
@ -13,7 +13,6 @@ import javax.persistence.TypedQuery;
|
|||
import javax.persistence.criteria.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Link;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -13,36 +11,6 @@ import java.util.Optional;
|
|||
* @author : RYAN0UP
|
||||
* @date : 2017/11/14
|
||||
*/
|
||||
public interface LinkService {
|
||||
public interface LinkService extends CrudService<Link, Long> {
|
||||
|
||||
/**
|
||||
* 新增/修改友情链接
|
||||
*
|
||||
* @param link link
|
||||
* @return Link
|
||||
*/
|
||||
Link save(Link link);
|
||||
|
||||
/**
|
||||
* 根据编号删除
|
||||
*
|
||||
* @param linkId linkId
|
||||
* @return Link
|
||||
*/
|
||||
Link remove(Long linkId);
|
||||
|
||||
/**
|
||||
* 查询所有
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
List<Link> findAll();
|
||||
|
||||
/**
|
||||
* 根据编号查询单个链接
|
||||
*
|
||||
* @param linkId linkId
|
||||
* @return Link
|
||||
*/
|
||||
Optional<Link> findByLinkId(Long linkId);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package cc.ryanc.halo.service.impl;
|
|||
import cc.ryanc.halo.model.domain.Link;
|
||||
import cc.ryanc.halo.repository.LinkRepository;
|
||||
import cc.ryanc.halo.service.LinkService;
|
||||
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.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -20,14 +20,18 @@ import java.util.Optional;
|
|||
* @date : 2017/11/14
|
||||
*/
|
||||
@Service
|
||||
public class LinkServiceImpl implements LinkService {
|
||||
public class LinkServiceImpl extends AbstractCrudService<Link, Long> implements LinkService {
|
||||
|
||||
private static final String LINKS_CACHE_KEY = "'link'";
|
||||
|
||||
private static final String LINKS_CACHE_NAME = "links";
|
||||
|
||||
@Autowired
|
||||
private LinkRepository linkRepository;
|
||||
private final LinkRepository linkRepository;
|
||||
|
||||
public LinkServiceImpl(LinkRepository linkRepository) {
|
||||
super(linkRepository);
|
||||
this.linkRepository = linkRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增/修改友情链接
|
||||
|
@ -37,8 +41,8 @@ public class LinkServiceImpl implements LinkService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = LINKS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Link save(Link link) {
|
||||
return linkRepository.save(link);
|
||||
public Link create(Link link) {
|
||||
return super.create(link);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,10 +53,8 @@ public class LinkServiceImpl implements LinkService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = LINKS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Link remove(Long linkId) {
|
||||
final Optional<Link> link = this.findByLinkId(linkId);
|
||||
linkRepository.delete(link.get());
|
||||
return link.get();
|
||||
public Link removeById(Long linkId) {
|
||||
return super.removeById(linkId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,18 +64,8 @@ public class LinkServiceImpl implements LinkService {
|
|||
*/
|
||||
@Override
|
||||
@Cacheable(value = LINKS_CACHE_NAME, key = LINKS_CACHE_KEY)
|
||||
public List<Link> findAll() {
|
||||
return linkRepository.findAll();
|
||||
public List<Link> listAll() {
|
||||
return super.listAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据编号查询友情链接
|
||||
*
|
||||
* @param linkId linkId
|
||||
* @return Optional
|
||||
*/
|
||||
@Override
|
||||
public Optional<Link> findByLinkId(Long linkId) {
|
||||
return linkRepository.findById(linkId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ public class PageController {
|
|||
*/
|
||||
@GetMapping(value = "/links/edit")
|
||||
public String toEditLink(Model model, @RequestParam("linkId") Long linkId) {
|
||||
final Optional<Link> link = linkService.findByLinkId(linkId);
|
||||
final Optional<Link> link = linkService.fetchById(linkId);
|
||||
model.addAttribute("updateLink", link.orElse(new Link()));
|
||||
return "admin/admin_page_link";
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ public class PageController {
|
|||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
|
||||
}
|
||||
}
|
||||
link = linkService.save(link);
|
||||
link = linkService.create(link);
|
||||
if (null == link) {
|
||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ public class PageController {
|
|||
@GetMapping(value = "/links/remove")
|
||||
public String removeLink(@RequestParam("linkId") Long linkId) {
|
||||
try {
|
||||
linkService.remove(linkId);
|
||||
linkService.removeById(linkId);
|
||||
} catch (Exception e) {
|
||||
log.error("Deleting a friendship link failed: {}", e.getMessage());
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ public class ApiLinkController {
|
|||
*/
|
||||
@GetMapping
|
||||
public JsonResult links() {
|
||||
final List<Link> links = linkService.findAll();
|
||||
final List<Link> links = linkService.listAll();
|
||||
if (null != links && links.size() > 0) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), links);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue