From ba0543ffb34905120839a0395202a9ec08ea2cb1 Mon Sep 17 00:00:00 2001 From: chenyang Date: Thu, 21 Dec 2023 22:22:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/logic/video/video.go | 40 ++++++++++++++++++++++++++++++++++------ app/service/video.go | 2 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/logic/video/video.go b/app/logic/video/video.go index d481de5..a363c71 100644 --- a/app/logic/video/video.go +++ b/app/logic/video/video.go @@ -2,6 +2,8 @@ package video import ( "context" + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/text/gstr" v1 "xgit.pub/module/cms/app/api/video/v1" "xgit.pub/module/cms/app/dao" @@ -83,25 +85,51 @@ func (s *sVideo) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetLi // Create 创建 func (s *sVideo) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) { + err = dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = dao.Video.Ctx(ctx).InsertAndGetId(req) + return err + }) return } // Update 更新 -func (s *sVideo) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) { - return +func (s *sVideo) Update(ctx context.Context, req *v1.UpdateReq) (err error) { + return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + if req.Id == 0 { + return gerror.New("编号不能为空") + } + _, err = dao.Video.Ctx(ctx).OmitEmpty().Data(req).Where(dao.Video.Columns().Id, req.Id).Update() + return err + }) } // Delete 删除 -func (s *sVideo) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) { - return +func (s *sVideo) Delete(ctx context.Context, req *v1.DeleteReq) (err error) { + return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + if req.Id < 1 { + return gerror.New("编号不能为空") + } + _, err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Delete() + return nil + }) } // BatchDelete 批量删除 -func (s *sVideo) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error) { - return +func (s *sVideo) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (err error) { + return dao.Video.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + if len(req.Ids) < 1 { + return gerror.New("编号不能为空") + } + _, err = dao.Video.Ctx(ctx).WhereIn(dao.Video.Columns().Id, req.Ids).Delete() + return nil + }) } // Get 获取 func (s *sVideo) Get(ctx context.Context, req *v1.GetReq) (res *v1.GetRes, err error) { + err = dao.Video.Ctx(ctx).Where(dao.Video.Columns().Id, req.Id).Scan(&res) + if err != nil { + err = gerror.New("Video not found") + } return } diff --git a/app/service/video.go b/app/service/video.go index 3c24614..1b0a3a1 100644 --- a/app/service/video.go +++ b/app/service/video.go @@ -17,7 +17,7 @@ type ( // Create 创建 Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) // Update 更新 - Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) + Update(ctx context.Context, req *v1.UpdateReq) (err error) // Delete 删除 Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) // BatchDelete 批量删除