-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.go
187 lines (157 loc) · 3.98 KB
/
list.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cosy
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy/logger"
"github.com/uozi-tech/cosy/model"
"github.com/uozi-tech/cosy/settings"
"gorm.io/gorm"
"net/http"
"strings"
)
// GetPagingParams get paging params
func GetPagingParams(c *gin.Context) (page, offset, pageSize int) {
page = cast.ToInt(c.Query("page"))
if page == 0 {
page = 1
}
pageSize = cast.ToInt(settings.AppSettings.PageSize)
reqPageSize := c.Query("page_size")
if reqPageSize != "" {
pageSize = cast.ToInt(reqPageSize)
}
offset = (page - 1) * pageSize
return
}
// combineStdSelectorRequest combine std selector request
func (c *Ctx[T]) combineStdSelectorRequest() {
StdSelectorInitID := c.QueryArray("id[]")
if len(StdSelectorInitID) == 0 {
return
}
c.GormScope(func(tx *gorm.DB) *gorm.DB {
var sb strings.Builder
_, err := fmt.Fprintf(&sb, "%s IN ?", c.itemKey)
if err != nil {
logger.Error(err)
return tx
}
return tx.Where(sb.String(), StdSelectorInitID)
})
}
// result get result
func (c *Ctx[T]) result() (*gorm.DB, bool) {
c.resolvePreloadWithScope()
c.resolveJoinsWithScopes()
c.beforeExecuteHook()
var dbModel T
result := model.UseDB()
if cast.ToBool(c.Query("trash")) {
tableName := c.table
if c.table == "" {
stmt := &gorm.Statement{DB: model.UseDB()}
err := stmt.Parse(&dbModel)
if err != nil {
logger.Error(err)
return nil, false
}
tableName = stmt.Schema.Table
}
resolvedModel := model.GetResolvedModel[T]()
if deletedAt, ok := resolvedModel.Fields["DeletedAt"]; !ok ||
(deletedAt.DefaultValue == "" || deletedAt.DefaultValue == "null") {
result = result.Unscoped().Where(tableName + ".deleted_at IS NOT NULL")
} else {
result = result.Unscoped().Where(tableName + ".deleted_at != 0")
}
}
result = result.Model(&dbModel)
c.handleTable()
c.combineStdSelectorRequest()
c.applyGormScopes(result)
return result, true
}
// resolveData resolve data
func (c *Ctx[T]) resolveData(result *gorm.DB) (data any) {
// has scanner
if c.scan != nil {
return c.scan(result)
}
models := make([]*T, 0)
result.Find(&models)
// no transformer
if c.transformer == nil {
return models
}
// use transformer
transformed := make([]any, 0)
for k := range models {
transformed = append(transformed, c.transformer(models[k]))
}
return transformed
}
// ListAllData return list all data
func (c *Ctx[T]) ListAllData() (data any, ok bool) {
result, ok := c.result()
if !ok {
return nil, false
}
if !c.disableSortOrder {
result = result.Scopes(c.sortOrder)
}
data = c.resolveData(result)
return data, true
}
// PagingListData return paging list data
func (c *Ctx[T]) PagingListData() (*model.DataList, bool) {
result, ok := c.result()
if !ok {
return nil, false
}
scopesResult := result.Scopes(c.paginate)
if !c.disableSortOrder {
scopesResult = scopesResult.Scopes(c.sortOrder)
}
data := &model.DataList{}
data.Data = c.resolveData(scopesResult)
var totalRecords int64
delete(result.Statement.Clauses, "ORDER BY")
delete(result.Statement.Clauses, "LIMIT")
result.Count(&totalRecords)
page := cast.ToInt(c.Query("page"))
if page == 0 {
page = 1
}
pageSize := settings.AppSettings.PageSize
if reqPageSize := c.Query("page_size"); reqPageSize != "" {
pageSize = cast.ToInt(reqPageSize)
}
data.Pagination = model.Pagination{
Total: totalRecords,
PerPage: pageSize,
CurrentPage: page,
TotalPages: model.TotalPage(totalRecords, pageSize),
}
return data, true
}
// PagingList return paging list
func (c *Ctx[T]) PagingList() {
data, ok := c.PagingListData()
if ok {
if c.executedHook() {
return
}
c.JSON(http.StatusOK, data)
}
}
// EmptyPagingList return empty list
func (c *Ctx[T]) EmptyPagingList() {
pageSize := settings.AppSettings.PageSize
if reqPageSize := c.Query("page_size"); reqPageSize != "" {
pageSize = cast.ToInt(reqPageSize)
}
data := &model.DataList{Data: make([]any, 0)}
data.Pagination.PerPage = pageSize
c.JSON(http.StatusOK, data)
}