mirror of https://github.com/halo-dev/halo
Refactor MenuService
parent
4c8b6f24cd
commit
8387f18c7a
|
@ -44,7 +44,7 @@ public class CommonTagDirective implements TemplateDirectiveModel {
|
|||
String method = map.get(METHOD_KEY).toString();
|
||||
switch (method) {
|
||||
case "menus":
|
||||
environment.setVariable("menus", builder.build().wrap(menuService.findAll()));
|
||||
environment.setVariable("menus", builder.build().wrap(menuService.listAll()));
|
||||
break;
|
||||
case "categories":
|
||||
environment.setVariable("categories", builder.build().wrap(categoryService.listAll()));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Menu;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
@ -13,36 +14,6 @@ import java.util.Optional;
|
|||
* @author : RYAN0UP
|
||||
* @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);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package cc.ryanc.halo.service.impl;
|
|||
import cc.ryanc.halo.model.domain.Menu;
|
||||
import cc.ryanc.halo.repository.MenuRepository;
|
||||
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.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -20,14 +20,18 @@ import java.util.Optional;
|
|||
* @date : 2018/1/24
|
||||
*/
|
||||
@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_NAME = "menus";
|
||||
|
||||
@Autowired
|
||||
private MenuRepository menuRepository;
|
||||
private final MenuRepository menuRepository;
|
||||
|
||||
public MenuServiceImpl(MenuRepository menuRepository) {
|
||||
super(menuRepository);
|
||||
this.menuRepository = menuRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有菜单
|
||||
|
@ -36,8 +40,8 @@ public class MenuServiceImpl implements MenuService {
|
|||
*/
|
||||
@Override
|
||||
@Cacheable(value = MENUS_CACHE_NAME, key = MENUS_CACHE_KEY)
|
||||
public List<Menu> findAll() {
|
||||
return menuRepository.findAll();
|
||||
public List<Menu> listAll() {
|
||||
return super.listAll();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,8 +52,8 @@ public class MenuServiceImpl implements MenuService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Menu save(Menu menu) {
|
||||
return menuRepository.save(menu);
|
||||
public Menu create(Menu menu) {
|
||||
return super.create(menu);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,20 +64,8 @@ public class MenuServiceImpl implements MenuService {
|
|||
*/
|
||||
@Override
|
||||
@CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
||||
public Menu remove(Long menuId) {
|
||||
final Optional<Menu> menu = this.findByMenuId(menuId);
|
||||
menuRepository.delete(menu.orElse(null));
|
||||
return menu.orElse(null);
|
||||
public Menu removeById(Long menuId) {
|
||||
return super.removeById(menuId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据编号查询菜单
|
||||
*
|
||||
* @param menuId menuId
|
||||
* @return Menu
|
||||
*/
|
||||
@Override
|
||||
public Optional<Menu> findByMenuId(Long menuId) {
|
||||
return menuRepository.findById(menuId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ public class MenuController {
|
|||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), error.getDefaultMessage());
|
||||
}
|
||||
}
|
||||
menu = menuService.save(menu);
|
||||
menu = menuService.create(menu);
|
||||
if (null != menu) {
|
||||
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), "菜单保存成功!");
|
||||
} else {
|
||||
|
@ -71,7 +71,7 @@ public class MenuController {
|
|||
*/
|
||||
@GetMapping(value = "/edit")
|
||||
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);
|
||||
return "/admin/admin_menu";
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ public class MenuController {
|
|||
@GetMapping(value = "/remove")
|
||||
public String removeMenu(@RequestParam("menuId") Long menuId) {
|
||||
try {
|
||||
menuService.remove(menuId);
|
||||
menuService.removeById(menuId);
|
||||
} catch (Exception e) {
|
||||
log.error("Deleting menu failed: {}", e.getMessage());
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ public class ApiMenuController {
|
|||
*/
|
||||
@GetMapping
|
||||
public JsonResult menus() {
|
||||
final List<Menu> menus = menuService.findAll();
|
||||
final List<Menu> menus = menuService.listAll();
|
||||
if (null != menus && menus.size() > 0) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), menus);
|
||||
} else {
|
||||
|
|
|
@ -178,14 +178,14 @@ public class InstallController {
|
|||
menuIndex.setMenuUrl("/");
|
||||
menuIndex.setMenuSort(1);
|
||||
menuIndex.setMenuIcon(" ");
|
||||
menuService.save(menuIndex);
|
||||
menuService.create(menuIndex);
|
||||
|
||||
final Menu menuArchive = new Menu();
|
||||
menuArchive.setMenuName("归档");
|
||||
menuArchive.setMenuUrl("/archives");
|
||||
menuArchive.setMenuSort(2);
|
||||
menuArchive.setMenuIcon(" ");
|
||||
menuService.save(menuArchive);
|
||||
menuService.create(menuArchive);
|
||||
|
||||
OPTIONS.clear();
|
||||
OPTIONS = optionsService.findAllOptions();
|
||||
|
|
Loading…
Reference in New Issue