forked from gin-admin/gin-admin-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbll_impl.go
More file actions
122 lines (99 loc) · 2.82 KB
/
Copy pathbll_impl.go
File metadata and controls
122 lines (99 loc) · 2.82 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package generate
import (
"context"
"fmt"
"github.com/LyricTian/gin-admin-cli/util"
)
func getBllImplFileName(dir, name string) string {
fullname := fmt.Sprintf("%s/internal/app/bll/impl/internal/b_%s.go", dir, util.ToLowerUnderlinedNamer(name))
return fullname
}
// 生成bll实现文件
func genBllImpl(ctx context.Context, pkgName, dir, name, comment string) error {
data := map[string]interface{}{
"PkgName": pkgName,
"Name": name,
"Comment": comment,
}
buf, err := execParseTpl(bllImplTpl, data)
if err != nil {
return err
}
fullname := getBllImplFileName(dir, name)
err = createFile(ctx, fullname, buf)
if err != nil {
return err
}
fmt.Printf("文件[%s]写入成功\n", fullname)
return execGoFmt(fullname)
}
const bllImplTpl = `
package internal
import (
"context"
"{{.PkgName}}/internal/app/errors"
"{{.PkgName}}/internal/app/model"
"{{.PkgName}}/internal/app/schema"
"{{.PkgName}}/pkg/util"
)
// New{{.Name}} 创建{{.Comment}}
func New{{.Name}}(m{{.Name}} model.I{{.Name}}) *{{.Name}} {
return &{{.Name}}{
{{.Name}}Model: m{{.Name}},
}
}
// {{.Name}} {{.Comment}}业务逻辑
type {{.Name}} struct {
{{.Name}}Model model.I{{.Name}}
}
// Query 查询数据
func (a *{{.Name}}) Query(ctx context.Context, params schema.{{.Name}}QueryParam, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}QueryResult, error) {
return a.{{.Name}}Model.Query(ctx, params, opts...)
}
// Get 查询指定数据
func (a *{{.Name}}) Get(ctx context.Context, recordID string, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}, error) {
item, err := a.{{.Name}}Model.Get(ctx, recordID, opts...)
if err != nil {
return nil, err
} else if item == nil {
return nil, errors.ErrNotFound
}
return item, nil
}
func (a *{{.Name}}) getUpdate(ctx context.Context, recordID string) (*schema.{{.Name}}, error) {
return a.Get(ctx, recordID)
}
// Create 创建数据
func (a *{{.Name}}) Create(ctx context.Context, item schema.{{.Name}}) (*schema.{{.Name}}, error) {
item.RecordID = util.MustUUID()
err := a.{{.Name}}Model.Create(ctx, item)
if err != nil {
return nil, err
}
return a.getUpdate(ctx, item.RecordID)
}
// Update 更新数据
func (a *{{.Name}}) Update(ctx context.Context, recordID string, item schema.{{.Name}}) (*schema.{{.Name}}, error) {
oldItem, err := a.{{.Name}}Model.Get(ctx, recordID)
if err != nil {
return nil, err
} else if oldItem == nil {
return nil, errors.ErrNotFound
}
err = a.{{.Name}}Model.Update(ctx, recordID, item)
if err != nil {
return nil, err
}
return a.getUpdate(ctx, recordID)
}
// Delete 删除数据
func (a *{{.Name}}) Delete(ctx context.Context, recordID string) error {
oldItem, err := a.{{.Name}}Model.Get(ctx, recordID)
if err != nil {
return err
} else if oldItem == nil {
return errors.ErrNotFound
}
return a.{{.Name}}Model.Delete(ctx, recordID)
}
`