-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.go
260 lines (237 loc) · 5.43 KB
/
obj.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package rich
import (
"encoding/json"
"errors"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/labstack/echo"
"github.com/xuender/go-utils"
)
// Obj 基础对象
type Obj struct {
ID utils.ID `json:"id"` // 主键
Name string `json:"name" validate:"required,lte=50"` // 名称 必填 最长50
Pinyin string `json:"pinyin"` // 拼音
Note string `json:"note" validate:"lte=100"` // 备注 最长100
Ca time.Time `json:"ca"` // 创建时间
Da time.Time `json:"da,omitempty"` // 删除时间
}
// Delete 删除对象
func (o *Obj) Delete() {
o.Da = time.Now()
}
// IsDelete 对象是否删除
func (o *Obj) IsDelete() bool {
return o.Da.Year() > 1
}
// Deleter 删除器
type Deleter interface {
IsDelete() bool
Delete()
}
// Binder 对象绑定
type Binder interface {
// Bind 绑定数据
Bind(c echo.Context) error
}
// Puter 对象修改
type Puter interface {
// BeforePut 修改前
BeforePut(id utils.ID)
}
// Poster 新对象操作
type Poster interface {
// BeforePost 保存前
BeforePost(key byte) utils.ID
}
// Matcher 对象比较
type Matcher interface {
Match(txt string) bool
}
// Includeser 包含
type Includeser interface {
Includes(tags []string) bool
}
// BeforePut 修改对象
func (o *Obj) BeforePut(id utils.ID) {
o.Name = strings.Trim(o.Name, " ")
o.ID = id
o.Pinyin = py(o.Name)
}
// BeforePost 对象新建前
func (o *Obj) BeforePost(key byte) utils.ID {
o.Name = strings.Trim(o.Name, " ")
o.ID = utils.NewID(key)
o.Ca = time.Now()
o.Pinyin = py(o.Name)
return o.ID
}
// ObjPut 对象修改
func (w *Web) ObjPut(c echo.Context, p Puter, key byte, bind func() error, check func() error) error {
idstr := c.Param("id")
if idstr == "" {
return errors.New("id为空")
}
id := new(utils.ID)
err := id.Parse(idstr)
if err != nil {
return err
}
if id[0] != key {
return errors.New("前缀错误")
}
w.Get(id[:], p)
if err := bind(); err != nil {
return err
}
p.BeforePut(*id)
if check != nil {
if err := check(); err != nil {
return err
}
}
w.Put(id[:], p)
return c.JSON(http.StatusOK, p)
}
// ObjPost 对象新增
func (w *Web) ObjPost(c echo.Context, p Poster, key byte, bind func() error, check func() error) error {
if err := bind(); err != nil {
return err
}
id := p.BeforePost(key)
if check != nil {
if err := check(); err != nil {
return err
}
}
w.Put(id[:], p)
return c.JSON(http.StatusCreated, p)
}
// ObjLoad 对象加载
func (w *Web) ObjLoad(c echo.Context, o interface{}) error {
id := new(utils.ID)
err := id.Parse(c.Param("id"))
if err != nil {
return err
}
return w.Get(id[:], o)
}
// ObjSearch 对象搜索
func (w *Web) ObjSearch(c echo.Context, i interface{}) (interface{}, error) {
txt := c.QueryParam("search")
if txt != "" {
return utils.Filter(i, func(m Matcher) bool {
return m.Match(txt)
})
}
return i, nil
}
// ObjSelect 对象选择
func (w *Web) ObjSelect(c echo.Context, i interface{}) (interface{}, error) {
str := c.QueryParam("tags")
if str != "" {
tags := []string{}
json.Unmarshal([]byte(str), &tags)
return utils.Filter(i, func(m Includeser) bool {
return m.Includes(tags)
})
}
return i, nil
}
// ObjPaging 分页
func (w *Web) ObjPaging(c echo.Context, i interface{}) error {
rv := reflect.ValueOf(i)
err := errors.New("分页对象必须是slice或array")
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return err
}
s := c.QueryParam("size")
p := c.QueryParam("page")
size := 20
page := 0
if s != "" {
size, err = strconv.Atoi(s)
if err != nil {
return err
}
}
if p != "" {
page, err = strconv.Atoi(p)
if err != nil {
return err
}
}
start := page * size
if start > rv.Len() {
return c.JSON(http.StatusOK, []int{})
}
end := start + size
if end > rv.Len() {
end = rv.Len()
}
t := rv.Type().Elem()
out := reflect.MakeSlice(reflect.SliceOf(t), 0, end-start)
for i := start; i < end; i++ {
out = reflect.Append(out, rv.Index(i))
}
return c.JSON(http.StatusOK, Paging{Data: out.Interface(), Total: rv.Len()})
}
// ObjGet 对象获取
func (w *Web) ObjGet(c echo.Context, o interface{}) error {
if err := w.ObjLoad(c, o); err != nil {
return err
}
return c.JSON(http.StatusOK, o)
}
// ObjDelete 对象删除
func (w *Web) ObjDelete(c echo.Context, key byte, check func(id utils.ID) error) error {
id := utils.ID{}
if err := id.Parse(c.Param("id")); err != nil {
return err
}
if id[0] != key {
return errors.New("前缀错误")
}
if err := check(id); err != nil {
return err
}
w.Delete(id[:])
return c.JSON(http.StatusNoContent, nil)
}
// ObjDeleter 对象删除器
func (w *Web) ObjDeleter(c echo.Context, key byte, obj Deleter, check func() error) error {
id := utils.ID{}
if err := id.Parse(c.Param("id")); err != nil {
return err
}
if id[0] != key {
return errors.New("前缀错误")
}
w.Get(id[:], obj)
if obj.IsDelete() {
return c.JSON(http.StatusOK, nil)
}
if err := check(); err != nil {
return err
}
obj.Delete()
w.Put(id[:], obj)
return c.JSON(http.StatusNoContent, nil)
}
// Match 查找匹配
func (o Obj) Match(txt string) bool {
if strings.Contains(o.Name, txt) ||
strings.Contains(o.Pinyin, strings.ToLower(txt)) {
return true
}
s := []byte{}
for _, p := range strings.Split(o.Pinyin, " ") {
if len(p) > 0 {
s = append(s, p[0])
}
}
return strings.Contains(string(s), strings.ToLower(txt))
}