/
crud.go
105 lines (91 loc) · 3.41 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
package resource
import (
"errors"
"fmt"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/utils"
"github.com/qor/roles"
)
func (res *Resource) findOneHandler(result interface{}, metaValues *MetaValues, context *qor.Context) error {
if res.HasPermission(roles.Read, context) {
var (
scope = context.GetDB().NewScope(res.Value)
primaryField = res.PrimaryField()
primaryKey string
)
if metaValues == nil {
primaryKey = context.ResourceID
} else if primaryField == nil {
return nil
} else if id := metaValues.Get(primaryField.Name); id != nil {
primaryKey = utils.ToString(id.Value)
}
if primaryKey != "" {
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, fmt.Sprintf("%v = ?", scope.Quote(primaryField.DBName)), primaryKey)
return ErrProcessorSkipLeft
}
}
}
return context.GetDB().First(result, fmt.Sprintf("%v.%v = ?", scope.QuotedTableName(), scope.Quote(primaryField.DBName)), primaryKey).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 _, ok := db.Get("qor:getting_total_count"); ok {
return context.GetDB().Count(result).Error
} else {
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 (context.GetDB().NewScope(result).PrimaryKeyZero() &&
res.HasPermission(roles.Create, context)) || // has create permission
res.HasPermission(roles.Update, context) { // has update permission
results := context.GetDB().Save(result)
if results.RowsAffected == 0 {
primaryField := context.GetDB().NewScope(result).PrimaryField()
// if primary field has value and it is not a auto increment field, then create it if nothing updated
if _, ok := primaryField.TagSettings["AUTO_INCREMENT"]; !primaryField.IsBlank && !ok {
return context.GetDB().Create(result).Error
}
}
return results.Error
}
return roles.ErrPermissionDenied
}
func (res *Resource) deleteHandler(result interface{}, context *qor.Context) error {
if res.HasPermission(roles.Delete, context) {
scope := context.GetDB().NewScope(res.Value)
if !context.GetDB().First(result, fmt.Sprintf("%v = ?", scope.Quote(res.PrimaryDBName())), context.ResourceID).RecordNotFound() {
return context.GetDB().Delete(result).Error
}
return gorm.ErrRecordNotFound
}
return roles.ErrPermissionDenied
}
// 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)
}