refactor🎨 api业务功能调整

This commit is contained in:
zhangwenjian committed 2021-05-23 17:52:43 +08:00
1 parent 6cbded6241
commit 2ac8f925c9
4 files changed
+39 -68

No files matched your search

+7 -12
View File
@@ -30,7 +30,7 @@ type SysApi struct {
// @Param pageSize query int false "页条数" // @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码" // @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.SysApi}} "{"code": 200, "data": [...]}" // @Success 200 {object} response.Response{data=response.Page{list=[]models.SysApi}} "{"code": 200, "data": [...]}"
// @Router /api/v1/sys_api [get] // @Router /api/v1/sys-api [get]
// @Security Bearer // @Security Bearer
func (e SysApi) GetSysApiList(c *gin.Context) { func (e SysApi) GetSysApiList(c *gin.Context) {
s := new(service.SysApi) s := new(service.SysApi)
@@ -105,35 +105,30 @@ func (e SysApi) GetSysApi(c *gin.Context) {
// @Router /api/v1/sys_api/{id} [put] // @Router /api/v1/sys_api/{id} [put]
// @Security Bearer // @Security Bearer
func (e SysApi) UpdateSysApi(c *gin.Context) { func (e SysApi) UpdateSysApi(c *gin.Context) {
control := new(dto.SysApiControl) req := new(dto.SysApiControl)
s := new(service.SysApi) s := new(service.SysApi)
err := e.MakeContext(c). err := e.MakeContext(c).
MakeOrm(). MakeOrm().
Bind(control). Bind(req).
MakeService(&s.Service). MakeService(&s.Service).
Errors Errors
if err != nil { if err != nil {
e.Logger.Error(err) e.Logger.Error(err)
return return
} }
object, err := control.GenerateM()
if err != nil { req.SetUpdateBy(user.GetUserId(c))
e.Logger.Errorf("generate SysApi model error, %s", err.Error())
e.Error(http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetUpdateBy(user.GetUserId(c))
//数据权限检查 //数据权限检查
p := actions.GetPermissionFromContext(c) p := actions.GetPermissionFromContext(c)
err = s.UpdateSysApi(object.(*models.SysApi), p) err = s.UpdateSysApi(req, p)
if err != nil { if err != nil {
e.Logger.Errorf("Update SysApi error, %s", err.Error()) e.Logger.Errorf("Update SysApi error, %s", err.Error())
e.Error(http.StatusInternalServerError, err, "更新失败") e.Error(http.StatusInternalServerError, err, "更新失败")
return return
} }
e.OK(object.GetId(), "更新成功") e.OK(req.GetId(), "更新成功")
} }
// DeleteSysApi 删除接口管理 // DeleteSysApi 删除接口管理
+9 -12
View File
@@ -1,26 +1,23 @@
package models package models
import ( import (
// "gorm.io/gorm"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/go-admin-team/go-admin-core/sdk" "github.com/go-admin-team/go-admin-core/sdk"
"github.com/go-admin-team/go-admin-core/sdk/runtime" "github.com/go-admin-team/go-admin-core/sdk/runtime"
"github.com/go-admin-team/go-admin-core/storage" "github.com/go-admin-team/go-admin-core/storage"
"go-admin/common/models" "go-admin/common/models"
) )
type SysApi struct { type SysApi struct {
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"` Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
Handle string `json:"handle" gorm:"type:varchar(128);comment:handle"` Handle string `json:"handle" gorm:"size:128;comment:handle"`
Title string `json:"title" gorm:"type:varchar(128);comment:标题"` Title string `json:"title" gorm:"size:128;comment:标题"`
Permission string `json:"permission" gorm:"size:255;"` Path string `json:"path" gorm:"size:128;comment:地址"`
Path string `json:"path" gorm:"type:varchar(128);comment:地址"` Action string `json:"action" gorm:"size:16;comment:请求类型"`
Paths string `json:"paths" gorm:"type:varchar(128);comment:Paths"` Type string `json:"type" gorm:"size:16;comment:接口类型"`
Action string `json:"action" gorm:"type:varchar(16);comment:类型"`
ParentId int `json:"parentId" gorm:"comment:按钮id"`
Sort int `json:"sort" gorm:"type:tinyint(4);comment:排序"`
models.ModelTime models.ModelTime
models.ControlBy models.ControlBy
} }
@@ -67,4 +64,4 @@ func SaveSysApi(message storage.Messager) (err error) {
} }
} }
return nil return nil
} }
+15 -38
View File
@@ -43,47 +43,24 @@ func (m *SysApiSearch) Bind(ctx *gin.Context) error {
} }
type SysApiControl struct { type SysApiControl struct {
Id int `uri:"id" comment:"编码"` // 编码 Id int `uri:"id" comment:"编码"` // 编码
Handle string `json:"handle" comment:"handle"` Handle string `json:"handle" comment:"handle"`
Title string `json:"title" comment:"标题"` Title string `json:"title" comment:"标题"`
Path string `json:"path" comment:"地址"` Path string `json:"path" comment:"地址"`
Paths string `json:"paths" comment:""` Type string `json:"type" comment:""`
Action string `json:"action" comment:"类型"` Action string `json:"action" comment:"类型"`
ParentId int `json:"parentId" comment:"按钮id"` common.ControlBy
Sort int `json:"sort" comment:"排序"`
} }
func (s *SysApiControl) Generate() dto.Control { func (s *SysApiControl) Generate(model *models.SysApi) {
o := *s if s.Id != 0 {
return &o model.Id = s.Id
}
func (s *SysApiControl) Bind(ctx *gin.Context) error {
log := api.GetRequestLogger(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Errorf("ShouldBindUri error: %s", err.Error())
return errors.New("数据绑定出错")
} }
err = ctx.ShouldBind(s) model.Handle = s.Handle
if err != nil { model.Title = s.Title
log.Errorf("ShouldBind error: %s", err.Error()) model.Path = s.Path
err = errors.New("数据绑定出错") model.Type = s.Type
} model.Action = s.Action
return err
}
func (s *SysApiControl) GenerateM() (common.ActiveRecord, error) {
return &models.SysApi{
Id: s.Id,
Handle: s.Handle,
Title: s.Title,
Path: s.Path,
Paths: s.Paths,
Action: s.Action,
ParentId: s.ParentId,
Sort: s.Sort,
}, nil
} }
func (s *SysApiControl) GetId() interface{} { func (s *SysApiControl) GetId() interface{} {
+8 -6
View File
@@ -75,11 +75,13 @@ func (e *SysApi) CheckStorageSysApi(c *[]runtime.Router) error {
} }
// UpdateSysApi 修改SysApi对象 // UpdateSysApi 修改SysApi对象
func (e *SysApi) UpdateSysApi(c *models.SysApi, p *actions.DataPermission) error { func (e *SysApi) UpdateSysApi(c *dto.SysApiControl, p *actions.DataPermission) error {
db := e.Orm.Model(c). var model = models.SysApi{}
Scopes( e.Orm.Scopes(
actions.Permission(c.TableName(), p), actions.Permission(model.TableName(), p),
).Where(c.GetId()).Updates(c) ).First(&model, c.GetId())
c.Generate(&model)
db := e.Orm.Save(&model)
if err := db.Error; err != nil { if err := db.Error; err != nil {
e.Log.Errorf("Service UpdateSysApi error:%s", err) e.Log.Errorf("Service UpdateSysApi error:%s", err)
return err return err
@@ -106,4 +108,4 @@ func (e *SysApi) RemoveSysApi(d *dto.SysApiById, p *actions.DataPermission) erro
return errors.New("无权删除该数据") return errors.New("无权删除该数据")
} }
return nil return nil
} }