diff --git a/model/gmodel/fs_resource_logic.go b/model/gmodel/fs_resource_logic.go index 814257c5..e72f5ae3 100644 --- a/model/gmodel/fs_resource_logic.go +++ b/model/gmodel/fs_resource_logic.go @@ -22,13 +22,13 @@ func (p *FsResourceModel) FindOneById(ctx context.Context, resourceId string) (* return &resp, nil } -func (p *FsResourceModel) CreateOrUpdate(ctx context.Context, req *FsResource) (resp *FsResource, err error) { - rowBuilder := p.db.Table(p.name).WithContext(ctx) - if req.ResourceId != "" { - err = rowBuilder.Save(req).Error - } else { - err = rowBuilder.Create(req).Error - } +func (p *FsResourceModel) Create(ctx context.Context, req *FsResource) (resp *FsResource, err error) { + err = p.db.Table(p.name).WithContext(ctx).Create(req).Error + return req, err +} + +func (p *FsResourceModel) Update(ctx context.Context, req *FsResource) (resp *FsResource, err error) { + err = p.db.Table(p.name).WithContext(ctx).Where("resource_id =?", req.ResourceId).Save(req).Error return req, err } diff --git a/server/upload/internal/logic/uploadcallbacklogic.go b/server/upload/internal/logic/uploadcallbacklogic.go index 1f9147d5..7c15d85f 100644 --- a/server/upload/internal/logic/uploadcallbacklogic.go +++ b/server/upload/internal/logic/uploadcallbacklogic.go @@ -1,6 +1,7 @@ package logic import ( + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -10,6 +11,7 @@ import ( "fusenapi/server/upload/internal/types" "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" ) type UploadCallbackLogic struct { @@ -38,5 +40,31 @@ func NewUploadCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up func (l *UploadCallbackLogic) UploadCallback(req *types.UploadCallbackReq, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null + + resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) + err := resourceModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) { + resourceModelTS := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) + resourceInfo, err := resourceModelTS.FindOneById(ctx, req.ResourceId) + if err != nil { + return err + } + + var fsResource = &gmodel.FsResource{} + fsResource.ResourceId = req.ResourceId + fsResource.ResourceType = &req.ResourceType + fsResource.ResourceUrl = &req.ResourceUrl + fsResource.Metadata = &req.Metadata + fsResource.ApiType = &req.ApiType + if resourceInfo.ResourceId == "" { + _, err = resourceModelTS.Create(ctx, fsResource) + } else { + _, err = resourceModelTS.Update(ctx, fsResource) + } + return err + }) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,UploadCallback failed") + } return resp.SetStatus(basic.CodeOK) } diff --git a/server/upload/internal/logic/uploadfilesbackendlogic.go b/server/upload/internal/logic/uploadfilesbackendlogic.go index e37c9ec8..74a7ba00 100644 --- a/server/upload/internal/logic/uploadfilesbackendlogic.go +++ b/server/upload/internal/logic/uploadfilesbackendlogic.go @@ -185,7 +185,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, uploadUrl.ResourceUrl = url var version string = "0.0.1" var nowTime = time.Now() - _, err = resourceModel.CreateOrUpdate(l.ctx, &gmodel.FsResource{ + _, err = resourceModel.Create(l.ctx, &gmodel.FsResource{ ResourceId: resourceId, UserId: &userId, GuestId: &guestId,