-
Notifications
You must be signed in to change notification settings - Fork 1
/
many.go
145 lines (123 loc) 路 3.45 KB
/
many.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
package action
import (
"github.com/duxweb/go-fast/database"
"github.com/duxweb/go-fast/helper"
coreModel "github.com/duxweb/go-fast/models"
"github.com/duxweb/go-fast/response"
"github.com/labstack/echo/v4"
"github.com/samber/lo"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
"gorm.io/gorm/clause"
"strings"
)
func (t *Resources[T]) List(ctx echo.Context) error {
var err error
if t.initFun != nil {
err = t.initFun(t, ctx)
if err != nil {
return err
}
}
params, err := helper.Qs(ctx)
if err != nil {
return err
}
if t.Tree || !params.Get("pageSize").Exists() {
t.Pagination.Status = false
}
pageSize := 0
if t.Pagination.Status {
pageSize = lo.Ternary[int](params.Get("pageSize").Exists(), int(params.Get("pageSize").Uint()), t.Pagination.PageSize)
}
query := database.Gorm().Model(t.Model).Debug()
if params.Get("id").Exists() {
query = query.Where(t.Key+" = ?", params.Get("id"))
}
if params.Get("ids").Exists() {
ids := strings.Split(params.Get("ids").String(), ",")
ids = lo.Filter[string](ids, func(item string, index int) bool {
if item != "" {
return true
}
return false
})
query = query.Where(t.Key+" in ?", ids).Clauses(clause.OrderBy{
Expression: clause.Expr{SQL: "FIELD(" + t.Key + ",?)", Vars: []any{ids}, WithoutParentheses: true},
})
}
sorts := t.getSorts(params)
for k, v := range sorts {
query = query.Order(k + " " + cast.ToString(v))
}
if t.queryManyFun != nil {
query = t.queryManyFun(query, params, ctx)
}
if t.queryFun != nil {
query = t.queryFun(query, ctx)
}
models := make([]T, 0)
var pagination *coreModel.Pagination
if t.Pagination.Status {
pagination = coreModel.NewPagination(int(params.Get("page").Int()), pageSize)
err = query.Scopes(coreModel.Paginate(pagination)).Find(&models).Error
} else {
if t.Tree {
query = query.Preload(clause.Associations, coreModel.ChildrenPreload).Where("parent_id = 0")
}
err = query.Find(&models).Error
}
if err != nil {
return err
}
if t.manyAfterFun != nil {
models = t.manyAfterFun(models, params, ctx)
}
data := make([]map[string]any, 0)
meta := map[string]any{}
if t.TransformFun != nil {
data, meta = coreModel.FormatData[T](models, t.TransformFun, pagination)
}
data = t.filterData(data, t.IncludesMany, t.ExcludesMany)
return response.Send(ctx, response.Data{
Data: data,
Meta: meta,
})
}
type ManyCallFun[T any] func(data []T, params *gjson.Result, ctx echo.Context) []T
func (t *Resources[T]) ManyAfter(call ManyCallFun[T]) {
t.manyAfterFun = call
}
// getSorts 鑾峰彇鎺掑簭瑙勫垯
func (t *Resources[T]) getSorts(params *gjson.Result) map[string]string {
data := map[string]string{}
for key, value := range params.Map() {
if !strings.HasSuffix(key, "_sort") {
continue
}
if value.String() != "asc" && value.String() != "desc" {
continue
}
field := key[0 : len(key)-5]
data[field] = cast.ToString(value)
}
return data
}
func (t *Resources[T]) filterData(data []map[string]any, includes []string, excludes []string) []map[string]any {
result := make([]map[string]any, 0)
for _, item := range data {
datum := item
if len(includes) > 0 {
datum = lo.PickBy[string, any](item, func(key string, value any) bool {
return lo.IndexOf[string](includes, key) != -1
})
}
if len(excludes) > 0 {
datum = lo.PickBy[string, any](datum, func(key string, value any) bool {
return lo.IndexOf[string](excludes, key) == -1
})
}
result = append(result, datum)
}
return result
}