forked from qor-jjgo/qor
/
crud.go
153 lines (130 loc) · 5.01 KB
/
crud.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package resource
import (
"errors"
"fmt"
"strings"
"github.com/adderly/qor"
"github.com/adderly/qor/utils"
"github.com/adderly/roles"
"gorm.io/gorm"
)
// CallFindOne call find one method
func (res *Resource) CallFindOne(result interface{}, metaValues *MetaValues, context *qor.Context) error {
return res.FindOneHandler(result, metaValues, context)
}
// CallFindMany call find many method
func (res *Resource) CallFindMany(result interface{}, context *qor.Context) error {
return res.FindManyHandler(result, context)
}
// CallSave call save method
func (res *Resource) CallSave(result interface{}, context *qor.Context) error {
return res.SaveHandler(result, context)
}
// CallDelete call delete method
func (res *Resource) CallDelete(result interface{}, context *qor.Context) error {
return res.DeleteHandler(result, context)
}
// ToPrimaryQueryParams generate query params based on primary key, multiple primary value are linked with a comma
func (res *Resource) ToPrimaryQueryParams(primaryValue string, context *qor.Context) (string, []interface{}) {
if primaryValue != "" {
scope := utils.NewScope(res.Value)
// multiple primary fields
if len(res.PrimaryFields) > 1 {
if primaryValueStrs := strings.Split(primaryValue, ","); len(primaryValueStrs) == len(res.PrimaryFields) {
sqls := []string{}
primaryValues := []interface{}{}
for idx, field := range res.PrimaryFields {
sqls = append(sqls, fmt.Sprintf("%v.%v = ?", scope.Table, field.DBName))
primaryValues = append(primaryValues, primaryValueStrs[idx])
}
return strings.Join(sqls, " AND "), primaryValues
}
}
// fallback to first configured primary field
if len(res.PrimaryFields) > 0 {
return fmt.Sprintf("%v.%v = ?", scope.Table, res.PrimaryFields[0].DBName), []interface{}{primaryValue}
}
// if no configured primary fields found
if res.primaryField != nil {
return fmt.Sprintf("%v.%v = ?", res.Name, res.primaryField.DBName), []interface{}{primaryValue}
}
}
return "", []interface{}{}
}
// ToPrimaryQueryParamsFromMetaValue generate query params based on MetaValues
func (res *Resource) ToPrimaryQueryParamsFromMetaValue(metaValues *MetaValues, context *qor.Context) (string, []interface{}) {
var (
sqls []string
primaryValues []interface{}
scope = utils.NewScope(res.Value)
)
if metaValues != nil {
for _, field := range res.PrimaryFields {
if metaField := metaValues.Get(field.Name); metaField != nil {
sqls = append(sqls, fmt.Sprintf("%v.%v = ?", scope.Table, field.DBName))
primaryValues = append(primaryValues, utils.ToString(metaField.Value))
}
}
}
return strings.Join(sqls, " AND "), primaryValues
}
func (res *Resource) findOneHandler(result interface{}, metaValues *MetaValues, context *qor.Context) error {
if res.HasPermission(roles.Read, context) {
var (
primaryQuerySQL string
primaryParams []interface{}
)
if metaValues == nil {
primaryQuerySQL, primaryParams = res.ToPrimaryQueryParams(context.ResourceID, context)
} else {
primaryQuerySQL, primaryParams = res.ToPrimaryQueryParamsFromMetaValue(metaValues, context)
}
if primaryQuerySQL != "" {
if metaValues != nil {
if destroy := metaValues.Get("_destroy"); destroy != nil {
if fmt.Sprint(destroy.Value) != "0" && res.HasPermission(roles.Delete, context) {
context.GetDB().Delete(result, append([]interface{}{primaryQuerySQL}, primaryParams...)...)
return ErrProcessorSkipLeft
}
}
}
return context.GetDB().First(result, append([]interface{}{primaryQuerySQL}, primaryParams...)...).Error
}
return errors.New("failed to find")
}
return roles.ErrPermissionDenied
}
func (res *Resource) findManyHandler(result interface{}, context *qor.Context) error {
if res.HasPermission(roles.Read, context) {
db := context.GetDB()
if getTotalCountValue, ok := db.Get("qor:getting_total_count"); ok {
if getTotalCount, getTotalCountOk := getTotalCountValue.(bool); getTotalCountOk && getTotalCount {
if err := context.GetDB().Count(result.(*int64)).Error; err != nil {
return err
}
return nil
}
}
return context.GetDB().Set("gorm:order_by_primary_key", "DESC").Find(result).Error
}
return roles.ErrPermissionDenied
}
func (res *Resource) saveHandler(result interface{}, context *qor.Context) error {
if (utils.PrimaryKeyZero(result) &&
res.HasPermission(roles.Create, context)) || // has create permission
res.HasPermission(roles.Update, context) { // has update permission
return context.GetDB().Save(result).Error
}
return roles.ErrPermissionDenied
}
func (res *Resource) deleteHandler(result interface{}, context *qor.Context) error {
if res.HasPermission(roles.Delete, context) {
if primaryQuerySQL, primaryParams := res.ToPrimaryQueryParams(context.ResourceID, context); primaryQuerySQL != "" {
if context.GetDB().First(result, append([]interface{}{primaryQuerySQL}, primaryParams...)...).Error != gorm.ErrRecordNotFound {
return context.GetDB().Delete(result).Error
}
}
return gorm.ErrRecordNotFound
}
return roles.ErrPermissionDenied
}