Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* feat: halo-dev#1119

* feat: halo-dev#1119

* pref: allows multiple same menus.

* feat: support create menu in batch.

* feat: support delete menu in batch.

* feat: halo-dev#1119
  • Loading branch information
ruibaby authored Nov 2, 2020
1 parent ff734ed commit c9ce7d2
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.MenuDTO;
import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Menu;
import run.halo.app.model.params.MenuParam;
import run.halo.app.model.vo.MenuVO;
import run.halo.app.service.MenuService;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.domain.Sort.Direction.DESC;
Expand Down Expand Up @@ -40,11 +42,17 @@ public List<MenuDTO> listAll(@SortDefault(sort = "team", direction = DESC) Sort
}

@GetMapping("tree_view")
@ApiOperation("Lists categories as tree")
@ApiOperation("Lists menus as tree")
public List<MenuVO> listAsTree(@SortDefault(sort = "team", direction = DESC) Sort sort) {
return menuService.listAsTree(sort.and(Sort.by(ASC, "priority")));
}

@GetMapping("team/tree_view")
@ApiOperation("Lists menus as tree by team")
public List<MenuVO> listDefaultsAsTreeByTeam(@SortDefault(sort = "priority", direction = ASC) Sort sort, @RequestParam(name = "team") String team) {
return menuService.listByTeamAsTree(team, sort);
}

