-
Notifications
You must be signed in to change notification settings - Fork 10
/
product_tag.go
168 lines (139 loc) · 4.18 KB
/
product_tag.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
package woocommerce
import (
"errors"
"fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/hiscaler/woocommerce-go/entity"
jsoniter "github.com/json-iterator/go"
)
type productTagService service
type ProductTagsQueryParams struct {
queryParams
Search string `url:"search,omitempty"`
Exclude []int `url:"exclude,omitempty"`
Include []int `url:"include,omitempty"`
HideEmpty bool `url:"hide_empty,omitempty"`
Product int `url:"product,omitempty"`
Slug string `url:"slug,omitempty"`
}
func (m ProductTagsQueryParams) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.OrderBy, validation.When(m.OrderBy != "", validation.In("id", "include", "name", "slug", "term_group", "description", "count").Error("无效的排序字段"))),
)
}
func (s productTagService) All(params ProductTagsQueryParams) (items []entity.ProductTag, total, totalPages int, isLastPage bool, err error) {
if err = params.Validate(); err != nil {
return
}
params.TidyVars()
resp, err := s.httpClient.R().SetQueryParamsFromValues(toValues(params)).Get("/products/tags")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
total, totalPages, isLastPage = parseResponseTotal(params.Page, resp)
}
return
}
func (s productTagService) One(id int) (item entity.ProductTag, err error) {
var res entity.ProductTag
resp, err := s.httpClient.R().Get(fmt.Sprintf("/products/tags/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
if err = jsoniter.Unmarshal(resp.Body(), &res); err == nil {
item = res
}
}
return
}
// 新增商品标签
type UpsertProductTagRequest struct {
Name string `json:"name"`
Slug string `json:"slug,omitempty"`
Description string `json:"description,omitempty"`
}
type CreateProductTagRequest = UpsertProductTagRequest
type UpdateProductTagRequest = UpsertProductTagRequest
func (m UpsertProductTagRequest) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Name,
validation.Required.Error("标签名称不能为空"),
),
)
}
func (s productTagService) Create(req CreateProductTagRequest) (item entity.ProductTag, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Post("/products/tags")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
func (s productTagService) Update(id int, req UpdateProductTagRequest) (item entity.ProductTag, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Put(fmt.Sprintf("/products/tags/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
func (s productTagService) Delete(id int, force bool) (item entity.ProductTag, err error) {
resp, err := s.httpClient.R().
SetBody(map[string]bool{"force": force}).
Delete(fmt.Sprintf("/products/tags/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Batch tag create,update and delete operation
type BatchProductTagsCreateItem = UpsertProductTagRequest
type BatchProductTagsUpdateItem struct {
ID int `json:"id"`
UpsertProductTagRequest
}
type BatchProductTagsRequest struct {
Create []BatchProductTagsCreateItem `json:"create,omitempty"`
Update []BatchProductTagsUpdateItem `json:"update,omitempty"`
Delete []int `json:"delete,omitempty"`
}
func (m BatchProductTagsRequest) Validate() error {
if len(m.Create) == 0 && len(m.Update) == 0 && len(m.Delete) == 0 {
return errors.New("无效的请求数据")
}
return nil
}
type BatchProductTagsResult struct {
Create []entity.ProductTag `json:"create"`
Update []entity.ProductTag `json:"update"`
Delete []entity.ProductTag `json:"delete"`
}
func (s productTagService) Batch(req BatchProductTagsRequest) (res BatchProductTagsResult, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Post("/products/tags/batch")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &res)
}
return
}