/
resource.go
72 lines (58 loc) · 2.1 KB
/
resource.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
package activity
import (
"fmt"
"strings"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
)
func prepareGetActivitiesDB(context *admin.Context, result interface{}, types ...string) *gorm.DB {
resourceID := getPrimaryKey(context, result)
db := context.GetDB().Order("id asc").Where("resource_id = ? AND resource_type = ?", resourceID, context.Resource.ToParam())
var inTypes, notInTypes []string
for _, t := range types {
if strings.HasPrefix(t, "-") {
notInTypes = append(notInTypes, strings.TrimPrefix(t, "-"))
} else {
inTypes = append(inTypes, t)
}
}
if len(inTypes) > 0 {
db = db.Where("type IN (?)", inTypes)
}
if len(notInTypes) > 0 {
db = db.Where("type NOT IN (?)", notInTypes)
}
return db
}
// GetActivities get activities for selected types
func GetActivities(context *admin.Context, result interface{}, types ...string) ([]QorActivity, error) {
var activities []QorActivity
db := prepareGetActivitiesDB(context, result, types...)
err := db.Find(&activities).Error
return activities, err
}
// GetActivitiesCount get activities's count for selected types
func GetActivitiesCount(context *admin.Context, result interface{}, types ...string) int {
var count int
prepareGetActivitiesDB(context, result, types...).Model(&QorActivity{}).Count(&count)
return count
}
// CreateActivity creates an activity for this context
func CreateActivity(context *admin.Context, activity *QorActivity, result interface{}) error {
var activityResource = context.Admin.GetResource("QorActivity")
// fill in necessary activity fields
activity.ResourceType = context.Resource.ToParam()
activity.ResourceID = getPrimaryKey(context, result)
if context.CurrentUser != nil {
activity.CreatorName = context.CurrentUser.DisplayName()
}
return activityResource.CallSave(activity, context.Context)
}
func getPrimaryKey(context *admin.Context, record interface{}) string {
db := context.GetDB()
var primaryValues []string
for _, field := range db.NewScope(record).PrimaryFields() {
primaryValues = append(primaryValues, fmt.Sprint(field.Field.Interface()))
}
return strings.Join(primaryValues, "::")
}