-
Notifications
You must be signed in to change notification settings - Fork 22
/
create_cloud_file_logic.go
64 lines (51 loc) · 1.67 KB
/
create_cloud_file_logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cloudfile
import (
"context"
"github.com/suyuan32/simple-admin-file/ent"
"github.com/zeromicro/go-zero/core/errorx"
"github.com/suyuan32/simple-admin-file/internal/svc"
"github.com/suyuan32/simple-admin-file/internal/types"
"github.com/suyuan32/simple-admin-file/internal/utils/dberrorhandler"
"github.com/suyuan32/simple-admin-common/i18n"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateCloudFileLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewCreateCloudFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCloudFileLogic {
return &CreateCloudFileLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *CreateCloudFileLogic) CreateCloudFile(req *types.CloudFileInfo) (*types.BaseMsgResp, error) {
// check storage provider exist
_, err := l.svcCtx.DB.StorageProvider.Get(l.ctx, *req.ProviderId)
switch {
case ent.IsNotFound(err):
return nil, errorx.NewCodeInvalidArgumentError("storage_provider.StorageProviderNotExist")
case err != nil:
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
}
query := l.svcCtx.DB.CloudFile.Create().
SetNotNilState(req.State).
SetNotNilName(req.Name).
SetNotNilURL(req.Url).
SetNotNilSize(req.Size).
SetNotNilFileType(req.FileType).
SetNotNilUserID(req.UserId)
if req.ProviderId != nil {
query = query.SetStorageProvidersID(*req.ProviderId)
}
if req.TagIds != nil {
query = query.AddTagIDs(req.TagIds...)
}
_, err = query.Save(l.ctx)
if err != nil {
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
}
return &types.BaseMsgResp{Msg: l.svcCtx.Trans.Trans(l.ctx, i18n.CreateSuccess)}, nil
}