forked from gin-admin/gin-admin-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_impl.go
More file actions
133 lines (109 loc) · 3.31 KB
/
Copy pathmodel_impl.go
File metadata and controls
133 lines (109 loc) · 3.31 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
123
124
125
126
127
128
129
130
131
132
133
package generate
import (
"context"
"fmt"
"github.com/LyricTian/gin-admin-cli/util"
)
func getModelImplFileName(dir, name string) string {
fullname := fmt.Sprintf("%s/internal/app/model/impl/gorm/internal/model/m_%s.go", dir, util.ToLowerUnderlinedNamer(name))
return fullname
}
// 生成model实现文件
func genModelImpl(ctx context.Context, pkgName, dir, name, comment string) error {
data := map[string]interface{}{
"PkgName": pkgName,
"Name": name,
"PluralName": util.ToPlural(name),
"Comment": comment,
}
buf, err := execParseTpl(modelImplTpl, data)
if err != nil {
return err
}
fullname := getModelImplFileName(dir, name)
err = createFile(ctx, fullname, buf)
if err != nil {
return err
}
fmt.Printf("文件[%s]写入成功\n", fullname)
return execGoFmt(fullname)
}
const modelImplTpl = `
package model
import (
"context"
"{{.PkgName}}/internal/app/errors"
"{{.PkgName}}/internal/app/model/impl/gorm/internal/entity"
"{{.PkgName}}/internal/app/schema"
"github.com/jinzhu/gorm"
)
// New{{.Name}} 创建{{.Comment}}存储实例
func New{{.Name}}(db *gorm.DB) *{{.Name}} {
return &{{.Name}}{db}
}
// {{.Name}} {{.Comment}}存储
type {{.Name}} struct {
db *gorm.DB
}
func (a *{{.Name}}) getQueryOption(opts ...schema.{{.Name}}QueryOptions) schema.{{.Name}}QueryOptions {
var opt schema.{{.Name}}QueryOptions
if len(opts) > 0 {
opt = opts[0]
}
return opt
}
// Query 查询数据
func (a *{{.Name}}) Query(ctx context.Context, params schema.{{.Name}}QueryParam, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}QueryResult, error) {
db := entity.Get{{.Name}}DB(ctx, a.db)
db = db.Order("id DESC")
opt := a.getQueryOption(opts...)
var list entity.{{.PluralName}}
pr, err := WrapPageQuery(ctx, db, opt.PageParam, &list)
if err != nil {
return nil, errors.WithStack(err)
}
qr := &schema.{{.Name}}QueryResult{
PageResult: pr,
Data: list.ToSchema{{.PluralName}}(),
}
return qr, nil
}
// Get 查询指定数据
func (a *{{.Name}}) Get(ctx context.Context, recordID string, opts ...schema.{{.Name}}QueryOptions) (*schema.{{.Name}}, error) {
db := entity.Get{{.Name}}DB(ctx, a.db).Where("record_id=?", recordID)
var item entity.{{.Name}}
ok, err := FindOne(ctx, db, &item)
if err != nil {
return nil, errors.WithStack(err)
} else if !ok {
return nil, nil
}
return item.ToSchema{{.Name}}(), nil
}
// Create 创建数据
func (a *{{.Name}}) Create(ctx context.Context, item schema.{{.Name}}) error {
{{.Name}} := entity.Schema{{.Name}}(item).To{{.Name}}()
result := entity.Get{{.Name}}DB(ctx, a.db).Create({{.Name}})
if err := result.Error; err != nil {
return errors.WithStack(err)
}
return nil
}
// Update 更新数据
func (a *{{.Name}}) Update(ctx context.Context, recordID string, item schema.{{.Name}}) error {
{{.Name}} := entity.Schema{{.Name}}(item).To{{.Name}}()
result := entity.Get{{.Name}}DB(ctx, a.db).Where("record_id=?", recordID).Omit("record_id", "creator").Updates({{.Name}})
if err := result.Error; err != nil {
return errors.WithStack(err)
}
return nil
}
// Delete 删除数据
func (a *{{.Name}}) Delete(ctx context.Context, recordID string) error {
result := entity.Get{{.Name}}DB(ctx, a.db).Where("record_id=?", recordID).Delete(entity.{{.Name}}{})
if err := result.Error; err != nil {
return errors.WithStack(err)
}
return nil
}
`