|
1 | 1 | package models |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "regexp" |
5 | | - "slices" |
6 | | - "strings" |
7 | | - |
8 | | - "humpback/common/enum" |
9 | | - "humpback/common/locales" |
10 | | - "humpback/common/response" |
11 | | - "humpback/common/verify" |
12 | | - "humpback/pkg/utils" |
13 | | - "humpback/types" |
| 4 | + "regexp" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "humpback/common/enum" |
| 9 | + "humpback/common/locales" |
| 10 | + "humpback/common/response" |
| 11 | + "humpback/common/verify" |
| 12 | + "humpback/pkg/utils" |
| 13 | + "humpback/types" |
14 | 14 | ) |
15 | 15 |
|
16 | 16 | type RegistryCreateReqInfo struct { |
17 | | - URL string `json:"url"` |
18 | | - IsDefault bool `json:"isDefault"` |
19 | | - Username string `json:"username"` |
20 | | - Password string `json:"password"` |
| 17 | + URL string `json:"url"` |
| 18 | + IsDefault bool `json:"isDefault"` |
| 19 | + Username string `json:"username"` |
| 20 | + Password string `json:"password"` |
21 | 21 | } |
22 | 22 |
|
23 | 23 | func (r *RegistryCreateReqInfo) Check() error { |
24 | | - if r.Username != "" { |
25 | | - r.Username = utils.RSADecrypt(r.Username) |
26 | | - } |
27 | | - if r.Password != "" { |
28 | | - r.Password = utils.RSADecrypt(r.Password) |
29 | | - } |
30 | | - |
31 | | - re := regexp.MustCompile(`/+`) |
32 | | - r.URL = strings.Trim(re.ReplaceAllString(r.URL, "/"), "/") |
33 | | - |
34 | | - if err := verify.CheckRequiredAndLengthLimit(r.URL, 0, enum.LimitRegistryUrl.Max, locales.CodeRegistryUrlNotEmpty, locales.CodeRegistryUrlLimitLength); err != nil { |
35 | | - return err |
36 | | - } |
37 | | - if err := verify.CheckLengthLimit(r.Username, 0, enum.LimitUsername.Max, locales.CodeRegistryUsernameLimitLength); err != nil { |
38 | | - return err |
39 | | - } |
40 | | - if err := verify.CheckLengthLimit(r.Password, 0, enum.LimitPassword.Max, locales.CodeRegistryPasswordLimitLength); err != nil { |
41 | | - return err |
42 | | - } |
43 | | - return nil |
| 24 | + if r.Username != "" { |
| 25 | + r.Username = utils.RSADecrypt(r.Username) |
| 26 | + } |
| 27 | + if r.Password != "" { |
| 28 | + r.Password = utils.RSADecrypt(r.Password) |
| 29 | + } |
| 30 | + |
| 31 | + re := regexp.MustCompile(`/+`) |
| 32 | + r.URL = strings.Trim(re.ReplaceAllString(r.URL, "/"), "/") |
| 33 | + |
| 34 | + if err := verify.CheckRequiredAndLengthLimit(r.URL, 0, enum.LimitRegistryUrl.Max, locales.CodeRegistryUrlNotEmpty, locales.CodeRegistryUrlLimitLength); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + if err := verify.CheckLengthLimit(r.Username, 0, enum.LimitRegistryUsername.Max, locales.CodeRegistryUsernameLimitLength); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + if err := verify.CheckLengthLimit(r.Password, 0, enum.LimitRegistryPassword.Max, locales.CodeRegistryPasswordLimitLength); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + return nil |
44 | 44 | } |
45 | 45 |
|
46 | 46 | func (r *RegistryCreateReqInfo) NewRegistryInfo() *types.Registry { |
47 | | - nowT := utils.NewActionTimestamp() |
48 | | - return &types.Registry{ |
49 | | - RegistryId: utils.NewGuidStr(), |
50 | | - URL: r.URL, |
51 | | - IsDefault: r.IsDefault, |
52 | | - Username: r.Username, |
53 | | - Password: r.Password, |
54 | | - CreatedAt: nowT, |
55 | | - UpdatedAt: nowT, |
56 | | - } |
| 47 | + nowT := utils.NewActionTimestamp() |
| 48 | + return &types.Registry{ |
| 49 | + RegistryId: utils.NewGuidStr(), |
| 50 | + URL: r.URL, |
| 51 | + IsDefault: r.IsDefault, |
| 52 | + Username: r.Username, |
| 53 | + Password: r.Password, |
| 54 | + CreatedAt: nowT, |
| 55 | + UpdatedAt: nowT, |
| 56 | + } |
57 | 57 | } |
58 | 58 |
|
59 | 59 | type RegistryUpdateReqInfo struct { |
60 | | - RegistryId string `json:"registryId"` |
61 | | - RegistryCreateReqInfo |
| 60 | + RegistryId string `json:"registryId"` |
| 61 | + RegistryCreateReqInfo |
62 | 62 | } |
63 | 63 |
|
64 | 64 | func (r *RegistryUpdateReqInfo) Check() error { |
65 | | - if err := verify.CheckIsEmpty(r.RegistryId, locales.CodeRegistryIdNotEmpty); err != nil { |
66 | | - return err |
67 | | - } |
68 | | - return r.RegistryCreateReqInfo.Check() |
| 65 | + if err := verify.CheckIsEmpty(r.RegistryId, locales.CodeRegistryIdNotEmpty); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return r.RegistryCreateReqInfo.Check() |
69 | 69 | } |
70 | 70 |
|
71 | 71 | func (r *RegistryUpdateReqInfo) NewRegistryInfo(oldInfo *types.Registry) *types.Registry { |
72 | | - return &types.Registry{ |
73 | | - RegistryId: oldInfo.RegistryId, |
74 | | - URL: r.URL, |
75 | | - IsDefault: r.IsDefault, |
76 | | - Username: r.Username, |
77 | | - Password: r.Password, |
78 | | - CreatedAt: oldInfo.CreatedAt, |
79 | | - UpdatedAt: utils.NewActionTimestamp(), |
80 | | - } |
| 72 | + return &types.Registry{ |
| 73 | + RegistryId: oldInfo.RegistryId, |
| 74 | + URL: r.URL, |
| 75 | + IsDefault: r.IsDefault, |
| 76 | + Username: r.Username, |
| 77 | + Password: r.Password, |
| 78 | + CreatedAt: oldInfo.CreatedAt, |
| 79 | + UpdatedAt: utils.NewActionTimestamp(), |
| 80 | + } |
81 | 81 | } |
82 | 82 |
|
83 | 83 | type RegistryQueryReqInfo struct { |
84 | | - types.QueryInfo |
| 84 | + types.QueryInfo |
85 | 85 | } |
86 | 86 |
|
87 | 87 | func (r *RegistryQueryReqInfo) Check() error { |
88 | | - r.QueryInfo.CheckBase() |
89 | | - if r.Keywords != "" && r.Mode != "url" { |
90 | | - return response.NewBadRequestErr(locales.CodeRequestParamsInvalid) |
91 | | - } |
92 | | - return nil |
| 88 | + r.QueryInfo.CheckBase() |
| 89 | + if r.Keywords != "" && r.Mode != "url" { |
| 90 | + return response.NewBadRequestErr(locales.CodeRequestParamsInvalid) |
| 91 | + } |
| 92 | + return nil |
93 | 93 | } |
94 | 94 |
|
95 | 95 | func (r *RegistryQueryReqInfo) QueryFilter(registries []*types.Registry) []*types.Registry { |
96 | | - result := make([]*types.Registry, 0) |
97 | | - for _, registry := range registries { |
98 | | - if strings.Contains(strings.ToLower(registry.URL), strings.ToLower(r.Keywords)) { |
99 | | - result = append(result, registry) |
100 | | - } |
101 | | - } |
102 | | - r.sort(result) |
103 | | - return result |
| 96 | + result := make([]*types.Registry, 0) |
| 97 | + for _, registry := range registries { |
| 98 | + if strings.Contains(strings.ToLower(registry.URL), strings.ToLower(r.Keywords)) { |
| 99 | + result = append(result, registry) |
| 100 | + } |
| 101 | + } |
| 102 | + r.sort(result) |
| 103 | + return result |
104 | 104 | } |
105 | 105 |
|
106 | 106 | func (r *RegistryQueryReqInfo) sort(list []*types.Registry) []*types.Registry { |
107 | | - var sortField = []string{"url", "updatedAt", "createdAt"} |
108 | | - if r.SortInfo == nil || r.SortInfo.Field == "" || slices.Index(sortField, r.SortInfo.Field) == -1 { |
109 | | - return list |
110 | | - } |
111 | | - slices.SortFunc(list, func(a, b *types.Registry) int { |
112 | | - switch r.SortInfo.Field { |
113 | | - case "url": |
114 | | - return types.QuerySortOrder(r.SortInfo.Order, strings.ToLower(a.URL), strings.ToLower(b.URL)) |
115 | | - case "updatedAt": |
116 | | - return types.QuerySortOrder(r.SortInfo.Order, a.UpdatedAt, b.UpdatedAt) |
117 | | - case "createdAt": |
118 | | - return types.QuerySortOrder(r.SortInfo.Order, a.CreatedAt, b.CreatedAt) |
119 | | - } |
120 | | - return 1 |
121 | | - }) |
122 | | - return list |
| 107 | + var sortField = []string{"url", "updatedAt", "createdAt"} |
| 108 | + if r.SortInfo == nil || r.SortInfo.Field == "" || slices.Index(sortField, r.SortInfo.Field) == -1 { |
| 109 | + return list |
| 110 | + } |
| 111 | + slices.SortFunc(list, func(a, b *types.Registry) int { |
| 112 | + switch r.SortInfo.Field { |
| 113 | + case "url": |
| 114 | + return types.QuerySortOrder(r.SortInfo.Order, strings.ToLower(a.URL), strings.ToLower(b.URL)) |
| 115 | + case "updatedAt": |
| 116 | + return types.QuerySortOrder(r.SortInfo.Order, a.UpdatedAt, b.UpdatedAt) |
| 117 | + case "createdAt": |
| 118 | + return types.QuerySortOrder(r.SortInfo.Order, a.CreatedAt, b.CreatedAt) |
| 119 | + } |
| 120 | + return 1 |
| 121 | + }) |
| 122 | + return list |
123 | 123 | } |
0 commit comments