refactor🎨 api业务功能调整
This commit is contained in:
1 parent
6cbded6241
commit
2ac8f925c9
4 files changed
+39
-68
No files matched your search
@@ -30,7 +30,7 @@ type SysApi struct {
|
||||
// @Param pageSize query int false "页条数"
|
||||
// @Param pageIndex query int false "页码"
|
||||
// @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
|
||||
func (e SysApi) GetSysApiList(c *gin.Context) {
|
||||
s := new(service.SysApi)
|
||||
@@ -105,35 +105,30 @@ func (e SysApi) GetSysApi(c *gin.Context) {
|
||||
// @Router /api/v1/sys_api/{id} [put]
|
||||
// @Security Bearer
|
||||
func (e SysApi) UpdateSysApi(c *gin.Context) {
|
||||
control := new(dto.SysApiControl)
|
||||
req := new(dto.SysApiControl)
|
||||
s := new(service.SysApi)
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(control).
|
||||
Bind(req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
return
|
||||
}
|
||||
object, err := control.GenerateM()
|
||||
if err != nil {
|
||||
e.Logger.Errorf("generate SysApi model error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "模型生成失败")
|
||||
return
|
||||
}
|
||||
object.SetUpdateBy(user.GetUserId(c))
|
||||
|
||||
req.SetUpdateBy(user.GetUserId(c))
|
||||
|
||||
//数据权限检查
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
err = s.UpdateSysApi(object.(*models.SysApi), p)
|
||||
err = s.UpdateSysApi(req, p)
|
||||
if err != nil {
|
||||
e.Logger.Errorf("Update SysApi error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "更新失败")
|
||||
return
|
||||
}
|
||||
e.OK(object.GetId(), "更新成功")
|
||||
e.OK(req.GetId(), "更新成功")
|
||||
}
|
||||
|
||||
// DeleteSysApi 删除接口管理
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
// "gorm.io/gorm"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"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/storage"
|
||||
|
||||
"go-admin/common/models"
|
||||
)
|
||||
|
||||
type SysApi struct {
|
||||
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
|
||||
Handle string `json:"handle" gorm:"type:varchar(128);comment:handle"`
|
||||
Title string `json:"title" gorm:"type:varchar(128);comment:标题"`
|
||||
Permission string `json:"permission" gorm:"size:255;"`
|
||||
Path string `json:"path" gorm:"type:varchar(128);comment:地址"`
|
||||
Paths string `json:"paths" gorm:"type:varchar(128);comment:Paths"`
|
||||
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:排序"`
|
||||
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
|
||||
Handle string `json:"handle" gorm:"size:128;comment:handle"`
|
||||
Title string `json:"title" gorm:"size:128;comment:标题"`
|
||||
Path string `json:"path" gorm:"size:128;comment:地址"`
|
||||
Action string `json:"action" gorm:"size:16;comment:请求类型"`
|
||||
Type string `json:"type" gorm:"size:16;comment:接口类型"`
|
||||
models.ModelTime
|
||||
models.ControlBy
|
||||
}
|
||||
@@ -67,4 +64,4 @@ func SaveSysApi(message storage.Messager) (err error) {
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -43,47 +43,24 @@ func (m *SysApiSearch) Bind(ctx *gin.Context) error {
|
||||
}
|
||||
|
||||
type SysApiControl struct {
|
||||
Id int `uri:"id" comment:"编码"` // 编码
|
||||
Handle string `json:"handle" comment:"handle"`
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Path string `json:"path" comment:"地址"`
|
||||
Paths string `json:"paths" comment:""`
|
||||
Action string `json:"action" comment:"类型"`
|
||||
ParentId int `json:"parentId" comment:"按钮id"`
|
||||
Sort int `json:"sort" comment:"排序"`
|
||||
Id int `uri:"id" comment:"编码"` // 编码
|
||||
Handle string `json:"handle" comment:"handle"`
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Path string `json:"path" comment:"地址"`
|
||||
Type string `json:"type" comment:""`
|
||||
Action string `json:"action" comment:"类型"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *SysApiControl) Generate() dto.Control {
|
||||
o := *s
|
||||
return &o
|
||||
}
|
||||
|
||||
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("数据绑定出错")
|
||||
func (s *SysApiControl) Generate(model *models.SysApi) {
|
||||
if s.Id != 0 {
|
||||
model.Id = s.Id
|
||||
}
|
||||
err = ctx.ShouldBind(s)
|
||||
if err != nil {
|
||||
log.Errorf("ShouldBind error: %s", err.Error())
|
||||
err = errors.New("数据绑定出错")
|
||||
}
|
||||
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
|
||||
model.Handle = s.Handle
|
||||
model.Title = s.Title
|
||||
model.Path = s.Path
|
||||
model.Type = s.Type
|
||||
model.Action = s.Action
|
||||
}
|
||||
|
||||
func (s *SysApiControl) GetId() interface{} {
|
||||
|
||||
@@ -75,11 +75,13 @@ func (e *SysApi) CheckStorageSysApi(c *[]runtime.Router) error {
|
||||
}
|
||||
|
||||
// UpdateSysApi 修改SysApi对象
|
||||
func (e *SysApi) UpdateSysApi(c *models.SysApi, p *actions.DataPermission) error {
|
||||
db := e.Orm.Model(c).
|
||||
Scopes(
|
||||
actions.Permission(c.TableName(), p),
|
||||
).Where(c.GetId()).Updates(c)
|
||||
func (e *SysApi) UpdateSysApi(c *dto.SysApiControl, p *actions.DataPermission) error {
|
||||
var model = models.SysApi{}
|
||||
e.Orm.Scopes(
|
||||
actions.Permission(model.TableName(), p),
|
||||
).First(&model, c.GetId())
|
||||
c.Generate(&model)
|
||||
db := e.Orm.Save(&model)
|
||||
if err := db.Error; err != nil {
|
||||
e.Log.Errorf("Service UpdateSysApi error:%s", err)
|
||||
return err
|
||||
@@ -106,4 +108,4 @@ func (e *SysApi) RemoveSysApi(d *dto.SysApiById, p *actions.DataPermission) erro
|
||||
return errors.New("无权删除该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user