Complete menu list api

pull/137/head
johnniang 2019-04-03 17:04:06 +08:00
parent 69b25189b6
commit 7793737459
7 changed files with 148 additions and 8 deletions

View File

@ -0,0 +1,27 @@
package cc.ryanc.halo.model.dto;
import cc.ryanc.halo.model.dto.base.OutputConverter;
import cc.ryanc.halo.model.entity.Menu;
import lombok.Data;
/**
* Menu output dto.
*
* @author johnniang
* @date 4/3/19
*/
@Data
public class MenuOutputDTO implements OutputConverter<MenuOutputDTO, Menu> {
private Integer id;
private String name;
private String url;
private Integer sort;
private String target;
private String icon;
}

View File

@ -62,6 +62,19 @@ public class Menu extends BaseEntity {
@Override
public void prePersist() {
super.prePersist();
id = null;
if (sort == null) {
sort = 0;
}
if (target == null) {
target = "_self";
}
if (icon == null) {
icon = "";
}
}
}

View File

@ -1,7 +1,12 @@
package cc.ryanc.halo.service;
import cc.ryanc.halo.model.dto.MenuOutputDTO;
import cc.ryanc.halo.model.entity.Menu;
import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;
import java.util.List;
/**
* Menu service.
@ -10,4 +15,12 @@ import cc.ryanc.halo.service.base.CrudService;
*/
public interface MenuService extends CrudService<Menu, Integer> {
/**
* Lists all menu dtos.
*
* @param sort must not be null
* @return a list of menu output dto
*/
@NonNull
List<MenuOutputDTO> listDtos(@NonNull Sort sort);
}

View File

@ -1,10 +1,18 @@
package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.dto.MenuOutputDTO;
import cc.ryanc.halo.model.entity.Menu;
import cc.ryanc.halo.repository.MenuRepository;
import cc.ryanc.halo.service.MenuService;
import cc.ryanc.halo.service.base.AbstractCrudService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* MenuService implementation class
@ -21,4 +29,21 @@ public class MenuServiceImpl extends AbstractCrudService<Menu, Integer> implemen
super(menuRepository);
this.menuRepository = menuRepository;
}
@Override
public List<MenuOutputDTO> listDtos(Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
return convertTo(listAll(sort));
}
private List<MenuOutputDTO> convertTo(List<Menu> menus) {
if (CollectionUtils.isEmpty(menus)) {
return Collections.emptyList();
}
return menus.stream()
.map(menu -> new MenuOutputDTO().<MenuOutputDTO>convertFrom(menu))
.collect(Collectors.toList());
}
}

View File

@ -307,14 +307,15 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
List<ArchiveMonthVO> archives = new LinkedList<>();
yearMonthPostMap.forEach((year, monthPostMap) -> monthPostMap.forEach((month, postList) -> {
ArchiveMonthVO archive = new ArchiveMonthVO();
archive.setYear(year);
archive.setMonth(month);
archive.setPosts(convertTo(postList));
yearMonthPostMap.forEach((year, monthPostMap) ->
monthPostMap.forEach((month, postList) -> {
ArchiveMonthVO archive = new ArchiveMonthVO();
archive.setYear(year);
archive.setMonth(month);
archive.setPosts(convertTo(postList));
archives.add(archive);
}));
archives.add(archive);
}));
// Sort this list
archives.sort(new ArchiveMonthVO.ArchiveComparator());
@ -355,7 +356,8 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
}
// Convert
return posts.stream().map(post -> new PostMinimalOutputDTO().<PostMinimalOutputDTO>convertFrom(post))
return posts.stream()
.map(post -> new PostMinimalOutputDTO().<PostMinimalOutputDTO>convertFrom(post))
.collect(Collectors.toList());
}

View File

@ -0,0 +1,23 @@
package cc.ryanc.halo.web.controller.admin.api;
import cc.ryanc.halo.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Menu controller.
*
* @author johnniang
* @date 4/3/19
*/
@RestController
@RequestMapping("/admin/api/menus")
public class MenuController {
private final MenuService menuService;
public MenuController(MenuService menuService) {
this.menuService = menuService;
}
}

View File

@ -0,0 +1,37 @@
package cc.ryanc.halo.web.controller.portal.api;
import cc.ryanc.halo.model.dto.MenuOutputDTO;
import cc.ryanc.halo.service.MenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Portal menu controller.
*
* @author johnniang
* @date 4/3/19
*/
@RestController("PortalMenuController")
@RequestMapping("/api/menus")
public class MenuController {
private final MenuService menuService;
public MenuController(MenuService menuService) {
this.menuService = menuService;
}
@GetMapping
@ApiOperation("Lists all menus")
public List<MenuOutputDTO> listAll(@SortDefault(sort = "sort", direction = DESC) Sort sort) {
return menuService.listDtos(sort);
}
}