-
Notifications
You must be signed in to change notification settings - Fork 12
/
shipping_zone_method.go
115 lines (94 loc) · 2.82 KB
/
shipping_zone_method.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
package woocommerce
import (
"fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/hiscaler/woocommerce-go/entity"
jsoniter "github.com/json-iterator/go"
)
type shippingZoneMethodService service
// All list all shipping zone methods
func (s shippingZoneMethodService) All(zoneId int) (items []entity.ShippingZoneMethod, err error) {
resp, err := s.httpClient.R().Get(fmt.Sprintf("/shipping/zones/%d/methods", zoneId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// One retrieve a shipping zone method
func (s shippingZoneMethodService) One(zoneId, methodId int) (item entity.ShippingZoneMethod, err error) {
resp, err := s.httpClient.R().Get(fmt.Sprintf("/shipping/zones/%d/methods/%d", zoneId, methodId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Include include a shipping method to a shipping zone
type ShippingZoneMethodIncludeRequest struct {
MethodId string `json:"method_id"`
}
func (m ShippingZoneMethodIncludeRequest) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.MethodId, validation.Required.Error("配送方式不能为空")),
)
}
func (s shippingZoneMethodService) Include(zoneId int, req ShippingZoneMethodIncludeRequest) (item entity.ShippingZoneMethod, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().
SetBody(req).
Post(fmt.Sprintf("/shipping/zones/%d/methods", zoneId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Update
type UpdateShippingZoneMethodSetting struct {
Value string `json:"value"`
}
type UpdateShippingZoneMethodRequest struct {
Order int `json:"order"`
Enabled bool `json:"enabled"`
Settings UpdateShippingZoneMethodSetting `json:"settings"`
}
func (m UpdateShippingZoneMethodRequest) Validate() error {
return nil
}
func (s shippingZoneMethodService) Update(zoneId, methodId int, req UpdateShippingZoneMethodRequest) (item entity.ShippingZoneMethod, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().
SetBody(req).
Put(fmt.Sprintf("/shipping/zones/%d/methods/%d", zoneId, methodId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Delete delete a shipping zone
func (s shippingZoneMethodService) Delete(zoneId, methodId int, force bool) (item entity.ShippingZone, err error) {
resp, err := s.httpClient.R().
SetBody(map[string]bool{"force": force}).
Delete(fmt.Sprintf("/shipping/zones/%d/%d", zoneId, methodId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}