mirror of https://github.com/halo-dev/halo
Complete menu creation api
parent
7793737459
commit
65d42a1bdb
|
@ -31,13 +31,13 @@ public class Menu extends BaseEntity {
|
||||||
/**
|
/**
|
||||||
* Menu name.
|
* Menu name.
|
||||||
*/
|
*/
|
||||||
@Column(name = "name", columnDefinition = "varchar(255) not null")
|
@Column(name = "name", columnDefinition = "varchar(50) not null")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Menu access url.
|
* Menu access url.
|
||||||
*/
|
*/
|
||||||
@Column(name = "url", columnDefinition = "varchar(255) not null")
|
@Column(name = "url", columnDefinition = "varchar(1023) not null")
|
||||||
private String url;
|
private String url;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package cc.ryanc.halo.model.params;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.base.InputConverter;
|
||||||
|
import cc.ryanc.halo.model.entity.Menu;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
|
import org.hibernate.validator.constraints.URL;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Menu param.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 4/3/19
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
public class MenuParam implements InputConverter<Menu> {
|
||||||
|
|
||||||
|
@NotBlank(message = "Menu name must not be blank")
|
||||||
|
@Size(max = 50, message = "Length of menu name must not be more than {max}")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@NotBlank(message = "Menu url must not be blank")
|
||||||
|
@Size(max = 1023, message = "Length of menu url must not be more than {max}")
|
||||||
|
@URL(message = "Menu url has incorrect url format")
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
@Min(value = 0, message = "Menu sort must not be less than {value}")
|
||||||
|
private Integer sort;
|
||||||
|
|
||||||
|
@Size(max = 50, message = "Length of menu target must not be more than {max}")
|
||||||
|
private String target;
|
||||||
|
|
||||||
|
@Size(max = 50, message = "Length of menu icon must not be more than {max}")
|
||||||
|
private String icon;
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.repository;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.entity.Menu;
|
import cc.ryanc.halo.model.entity.Menu;
|
||||||
import cc.ryanc.halo.repository.base.BaseRepository;
|
import cc.ryanc.halo.repository.base.BaseRepository;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Menu repository.
|
* Menu repository.
|
||||||
|
@ -9,4 +10,12 @@ import cc.ryanc.halo.repository.base.BaseRepository;
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
*/
|
*/
|
||||||
public interface MenuRepository extends BaseRepository<Menu, Integer> {
|
public interface MenuRepository extends BaseRepository<Menu, Integer> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exists by menu name.
|
||||||
|
*
|
||||||
|
* @param name must not be blank
|
||||||
|
* @return true if exists; false otherwise
|
||||||
|
*/
|
||||||
|
boolean existsByName(@NonNull String name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.service;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.MenuOutputDTO;
|
import cc.ryanc.halo.model.dto.MenuOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Menu;
|
import cc.ryanc.halo.model.entity.Menu;
|
||||||
|
import cc.ryanc.halo.model.params.MenuParam;
|
||||||
import cc.ryanc.halo.service.base.CrudService;
|
import cc.ryanc.halo.service.base.CrudService;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
|
@ -23,4 +24,13 @@ public interface MenuService extends CrudService<Menu, Integer> {
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
List<MenuOutputDTO> listDtos(@NonNull Sort sort);
|
List<MenuOutputDTO> listDtos(@NonNull Sort sort);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a menu.
|
||||||
|
*
|
||||||
|
* @param menuParam must not be null
|
||||||
|
* @return created menu
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Menu createBy(@NonNull MenuParam menuParam);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package cc.ryanc.halo.service.impl;
|
package cc.ryanc.halo.service.impl;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.exception.AlreadyExistsException;
|
||||||
import cc.ryanc.halo.model.dto.MenuOutputDTO;
|
import cc.ryanc.halo.model.dto.MenuOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Menu;
|
import cc.ryanc.halo.model.entity.Menu;
|
||||||
|
import cc.ryanc.halo.model.params.MenuParam;
|
||||||
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 cc.ryanc.halo.service.base.AbstractCrudService;
|
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||||
|
@ -37,6 +39,22 @@ public class MenuServiceImpl extends AbstractCrudService<Menu, Integer> implemen
|
||||||
return convertTo(listAll(sort));
|
return convertTo(listAll(sort));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Menu createBy(MenuParam menuParam) {
|
||||||
|
Assert.notNull(menuParam, "Menu param must not be null");
|
||||||
|
|
||||||
|
// Check the name
|
||||||
|
boolean exists = menuRepository.existsByName(menuParam.getName());
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
throw new AlreadyExistsException("The menu name " + menuParam.getName() + " has already existed").setErrorData(menuParam.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create an return
|
||||||
|
return create(menuParam.convertTo());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private List<MenuOutputDTO> convertTo(List<Menu> menus) {
|
private List<MenuOutputDTO> convertTo(List<Menu> menus) {
|
||||||
if (CollectionUtils.isEmpty(menus)) {
|
if (CollectionUtils.isEmpty(menus)) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
package cc.ryanc.halo.web.controller.admin.api;
|
package cc.ryanc.halo.web.controller.admin.api;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.MenuOutputDTO;
|
||||||
|
import cc.ryanc.halo.model.entity.Menu;
|
||||||
|
import cc.ryanc.halo.model.params.MenuParam;
|
||||||
import cc.ryanc.halo.service.MenuService;
|
import cc.ryanc.halo.service.MenuService;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.data.web.SortDefault;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Menu controller.
|
* Menu controller.
|
||||||
|
@ -20,4 +30,15 @@ public class MenuController {
|
||||||
this.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ApiOperation("Creates a menu")
|
||||||
|
public MenuOutputDTO createBy(@RequestBody @Valid MenuParam menuParam) {
|
||||||
|
return new MenuOutputDTO().convertFrom(menuService.createBy(menuParam));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue