优化
This commit is contained in:
@@ -18,6 +18,14 @@ func (h *API) List(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Page == 0 {
|
||||
httpx.Fail(c, "Page值不能为空")
|
||||
return
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
httpx.Fail(c, "PageSize值不能为空")
|
||||
return
|
||||
}
|
||||
items, total, err := h.service.ListAPI(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "获取失败")
|
||||
@@ -39,6 +47,10 @@ func (h *API) Create(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if message := validateAPIRequest(&req); message != "" {
|
||||
httpx.Fail(c, message)
|
||||
return
|
||||
}
|
||||
created, err := h.service.CreateAPIRequest(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "创建失败")
|
||||
@@ -52,6 +64,10 @@ func (h *API) Update(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if message := validateAPIRequest(&req); message != "" {
|
||||
httpx.Fail(c, message)
|
||||
return
|
||||
}
|
||||
if err := h.service.UpdateAPIRequest(c.Request.Context(), &req); err != nil {
|
||||
httpx.Fail(c, "修改失败")
|
||||
return
|
||||
@@ -64,7 +80,11 @@ func (h *API) Delete(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.service.DeleteAPIs(c.Request.Context(), []uint{req.ID}); err != nil {
|
||||
if req.ID == 0 {
|
||||
httpx.Fail(c, "ID值不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.DeleteAPI(c.Request.Context(), req.ID); err != nil {
|
||||
httpx.Fail(c, "删除失败")
|
||||
return
|
||||
}
|
||||
@@ -88,6 +108,10 @@ func (h *API) Find(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.ID == 0 {
|
||||
httpx.Fail(c, "ID值不能为空")
|
||||
return
|
||||
}
|
||||
item, err := h.service.FindAPIResponse(c.Request.Context(), req.ID)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "获取失败")
|
||||
@@ -156,10 +180,6 @@ func (h *API) Ignore(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Path == "" || req.Method == "" {
|
||||
httpx.Fail(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := h.service.SetAPIIgnored(c.Request.Context(), req.Path, req.Method, req.Flag); err != nil {
|
||||
httpx.Fail(c, "忽略失败")
|
||||
return
|
||||
@@ -173,12 +193,15 @@ func (h *API) ApplySync(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if err := h.service.ApplyAPISyncRequest(c.Request.Context(), &req); err != nil {
|
||||
httpx.Fail(c, "同步失败")
|
||||
httpx.Fail(c, "忽略失败")
|
||||
return
|
||||
}
|
||||
httpx.OK(c)
|
||||
}
|
||||
func (h *API) FreshCasbin(c *gin.Context) {
|
||||
// Policies are read from casbin_rule on every authorization decision, so
|
||||
// there is no in-memory enforcer cache to reload. Keep the compatible
|
||||
// endpoint and success response.
|
||||
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "刷新成功")
|
||||
}
|
||||
|
||||
@@ -188,6 +211,10 @@ func (h *API) SetPolicyPaths(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.SetPolicyPathsRequest(c.Request.Context(), &req); err != nil {
|
||||
httpx.Fail(c, "更新失败")
|
||||
return
|
||||
@@ -200,6 +227,10 @@ func (h *API) PolicyPaths(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
paths, err := h.service.PolicyPathResponses(c.Request.Context(), req.AuthorityID)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "获取失败")
|
||||
@@ -207,3 +238,18 @@ func (h *API) PolicyPaths(c *gin.Context) {
|
||||
}
|
||||
httpx.Write(c, httpx.CodeSuccess, gin.H{"paths": paths}, "获取成功")
|
||||
}
|
||||
|
||||
func validateAPIRequest(req *dto.APIRequest) string {
|
||||
switch {
|
||||
case req.Path == "":
|
||||
return "Path值不能为空"
|
||||
case req.Description == "":
|
||||
return "Description值不能为空"
|
||||
case req.APIGroup == "":
|
||||
return "ApiGroup值不能为空"
|
||||
case req.Method == "":
|
||||
return "Method值不能为空"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user