-
Notifications
You must be signed in to change notification settings - Fork 12
/
shipping_zone_location.go
65 lines (55 loc) · 1.74 KB
/
shipping_zone_location.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
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 shippingZoneLocationService service
// All list all shipping zone locations
func (s shippingZoneLocationService) All(shippingZoneId int) (items []entity.ShippingZoneLocation, err error) {
resp, err := s.httpClient.R().Get(fmt.Sprintf("/shipping/zones/%d/locations", shippingZoneId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}
// Update
type UpdateShippingZoneLocationsRequest []entity.ShippingZoneLocation
func (m UpdateShippingZoneLocationsRequest) Validate() error {
return validation.Validate(m, validation.Required.Error("待更新数据不能为空"),
validation.By(func(value interface{}) error {
items, ok := value.([]entity.ShippingZoneLocation)
if !ok {
return errors.New("待更新数据错误")
}
for _, item := range items {
err := validation.ValidateStruct(&item,
validation.Field(&item.Code, validation.Required.Error("代码不能为空")),
validation.Field(&item.Type, validation.In("postcode", "country", "state", "continent").Error("类型错误")),
)
if err != nil {
return err
}
}
return nil
}),
)
}
func (s shippingZoneLocationService) Update(shippingZoneId int, req UpdateShippingZoneLocationsRequest) (items []entity.ShippingZoneLocation, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Put(fmt.Sprintf("/shipping/zones/%d/locations", shippingZoneId))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
}
return
}