优化结构
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"kra/internal/service"
|
||||
"kra/internal/service/dto"
|
||||
|
||||
@@ -11,6 +12,27 @@ type Menu struct{ service *service.MenuService }
|
||||
|
||||
func NewMenu(service *service.MenuService) *Menu { return &Menu{service: service} }
|
||||
|
||||
func validateMenuRequest(req *dto.MenuRequest) error {
|
||||
if req == nil {
|
||||
return errors.New("菜单参数不能为空")
|
||||
}
|
||||
checks := []struct {
|
||||
value string
|
||||
field string
|
||||
}{
|
||||
{req.Path, "Path"}, {req.Name, "Name"}, {req.Component, "Component"}, {req.Meta.Title, "Title"},
|
||||
}
|
||||
for _, check := range checks {
|
||||
if check.value == "" {
|
||||
return errors.New(check.field + "值不能为空")
|
||||
}
|
||||
}
|
||||
if req.Sort < 0 {
|
||||
return errors.New("Sort长度或值不在合法范围,ge=0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Menu) List(c *gin.Context) {
|
||||
items, err := h.service.Tree(c.Request.Context())
|
||||
if err != nil {
|
||||
@@ -35,24 +57,8 @@ func (h *Menu) Create(c *gin.Context) {
|
||||
Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Path == "" {
|
||||
Fail(c, "Path值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Name == "" {
|
||||
Fail(c, "Name值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Component == "" {
|
||||
Fail(c, "Component值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Sort < 0 {
|
||||
Fail(c, "Sort长度或值不在合法范围,ge=0")
|
||||
return
|
||||
}
|
||||
if req.Meta.Title == "" {
|
||||
Fail(c, "Title值不能为空")
|
||||
if err := validateMenuRequest(&req); err != nil {
|
||||
Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.service.Create(c.Request.Context(), &req); err != nil {
|
||||
@@ -68,24 +74,8 @@ func (h *Menu) Update(c *gin.Context) {
|
||||
Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Path == "" {
|
||||
Fail(c, "Path值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Name == "" {
|
||||
Fail(c, "Name值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Component == "" {
|
||||
Fail(c, "Component值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Sort < 0 {
|
||||
Fail(c, "Sort长度或值不在合法范围,ge=0")
|
||||
return
|
||||
}
|
||||
if req.Meta.Title == "" {
|
||||
Fail(c, "Title值不能为空")
|
||||
if err := validateMenuRequest(&req); err != nil {
|
||||
Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.service.Update(c.Request.Context(), &req); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user