@GetMapping("{menuId:\\d+}")
@ApiOperation("Gets menu detail by id")
public MenuDTO getBy(@PathVariable("menuId") Integer menuId) {
Expand All @@ -57,10 +65,20 @@ public MenuDTO createBy(@RequestBody @Valid MenuParam menuParam) {
return new MenuDTO().convertFrom(menuService.createBy(menuParam));
}

@PostMapping("/batch")
public List<MenuDTO> createBatchBy(@RequestBody @Valid List<MenuParam> menuParams) {
List<Menu> menus = menuParams
.stream()
.map(InputConverter::convertTo)
.collect(Collectors.toList());
return menuService.createInBatch(menus).stream()
.map(menu -> (MenuDTO) new MenuDTO().convertFrom(menu))
.collect(Collectors.toList());
}

@PutMapping("{menuId:\\d+}")
@ApiOperation("Updates a menu")
public MenuDTO updateBy(@PathVariable("menuId") Integer menuId,
@RequestBody @Valid MenuParam menuParam) {
public MenuDTO updateBy(@PathVariable("menuId") Integer menuId, @RequestBody @Valid MenuParam menuParam) {
// Get the menu
Menu menu = menuService.getById(menuId);

Expand All @@ -71,6 +89,17 @@ public MenuDTO updateBy(@PathVariable("menuId") Integer menuId,
return new MenuDTO().convertFrom(menuService.update(menu));
}

@PutMapping("/batch")
public List<MenuDTO> updateBatchBy(@RequestBody @Valid List<MenuParam> menuParams) {
List<Menu> menus = menuParams
.stream()
.map(InputConverter::convertTo)
.collect(Collectors.toList());
return menuService.updateInBatch(menus).stream()
.map(menu -> (MenuDTO) new MenuDTO().convertFrom(menu))
.collect(Collectors.toList());
}

@DeleteMapping("{menuId:\\d+}")
@ApiOperation("Deletes a menu")
public MenuDTO deleteBy(@PathVariable("menuId") Integer menuId) {
Expand All @@ -84,6 +113,15 @@ public MenuDTO deleteBy(@PathVariable("menuId") Integer menuId) {
return new MenuDTO().convertFrom(menuService.removeById(menuId));
}

@DeleteMapping("/batch")
public List<MenuDTO> deleteBatchBy(@RequestBody List<Integer> menuIds) {
List<Menu> menus = menuService.listAllByIds(menuIds);
menuService.removeInBatch(menuIds);
return menus.stream()
.map(menu -> (MenuDTO) new MenuDTO().convertFrom(menu))
.collect(Collectors.toList());
}

@GetMapping("teams")
@ApiOperation("Lists all menu teams")
public List<String> teams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import freemarker.template.*;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.MenuService;
import run.halo.app.service.OptionService;

import java.io.IOException;
import java.util.Map;
Expand All @@ -25,8 +27,11 @@ public class MenuTagDirective implements TemplateDirectiveModel {

private final MenuService menuService;

public MenuTagDirective(Configuration configuration, MenuService menuService) {
private final OptionService optionService;

public MenuTagDirective(Configuration configuration, MenuService menuService, OptionService optionService) {
this.menuService = menuService;
this.optionService = optionService;
configuration.setSharedVariable("menuTag", this);
}

Expand All @@ -38,10 +43,12 @@ public void execute(Environment env, Map params, TemplateModel[] loopVars, Templ
String method = params.get(HaloConst.METHOD_KEY).toString();
switch (method) {
case "list":
env.setVariable("menus", builder.build().wrap(menuService.listAll()));
String listTeam = optionService.getByPropertyOrDefault(PrimaryProperties.DEFAULT_MENU_TEAM, String.class, "");
env.setVariable("menus", builder.build().wrap(menuService.listByTeam(listTeam, Sort.by(DESC, "priority"))));
break;
case "tree":
env.setVariable("menus", builder.build().wrap(menuService.listAsTree(Sort.by(DESC, "priority"))));
String treeTeam = optionService.getByPropertyOrDefault(PrimaryProperties.DEFAULT_MENU_TEAM, String.class, "");
env.setVariable("menus", builder.build().wrap(menuService.listByTeamAsTree(treeTeam, Sort.by(DESC, "priority"))));
break;
case "listTeams":
env.setVariable("teams", builder.build().wrap(menuService.listTeamVos(Sort.by(DESC, "priority"))));
Expand All @@ -51,8 +58,8 @@ public void execute(Environment env, Map params, TemplateModel[] loopVars, Templ
env.setVariable("menus", builder.build().wrap(menuService.listByTeam(team, Sort.by(DESC, "priority"))));
break;
case "treeByTeam":
String treeTeam = params.get("team").toString();
env.setVariable("menus", builder.build().wrap(menuService.listByTeamAsTree(treeTeam, Sort.by(DESC, "priority"))));
String treeTeamParam = params.get("team").toString();
env.setVariable("menus", builder.build().wrap(menuService.listByTeamAsTree(treeTeamParam, Sort.by(DESC, "priority"))));
break;
case "count":
env.setVariable("count", builder.build().wrap(menuService.count()));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/run/halo/app/model/params/MenuParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@ToString
public class MenuParam implements InputConverter<Menu> {

private Integer id;

@NotBlank(message = "菜单名称不能为空")
@Size(max = 50, message = "菜单名称的字符长度不能超过 {max}")
private String name;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public enum PrimaryProperties implements PropertyEnum {
/**
* developer mode.
*/
DEV_MODE("developer_mode", Boolean.class, "false");
DEV_MODE("developer_mode", Boolean.class, "false"),

/**
* default menu team name
*/
DEFAULT_MENU_TEAM("default_menu_team", String.class, "");

private final String value;

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/run/halo/app/model/properties/PropertyEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ static Map<String, PropertyEnum> getValuePropertyEnumMap() {
propertyEnumClasses.add(SeoProperties.class);
propertyEnumClasses.add(UpOssProperties.class);
propertyEnumClasses.add(ApiProperties.class);
propertyEnumClasses.add(StaticDeployProperties.class);
propertyEnumClasses.add(GitStaticDeployProperties.class);
propertyEnumClasses.add(NetlifyStaticDeployProperties.class);
propertyEnumClasses.add(PermalinkProperties.class);

Map<String, PropertyEnum> result = new HashMap<>();
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/run/halo/app/model/vo/MenuVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.ToString;
import run.halo.app.model.dto.MenuDTO;

import java.util.LinkedList;
import java.util.List;

/**
Expand All @@ -16,5 +17,5 @@
@ToString(callSuper = true)
public class MenuVO extends MenuDTO {

private List<MenuVO> children;
private List<MenuVO> children = new LinkedList<>();
}
5 changes: 1 addition & 4 deletions src/main/java/run/halo/app/service/impl/MenuServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,11 @@ public List<String> listAllTeams() {

@Override
public @NotNull Menu create(@NotNull Menu menu) {
nameMustNotExist(menu);

return super.create(menu);
}

@Override
public @NotNull Menu update(@NotNull Menu menu) {
nameMustNotExist(menu);

return super.update(menu);
}

Expand Down Expand Up @@ -215,6 +211,7 @@ private List<MenuDTO> convertTo(List<Menu> menus) {
.collect(Collectors.toList());
}

@Deprecated
private void nameMustNotExist(@NonNull Menu menu) {
Assert.notNull(menu, "Menu must not be null");

Expand Down

0 comments on commit c9ce7d2

Please sign in to comment.