|
1 | 1 | package models |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "slices" |
5 | | - "strings" |
6 | | - |
7 | | - "humpback/common/enum" |
8 | | - "humpback/common/locales" |
9 | | - "humpback/common/response" |
10 | | - "humpback/common/verify" |
11 | | - "humpback/pkg/utils" |
12 | | - "humpback/types" |
| 4 | + "slices" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "humpback/common/enum" |
| 8 | + "humpback/common/locales" |
| 9 | + "humpback/common/response" |
| 10 | + "humpback/common/verify" |
| 11 | + "humpback/pkg/utils" |
| 12 | + "humpback/types" |
13 | 13 | ) |
14 | 14 |
|
15 | 15 | type GroupCreateReqInfo struct { |
16 | | - GroupName string `json:"groupName"` |
17 | | - Description string `json:"description"` |
18 | | - Users []string `json:"users"` |
19 | | - Teams []string `json:"teams"` |
| 16 | + GroupName string `json:"groupName"` |
| 17 | + Description string `json:"description"` |
| 18 | + Users []string `json:"users"` |
| 19 | + Teams []string `json:"teams"` |
20 | 20 | } |
21 | 21 |
|
22 | 22 | func (g *GroupCreateReqInfo) Check() error { |
23 | | - if err := verify.CheckRequiredAndLengthLimit(g.GroupName, enum.LimitGroupName.Min, enum.LimitGroupName.Max, locales.CodeGroupNameNotEmpty, locales.CodeGroupNameLimitLength); err != nil { |
24 | | - return err |
25 | | - } |
26 | | - if err := verify.CheckLengthLimit(g.Description, enum.LimitDescription.Min, enum.LimitDescription.Max, locales.CodeDescriptionLimitLength); err != nil { |
27 | | - return err |
28 | | - } |
29 | | - return nil |
| 23 | + if err := verify.CheckRequiredAndLengthLimit(g.GroupName, enum.LimitGroupName.Min, enum.LimitGroupName.Max, locales.CodeGroupNameNotEmpty, locales.CodeGroupNameLimitLength); err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + if err := verify.CheckLengthLimit(g.Description, enum.LimitDescription.Min, enum.LimitDescription.Max, locales.CodeDescriptionLimitLength); err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + if g.Users == nil { |
| 30 | + g.Users = make([]string, 0) |
| 31 | + } |
| 32 | + if g.Teams == nil { |
| 33 | + g.Teams = make([]string, 0) |
| 34 | + } |
| 35 | + return nil |
30 | 36 | } |
31 | 37 |
|
32 | 38 | func (g *GroupCreateReqInfo) NewGroupInfo() *types.NodesGroups { |
33 | | - nowT := utils.NewActionTimestamp() |
34 | | - return &types.NodesGroups{ |
35 | | - GroupId: utils.NewGroupId(), |
36 | | - GroupName: g.GroupName, |
37 | | - Description: g.Description, |
38 | | - Users: g.Users, |
39 | | - Teams: g.Teams, |
40 | | - Nodes: make([]string, 0), |
41 | | - CreatedAt: nowT, |
42 | | - UpdatedAt: nowT, |
43 | | - } |
| 39 | + nowT := utils.NewActionTimestamp() |
| 40 | + return &types.NodesGroups{ |
| 41 | + GroupId: utils.NewGroupId(), |
| 42 | + GroupName: g.GroupName, |
| 43 | + Description: g.Description, |
| 44 | + Users: g.Users, |
| 45 | + Teams: g.Teams, |
| 46 | + Nodes: make([]string, 0), |
| 47 | + CreatedAt: nowT, |
| 48 | + UpdatedAt: nowT, |
| 49 | + } |
44 | 50 | } |
45 | 51 |
|
46 | 52 | type GroupUpdateReqInfo struct { |
47 | | - GroupId string `json:"groupId"` |
48 | | - GroupCreateReqInfo |
| 53 | + GroupId string `json:"groupId"` |
| 54 | + GroupCreateReqInfo |
49 | 55 | } |
50 | 56 |
|
51 | 57 | func (g *GroupUpdateReqInfo) Check() error { |
52 | | - if err := verify.CheckIsEmpty(g.GroupId, locales.CodeGroupIdNotEmpty); err != nil { |
53 | | - return err |
54 | | - } |
55 | | - return g.GroupCreateReqInfo.Check() |
| 58 | + if err := verify.CheckIsEmpty(g.GroupId, locales.CodeGroupIdNotEmpty); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + return g.GroupCreateReqInfo.Check() |
56 | 62 | } |
57 | 63 |
|
58 | 64 | func (g *GroupUpdateReqInfo) NewGroupInfo(oldInfo *types.NodesGroups) *types.NodesGroups { |
59 | | - return &types.NodesGroups{ |
60 | | - GroupId: oldInfo.GroupId, |
61 | | - GroupName: g.GroupName, |
62 | | - Description: g.Description, |
63 | | - Users: g.Users, |
64 | | - Teams: g.Teams, |
65 | | - Nodes: oldInfo.Nodes, |
66 | | - CreatedAt: oldInfo.CreatedAt, |
67 | | - UpdatedAt: utils.NewActionTimestamp(), |
68 | | - } |
| 65 | + return &types.NodesGroups{ |
| 66 | + GroupId: oldInfo.GroupId, |
| 67 | + GroupName: g.GroupName, |
| 68 | + Description: g.Description, |
| 69 | + Users: g.Users, |
| 70 | + Teams: g.Teams, |
| 71 | + Nodes: oldInfo.Nodes, |
| 72 | + CreatedAt: oldInfo.CreatedAt, |
| 73 | + UpdatedAt: utils.NewActionTimestamp(), |
| 74 | + } |
69 | 75 | } |
70 | 76 |
|
71 | 77 | type GroupQueryReqInfo struct { |
72 | | - UserInfo *types.User `json:"-"` |
73 | | - types.QueryInfo |
| 78 | + UserInfo *types.User `json:"-"` |
| 79 | + types.QueryInfo |
74 | 80 | } |
75 | 81 |
|
76 | 82 | func (g *GroupQueryReqInfo) Check() error { |
77 | | - g.QueryInfo.CheckBase() |
78 | | - if g.Keywords != "" && g.Mode != "groupName" { |
79 | | - return response.NewBadRequestErr(locales.CodeRequestParamsInvalid) |
80 | | - } |
81 | | - return nil |
| 83 | + g.QueryInfo.CheckBase() |
| 84 | + if g.Keywords != "" && g.Mode != "groupName" { |
| 85 | + return response.NewBadRequestErr(locales.CodeRequestParamsInvalid) |
| 86 | + } |
| 87 | + return nil |
82 | 88 | } |
83 | 89 |
|
84 | 90 | func (g *GroupQueryReqInfo) QueryFilter(groups []*types.NodesGroups) []*types.NodesGroups { |
85 | | - result := make([]*types.NodesGroups, 0) |
86 | | - for _, group := range groups { |
87 | | - if g.UserInfo.InGroup(group) && strings.Contains(strings.ToLower(group.GroupName), strings.ToLower(g.Keywords)) { |
88 | | - result = append(result, group) |
89 | | - } |
90 | | - } |
91 | | - g.sort(result) |
92 | | - return result |
| 91 | + result := make([]*types.NodesGroups, 0) |
| 92 | + for _, group := range groups { |
| 93 | + if g.UserInfo.InGroup(group) && strings.Contains(strings.ToLower(group.GroupName), strings.ToLower(g.Keywords)) { |
| 94 | + result = append(result, group) |
| 95 | + } |
| 96 | + } |
| 97 | + g.sort(result) |
| 98 | + return result |
93 | 99 | } |
94 | 100 |
|
95 | 101 | func (g *GroupQueryReqInfo) sort(list []*types.NodesGroups) []*types.NodesGroups { |
96 | | - var sortField = []string{"groupName", "updatedAt", "createdAt"} |
97 | | - if g.SortInfo == nil || g.SortInfo.Field == "" || slices.Index(sortField, g.SortInfo.Field) == -1 { |
98 | | - return list |
99 | | - } |
100 | | - slices.SortFunc(list, func(a, b *types.NodesGroups) int { |
101 | | - switch g.SortInfo.Field { |
102 | | - case "groupName": |
103 | | - return types.QuerySortOrder(g.SortInfo.Order, strings.ToLower(a.GroupName), strings.ToLower(b.GroupName)) |
104 | | - case "updatedAt": |
105 | | - return types.QuerySortOrder(g.SortInfo.Order, a.UpdatedAt, b.UpdatedAt) |
106 | | - case "createdAt": |
107 | | - return types.QuerySortOrder(g.SortInfo.Order, a.CreatedAt, b.CreatedAt) |
108 | | - } |
109 | | - return 1 |
110 | | - }) |
111 | | - return list |
| 102 | + var sortField = []string{"groupName", "updatedAt", "createdAt"} |
| 103 | + if g.SortInfo == nil || g.SortInfo.Field == "" || slices.Index(sortField, g.SortInfo.Field) == -1 { |
| 104 | + return list |
| 105 | + } |
| 106 | + slices.SortFunc(list, func(a, b *types.NodesGroups) int { |
| 107 | + switch g.SortInfo.Field { |
| 108 | + case "groupName": |
| 109 | + return types.QuerySortOrder(g.SortInfo.Order, strings.ToLower(a.GroupName), strings.ToLower(b.GroupName)) |
| 110 | + case "updatedAt": |
| 111 | + return types.QuerySortOrder(g.SortInfo.Order, a.UpdatedAt, b.UpdatedAt) |
| 112 | + case "createdAt": |
| 113 | + return types.QuerySortOrder(g.SortInfo.Order, a.CreatedAt, b.CreatedAt) |
| 114 | + } |
| 115 | + return 1 |
| 116 | + }) |
| 117 | + return list |
112 | 118 | } |
0 commit comments