Refactor MenuService

pull/98/head
johnniang 2019-02-20 10:26:53 +08:00
parent 4c8b6f24cd
commit 8387f18c7a
6 changed files with 23 additions and 60 deletions

View File

@ -44,7 +44,7 @@ public class CommonTagDirective implements TemplateDirectiveModel {
String method = map.get(METHOD_KEY).toString(); String method = map.get(METHOD_KEY).toString();
switch (method) { switch (method) {
case "menus": case "menus":
environment.setVariable("menus", builder.build().wrap(menuService.findAll())); environment.setVariable("menus", builder.build().wrap(menuService.listAll()));
break; break;
case "categories": case "categories":
environment.setVariable("categories", builder.build().wrap(categoryService.listAll())); environment.setVariable("categories", builder.build().wrap(categoryService.listAll()));

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service; package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Menu; import cc.ryanc.halo.model.domain.Menu;
import cc.ryanc.halo.service.base.CrudService;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -13,36 +14,6 @@ import java.util.Optional;
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2018/1/24 * @date : 2018/1/24
*/ */
public interface MenuService { public interface MenuService extends CrudService<Menu, Long> {
/**
* /
*
* @param menu menu
* @return Menu
*/
Menu save(Menu menu);
/**
*
*
* @return List
*/
List<Menu> findAll();
/**
*
*
* @param menuId menuId
* @return Menu
*/
Menu remove(Long menuId);
/**
*
*
* @param menuId menuId
* @return Optional
*/
Optional<Menu> findByMenuId(Long menuId);
} }

View File

@ -3,7 +3,7 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.Menu; import cc.ryanc.halo.model.domain.Menu;
import cc.ryanc.halo.repository.MenuRepository; import cc.ryanc.halo.repository.MenuRepository;
import cc.ryanc.halo.service.MenuService; import cc.ryanc.halo.service.MenuService;
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.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -20,14 +20,18 @@ import java.util.Optional;
* @date : 2018/1/24 * @date : 2018/1/24
*/ */
@Service @Service
public class MenuServiceImpl implements MenuService { public class MenuServiceImpl extends AbstractCrudService<Menu, Long> implements MenuService {
private static final String MENUS_CACHE_KEY = "'menu'"; private static final String MENUS_CACHE_KEY = "'menu'";
private static final String MENUS_CACHE_NAME = "menus"; private static final String MENUS_CACHE_NAME = "menus";
@Autowired private final MenuRepository menuRepository;
private MenuRepository menuRepository;
public MenuServiceImpl(MenuRepository menuRepository) {
super(menuRepository);
this.menuRepository = menuRepository;
}
/** /**
* *
@ -36,8 +40,8 @@ public class MenuServiceImpl implements MenuService {
*/ */
@Override @Override
@Cacheable(value = MENUS_CACHE_NAME, key = MENUS_CACHE_KEY) @Cacheable(value = MENUS_CACHE_NAME, key = MENUS_CACHE_KEY)
public List<Menu> findAll() { public List<Menu> listAll() {
return menuRepository.findAll(); return super.listAll();
} }
/** /**
@ -48,8 +52,8 @@ public class MenuServiceImpl implements MenuService {
*/ */
@Override @Override
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Menu save(Menu menu) { public Menu create(Menu menu) {
return menuRepository.save(menu); return super.create(menu);
} }
/** /**
@ -60,20 +64,8 @@ public class MenuServiceImpl implements MenuService {
*/ */
@Override @Override
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Menu remove(Long menuId) { public Menu removeById(Long menuId) {
final Optional<Menu> menu = this.findByMenuId(menuId); return super.removeById(menuId);
menuRepository.delete(menu.orElse(null));
return menu.orElse(null);
} }
/**
*
*
* @param menuId menuId
* @return Menu
*/
@Override
public Optional<Menu> findByMenuId(Long menuId) {
return menuRepository.findById(menuId);
}
} }

View File

@ -54,7 +54,7 @@ public class MenuController {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage()); return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
} }
} }
menu = menuService.save(menu); menu = menuService.create(menu);
if (null != menu) { if (null != menu) {
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), "菜单保存成功!"); return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), "菜单保存成功!");
} else { } else {
@ -71,7 +71,7 @@ public class MenuController {
*/ */
@GetMapping(value = "/edit") @GetMapping(value = "/edit")
public String updateMenu(@RequestParam("menuId") Long menuId, Model model) { public String updateMenu(@RequestParam("menuId") Long menuId, Model model) {
final Menu menu = menuService.findByMenuId(menuId).orElse(new Menu()); final Menu menu = menuService.fetchById(menuId).orElse(new Menu());
model.addAttribute("updateMenu", menu); model.addAttribute("updateMenu", menu);
return "/admin/admin_menu"; return "/admin/admin_menu";
} }
@ -85,7 +85,7 @@ public class MenuController {
@GetMapping(value = "/remove") @GetMapping(value = "/remove")
public String removeMenu(@RequestParam("menuId") Long menuId) { public String removeMenu(@RequestParam("menuId") Long menuId) {
try { try {
menuService.remove(menuId); menuService.removeById(menuId);
} catch (Exception e) { } catch (Exception e) {
log.error("Deleting menu failed: {}", e.getMessage()); log.error("Deleting menu failed: {}", e.getMessage());
} }

View File

@ -55,7 +55,7 @@ public class ApiMenuController {
*/ */
@GetMapping @GetMapping
public JsonResult menus() { public JsonResult menus() {
final List<Menu> menus = menuService.findAll(); final List<Menu> menus = menuService.listAll();
if (null != menus && menus.size() > 0) { if (null != menus && menus.size() > 0) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), menus); return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), menus);
} else { } else {

View File

@ -178,14 +178,14 @@ public class InstallController {
menuIndex.setMenuUrl("/"); menuIndex.setMenuUrl("/");
menuIndex.setMenuSort(1); menuIndex.setMenuSort(1);
menuIndex.setMenuIcon(" "); menuIndex.setMenuIcon(" ");
menuService.save(menuIndex); menuService.create(menuIndex);
final Menu menuArchive = new Menu(); final Menu menuArchive = new Menu();
menuArchive.setMenuName("归档"); menuArchive.setMenuName("归档");
menuArchive.setMenuUrl("/archives"); menuArchive.setMenuUrl("/archives");
menuArchive.setMenuSort(2); menuArchive.setMenuSort(2);
menuArchive.setMenuIcon(" "); menuArchive.setMenuIcon(" ");
menuService.save(menuArchive); menuService.create(menuArchive);
OPTIONS.clear(); OPTIONS.clear();
OPTIONS = optionsService.findAllOptions(); OPTIONS = optionsService.findAllOptions();