兼容批量删除
This commit is contained in:
1 parent
560e650070
commit
0ae41f247c
5 files changed
+27
-6
No files matched your search
@@ -39,7 +39,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
|
|||||||
|
|
||||||
db = db.WithContext(c).Scopes(
|
db = db.WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).Delete(object)
|
).Where(req.GetId()).Delete(object)
|
||||||
tools.HasError(db.Error, "删除失败", 500)
|
tools.HasError(db.Error, "删除失败", 500)
|
||||||
if db.RowsAffected == 0 {
|
if db.RowsAffected == 0 {
|
||||||
err = errors.New("无权删除该数据")
|
err = errors.New("无权删除该数据")
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
|
|||||||
|
|
||||||
db = db.WithContext(c).Scopes(
|
db = db.WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).Updates(object)
|
).Where(req.GetId()).Updates(object)
|
||||||
tools.HasError(db.Error, "更新失败", 500)
|
tools.HasError(db.Error, "更新失败", 500)
|
||||||
if db.RowsAffected == 0 {
|
if db.RowsAffected == 0 {
|
||||||
err = errors.New("无权更新该数据")
|
err = errors.New("无权更新该数据")
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func ViewAction(control dto.Control) gin.HandlerFunc {
|
|||||||
|
|
||||||
err = db.WithContext(c).Scopes(
|
err = db.WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).First(object).Error
|
).Where(req.GetId()).First(object).Error
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
tools.HasError(err, "查看失败", 404)
|
tools.HasError(err, "查看失败", 404)
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-3
@@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"go-admin/models"
|
"go-admin/models"
|
||||||
"go-admin/tools/model"
|
"go-admin/tools/model"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SysJobSearch struct {
|
type SysJobSearch struct {
|
||||||
@@ -76,11 +77,25 @@ func (s *SysJobControl) GenerateM() (model.ActiveRecord, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SysJobControl) GetId() interface{} {
|
||||||
|
return s.JobId
|
||||||
|
}
|
||||||
|
|
||||||
type SysJobById struct {
|
type SysJobById struct {
|
||||||
Id int `uri:"id" validate:"required"`
|
Id int `uri:"id" validate:"required"`
|
||||||
|
Ids []int `json:"ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SysJobById) Bind(ctx *gin.Context) error {
|
func (s *SysJobById) Bind(ctx *gin.Context) error {
|
||||||
|
if ctx.Request.Method == http.MethodDelete {
|
||||||
|
err := ctx.Bind(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(s.Ids) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
return ctx.BindUri(s)
|
return ctx.BindUri(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +105,12 @@ func (s *SysJobById) Generate() Control {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SysJobById) GenerateM() (model.ActiveRecord, error) {
|
func (s *SysJobById) GenerateM() (model.ActiveRecord, error) {
|
||||||
return &models.SysJob{
|
return &models.SysJob{}, nil
|
||||||
JobId: s.Id,
|
}
|
||||||
}, nil
|
|
||||||
|
func (s *SysJobById) GetId() interface{} {
|
||||||
|
if len(s.Ids) > 0 {
|
||||||
|
return s.Ids
|
||||||
|
}
|
||||||
|
return s.Id
|
||||||
}
|
}
|
||||||
@@ -17,4 +17,5 @@ type Control interface {
|
|||||||
Generate() Control
|
Generate() Control
|
||||||
Bind(ctx *gin.Context) error
|
Bind(ctx *gin.Context) error
|
||||||
GenerateM() (model.ActiveRecord, error)
|
GenerateM() (model.ActiveRecord, error)
|
||||||
|
GetId() interface{}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user