-
Notifications
You must be signed in to change notification settings - Fork 12
/
callbacks.go
91 lines (80 loc) · 2.98 KB
/
callbacks.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
package sorting
import (
"fmt"
"reflect"
"strings"
"github.com/jinzhu/gorm"
"github.com/qor/l10n"
)
func initalizePosition(scope *gorm.Scope) {
if !scope.HasError() {
if _, ok := scope.Value.(sortingInterface); ok {
var lastPosition int
scope.NewDB().Set("l10n:mode", "locale").Model(modelValue(scope.Value)).Select("position").Order("position DESC").Limit(1).Row().Scan(&lastPosition)
scope.SetColumn("Position", lastPosition+1)
}
}
}
func reorderPositions(scope *gorm.Scope) {
if !scope.HasError() {
if _, ok := scope.Value.(sortingInterface); ok {
table := scope.TableName()
var additionalSQL []string
var additionalValues []interface{}
// with l10n
if locale, ok := scope.DB().Get("l10n:locale"); ok && locale.(string) != "" && l10n.IsLocalizable(scope) {
additionalSQL = append(additionalSQL, "language_code = ?")
additionalValues = append(additionalValues, locale)
}
additionalValues = append(additionalValues, additionalValues...)
// with soft delete
if scope.HasColumn("DeletedAt") {
additionalSQL = append(additionalSQL, "deleted_at IS NULL")
}
var sql string
if len(additionalSQL) > 0 {
sql = fmt.Sprintf("UPDATE %v SET position = (SELECT COUNT(pos) + 1 FROM (SELECT DISTINCT(position) AS pos FROM %v WHERE %v) AS t2 WHERE t2.pos < %v.position) WHERE %v", table, table, strings.Join(additionalSQL, " AND "), table, strings.Join(additionalSQL, " AND "))
} else {
sql = fmt.Sprintf("UPDATE %v SET position = (SELECT COUNT(pos) + 1 FROM (SELECT DISTINCT(position) AS pos FROM %v) AS t2 WHERE t2.pos < %v.position)", table, table, table)
}
if scope.NewDB().Exec(sql, additionalValues...).Error == nil {
// Create Publish Event
createPublishEvent(scope.DB(), scope.Value)
}
}
}
}
func modelValue(value interface{}) interface{} {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
if reflectValue.IsValid() {
typ := reflectValue.Type()
if reflectValue.Kind() == reflect.Slice {
typ = reflectValue.Type().Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
}
return reflect.New(typ).Interface()
}
return nil
}
func beforeQuery(scope *gorm.Scope) {
modelValue := modelValue(scope.Value)
if _, ok := modelValue.(sortingDescInterface); ok {
scope.Search.Order("position desc")
} else if _, ok := modelValue.(sortingInterface); ok {
scope.Search.Order("position")
}
}
// RegisterCallbacks register callbacks into gorm db instance
func RegisterCallbacks(db *gorm.DB) {
if db.Callback().Create().Get("sorting:initalize_position") == nil {
db.Callback().Create().Before("gorm:create").Register("sorting:initalize_position", initalizePosition)
}
if db.Callback().Delete().Get("sorting:reorder_positions") == nil {
db.Callback().Delete().After("gorm:after_delete").Register("sorting:reorder_positions", reorderPositions)
}
if db.Callback().Query().Get("sorting:sort_by_position") == nil {
db.Callback().Query().Before("gorm:query").Register("sorting:sort_by_position", beforeQuery)
}
}