feat(cool): 添加删除和更新操作的数据影响行数检查
- 在Controller的Delete方法中添加RowsAffected检查,当影响行数为0时返回"not found"错误
- 在Controller的Update方法中添加RowsAffected检查,当影响行数为0时返回"not found"错误
- 修改Service接口定义,将ServiceDelete和ServiceUpdate方法的返回值类型从interface{}改为sql
This commit is contained in:
@@ -98,6 +98,11 @@ func (c *Controller) Delete(ctx context.Context, req *DeleteReq) (res *BaseRes,
|
||||
if err != nil {
|
||||
return Fail(err.Error()), err
|
||||
}
|
||||
t, _ := data.RowsAffected()
|
||||
if t == 0 {
|
||||
return Fail("not found"), err
|
||||
}
|
||||
|
||||
c.Service.ModifyAfter(ctx, "Delete", g.RequestFromCtx(ctx).GetMap())
|
||||
return Ok(data), err
|
||||
}
|
||||
@@ -115,6 +120,10 @@ func (c *Controller) Update(ctx context.Context, req *UpdateReq) (res *BaseRes,
|
||||
if err != nil {
|
||||
return Fail(err.Error()), err
|
||||
}
|
||||
t, _ := data.RowsAffected()
|
||||
if t == 0 {
|
||||
return Fail("not found"), err
|
||||
}
|
||||
c.Service.ModifyAfter(ctx, "Update", g.RequestFromCtx(ctx).GetMap())
|
||||
return Ok(data), err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
@@ -11,20 +12,21 @@ import (
|
||||
)
|
||||
|
||||
type IService interface {
|
||||
ServiceAdd(ctx context.Context, req *AddReq) (data interface{}, err error) // 新增
|
||||
ServiceDelete(ctx context.Context, req *DeleteReq) (data interface{}, err error) // 删除
|
||||
ServiceUpdate(ctx context.Context, req *UpdateReq) (data interface{}, err error) // 修改
|
||||
ServiceInfo(ctx context.Context, req *InfoReq) (data interface{}, err error) // 详情
|
||||
ServiceList(ctx context.Context, req *ListReq) (data interface{}, err error) // 列表
|
||||
ServicePage(ctx context.Context, req *PageReq) (data interface{}, err error) // 分页
|
||||
ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改前的操作
|
||||
ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改后的操作
|
||||
GetModel() IModel // 获取model
|
||||
ServiceAdd(ctx context.Context, req *AddReq) (data interface{}, err error) // 新增
|
||||
ServiceDelete(ctx context.Context, req *DeleteReq) (data sql.Result, err error) // 删除
|
||||
ServiceUpdate(ctx context.Context, req *UpdateReq) (data sql.Result, err error) // 修改
|
||||
ServiceInfo(ctx context.Context, req *InfoReq) (data interface{}, err error) // 详情
|
||||
ServiceList(ctx context.Context, req *ListReq) (data interface{}, err error) // 列表
|
||||
ServicePage(ctx context.Context, req *PageReq) (data interface{}, err error) // 分页
|
||||
ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改前的操作
|
||||
ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改后的操作
|
||||
GetModel() IModel // 获取model
|
||||
}
|
||||
type Service struct {
|
||||
Model IModel
|
||||
ListQueryOp *QueryOp
|
||||
PageQueryOp *QueryOp
|
||||
Where func(ctx context.Context) []g.Array // 删除修改定义条件
|
||||
InsertParam func(ctx context.Context) g.MapStrAny // Add时插入参数
|
||||
Before func(ctx context.Context) (err error) // CRUD前的操作
|
||||
InfoIgnoreProperty string // Info时忽略的字段,多个字段用逗号隔开
|
||||
@@ -104,16 +106,31 @@ func (s *Service) ServiceAdd(ctx context.Context, req *AddReq) (data interface{}
|
||||
}
|
||||
|
||||
// ServiceDelete 删除
|
||||
func (s *Service) ServiceDelete(ctx context.Context, req *DeleteReq) (data interface{}, err error) {
|
||||
func (s *Service) ServiceDelete(ctx context.Context, req *DeleteReq) (data sql.Result, err error) {
|
||||
ids := g.RequestFromCtx(ctx).Get("ids").Slice()
|
||||
m := g.DB(s.Model.GroupName()).Model(s.Model.TableName())
|
||||
if s.Where != nil {
|
||||
where := s.Where(ctx)
|
||||
if len(where) > 0 {
|
||||
for _, v := range where {
|
||||
if len(v) == 3 {
|
||||
if gconv.Bool(v[2]) {
|
||||
m.Where(v[0], v[1])
|
||||
}
|
||||
}
|
||||
if len(v) == 2 {
|
||||
m.Where(v[0], v[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
data, err = m.WhereIn("id", ids).Delete()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceUpdate 修改
|
||||
func (s *Service) ServiceUpdate(ctx context.Context, req *UpdateReq) (data interface{}, err error) {
|
||||
func (s *Service) ServiceUpdate(ctx context.Context, req *UpdateReq) (data sql.Result, err error) {
|
||||
r := g.RequestFromCtx(ctx)
|
||||
rmap := r.GetMap()
|
||||
if rmap["id"] == nil {
|
||||
@@ -136,6 +153,21 @@ func (s *Service) ServiceUpdate(ctx context.Context, req *UpdateReq) (data inter
|
||||
}
|
||||
m := DBM(s.Model)
|
||||
rmap["updateTime"] = nil
|
||||
if s.Where != nil {
|
||||
where := s.Where(ctx)
|
||||
if len(where) > 0 {
|
||||
for _, v := range where {
|
||||
if len(v) == 3 {
|
||||
if gconv.Bool(v[2]) {
|
||||
m.Where(v[0], v[1])
|
||||
}
|
||||
}
|
||||
if len(v) == 2 {
|
||||
m.Where(v[0], v[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//rmap["id"] = nil
|
||||
_, err = m.Data(rmap).Where("id", rmap["id"]).Update()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user