forked from PolarPanda611/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_patch.go
More file actions
121 lines (113 loc) · 3.73 KB
/
Copy pathservice_patch.go
File metadata and controls
121 lines (113 loc) · 3.73 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
package trinity
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strconv"
"github.com/jinzhu/gorm"
)
// PatchHandler : List method
func PatchHandler(r *PatchMixin) {
// if Callback BeforeGet registered , run before get callback
if IsFuncInited(r.ViewSetRunTime.BeforePatch) {
r.ViewSetRunTime.BeforePatch(r.ViewSetRunTime)
}
// if Callback Get registered , run before get callback, if not , run default get callback
if IsFuncInited(r.ViewSetRunTime.Patch) {
r.ViewSetRunTime.Patch(r.ViewSetRunTime)
}
// if Callback AfterGet registered , run before get callback, if not , run default get callback
if IsFuncInited(r.ViewSetRunTime.AfterPatch) {
r.ViewSetRunTime.AfterPatch(r.ViewSetRunTime)
}
if r.ViewSetRunTime.RealError == nil {
r.ViewSetRunTime.Response()
}
}
// DefaultPatchCallback : PATCH method
func DefaultPatchCallback(r *ViewSetRunTime) {
respBytes, err := ioutil.ReadAll(r.Gcontext.Request.Body)
if err != nil {
r.HandleResponse(400, nil, err, ErrResolveDataFailed)
return
}
requestbodyMap := make(map[string]interface{})
if err := json.Unmarshal(respBytes, &requestbodyMap); err != nil {
r.HandleResponse(400, nil, err, ErrResolveDataFailed)
return
}
id, err := strconv.ParseInt(r.Gcontext.Params.ByName("id"), 10, 64)
if err != nil {
r.HandleResponse(400, nil, err, ErrUpdateDataFailed)
return
}
delete(requestbodyMap, "key")
if r.EnableChangeLog {
searchOldValue := reflect.New(reflect.ValueOf(r.ModelSerializer).Elem().Type()).Interface()
if err := r.Db.Scopes(
r.DBFilterBackend,
FilterByParam(id),
FilterByFilter(r.Gcontext, r.FilterByList, r.FilterCustomizeFunc),
FilterBySearch(r.Gcontext, r.SearchingByList),
).Table(r.ResourceTableName).First(searchOldValue).Error; err != nil {
r.HandleResponse(400, nil, err, ErrUpdateDataFailed)
return
}
v := reflect.ValueOf(searchOldValue).Elem()
k := v.Type()
oldDataMap := make(map[string]string)
for i := 0; i < v.NumField(); i++ {
key := gorm.ToColumnName(k.Field(i).Name)
val := v.Field(i)
oldDataMap[key] = fmt.Sprint(val)
model, ok := val.Interface().(Model)
if ok {
oldDataMap["d_version"] = model.DVersion
}
}
userID := r.Gcontext.GetInt64("UserID")
for k, v := range requestbodyMap {
oldValue := oldDataMap[k]
fmt.Println(fmt.Sprintf("key: %v oldvaluetype : %v oldvalue: %v , newvaluetype : %v newvalue: %v ", k, reflect.ValueOf(oldValue).Kind(), oldValue, reflect.ValueOf(v).Kind(), fmt.Sprint(reflect.ValueOf(v))))
newValue := fmt.Sprint(reflect.ValueOf(v))
if oldValue == newValue {
continue
}
changeLog := AppChangelog{
Logmodel: Logmodel{CreateUserID: userID},
Resource: r.ResourceTableName,
Type: "Update",
Column: k,
OldValue: oldValue,
NewValue: newValue,
DVersion: oldDataMap["d_version"],
TraceID: r.Gcontext.GetString("TraceID"),
ResourceKey: r.Gcontext.Params.ByName("id"),
}
if err := r.Db.Create(&changeLog).Error; err != nil {
r.HandleResponse(400, nil, err, ErrUpdateDataFailed)
return
}
}
}
if r.EnableVersionControl {
}
updateQuery := r.Db.Set("gorm:save_associations", false).Scopes(
r.DBFilterBackend,
FilterByParam(id),
FilterByFilter(r.Gcontext, r.FilterByList, r.FilterCustomizeFunc),
FilterBySearch(r.Gcontext, r.SearchingByList),
DataVersionFilter(requestbodyMap["d_version"], r.EnableDataVersion),
).Table(r.ResourceTableName).Updates(requestbodyMap)
if err := updateQuery.Error; err != nil {
r.HandleResponse(400, nil, err, ErrUpdateDataFailed)
return
}
if effectNum := updateQuery.RowsAffected; effectNum != 1 {
r.HandleResponse(400, nil, ErrUpdateZeroAffected, ErrUpdateDataFailed)
return
}
r.HandleResponse(200, "Updated Successfully", nil, nil)
return
}