/
repo_impl_gorm.go
136 lines (106 loc) · 3.4 KB
/
repo_impl_gorm.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package generate
import (
"context"
"fmt"
"github.com/gin-admin/gin-admin-cli/v5/util"
)
func getModelImplGormFileName(dir, name string) string {
name = util.ToLowerUnderlinedNamer(name)
fullname := fmt.Sprintf("%s/internal/app/dao/%s/%s.repo.go", dir, name, name)
return fullname
}
func genModelImplGorm(ctx context.Context, pkgName, dir, name, comment string, excludeStatus, excludeCreate bool) error {
data := map[string]interface{}{
"PkgName": pkgName,
"Name": name,
"PluralName": util.ToPlural(name),
"Comment": comment,
"UnderLineName": util.ToLowerUnderlinedNamer(name),
"IncludeStatus": !excludeStatus,
}
buf, err := execParseTpl(daoGromRepoTpl, data)
if err != nil {
return err
}
fullname := getModelImplGormFileName(dir, name)
err = createFile(ctx, fullname, buf)
if err != nil {
return err
}
fmt.Printf("File write success: %s\n", fullname)
return execGoFmt(fullname)
}
const daoGromRepoTpl = `
package {{.UnderLineName}}
import (
"context"
"github.com/google/wire"
"gorm.io/gorm"
"{{.PkgName}}/internal/app/dao/util"
"{{.PkgName}}/internal/app/schema"
"{{.PkgName}}/pkg/errors"
)
// Injection wire
var {{.Name}}Set = wire.NewSet(wire.Struct(new({{.Name}}Repo), "*"))
type {{.Name}}Repo struct {
DB *gorm.DB
}
func (a *{{.Name}}Repo) getQueryOption(opts ...schema.{{.Name}}QueryOptions) schema.{{.Name}}QueryOptions {
var opt schema.{{.Name}}QueryOptions
if len(opts) > 0 {
opt = opts[0]
}
return opt
}
func (a *{{.Name}}Repo) Query(ctx context.Context, params schema.{{.Name}}QueryParam, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}QueryResult, error) {
opt := a.getQueryOption(opts...)
db := Get{{.Name}}DB(ctx, a.DB)
// TODO: Your where condition code here...
if len(opt.SelectFields) > 0 {
db = db.Select(opt.SelectFields)
}
if len(opt.OrderFields) > 0 {
db = db.Order(util.ParseOrder(opt.OrderFields))
}
var list {{.PluralName}}
pr, err := util.WrapPageQuery(ctx, db, params.PaginationParam, &list)
if err != nil {
return nil, errors.WithStack(err)
}
qr := &schema.{{.Name}}QueryResult{
PageResult: pr,
Data: list.ToSchema{{.PluralName}}(),
}
return qr, nil
}
func (a *{{.Name}}Repo) Get(ctx context.Context, id uint64, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}, error) {
var item {{.Name}}
ok, err := util.FindOne(ctx, Get{{.Name}}DB(ctx, a.DB).Where("id=?", id), &item)
if err != nil {
return nil, errors.WithStack(err)
} else if !ok {
return nil, nil
}
return item.ToSchema{{.Name}}(), nil
}
func (a *{{.Name}}Repo) Create(ctx context.Context, item schema.{{.Name}}) error {
eitem := Schema{{.Name}}(item).To{{.Name}}()
result := Get{{.Name}}DB(ctx, a.DB).Create(eitem)
return errors.WithStack(result.Error)
}
func (a *{{.Name}}Repo) Update(ctx context.Context, id uint64, item schema.{{.Name}}) error {
eitem := Schema{{.Name}}(item).To{{.Name}}()
result := Get{{.Name}}DB(ctx, a.DB).Where("id=?", id).Updates(eitem)
return errors.WithStack(result.Error)
}
func (a *{{.Name}}Repo) Delete(ctx context.Context, id uint64) error {
result := Get{{.Name}}DB(ctx, a.DB).Where("id=?", id).Delete({{.Name}}{})
return errors.WithStack(result.Error)
}
{{if .IncludeStatus}}
func (a *{{.Name}}Repo) UpdateStatus(ctx context.Context, id uint64, status int) error {
result := Get{{.Name}}DB(ctx, a.DB).Where("id=?", id).Update("status", status)
return errors.WithStack(result.Error)
}
{{end}}
`