Created
This commit is contained in:
68
setting_option.go
Normal file
68
setting_option.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package woogo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.cloudyne.io/go/woogo/entity"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// https://woocommerce.github.io/woocommerce-rest-api-docs/?php#setting-options
|
||||
|
||||
type settingOptionService service
|
||||
|
||||
// All list all setting options
|
||||
func (s settingOptionService) All(settingId string) (items []entity.SettingOption, err error) {
|
||||
resp, err := s.httpClient.R().Get(fmt.Sprintf("/settings/%s", settingId))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resp.IsSuccess() {
|
||||
err = jsoniter.Unmarshal(resp.Body(), &items)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// One retrieve a setting option
|
||||
func (s settingOptionService) One(groupId, optionId string) (item entity.SettingOption, err error) {
|
||||
resp, err := s.httpClient.R().Get(fmt.Sprintf("/settings/%s/%s", groupId, optionId))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resp.IsSuccess() {
|
||||
err = jsoniter.Unmarshal(resp.Body(), &item)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Create
|
||||
|
||||
type UpdateSettingOptionRequest struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func (m UpdateSettingOptionRequest) Validate() error {
|
||||
return validation.ValidateStruct(&m,
|
||||
validation.Field(&m.Value, validation.Required.Error("设置值不能为空")),
|
||||
)
|
||||
}
|
||||
|
||||
func (s settingOptionService) Update(groupId, optionId string, req UpdateSettingOptionRequest) (item entity.SettingOption, err error) {
|
||||
if err = req.Validate(); err != nil {
|
||||
return
|
||||
}
|
||||
resp, err := s.httpClient.R().
|
||||
SetBody(req).
|
||||
Put(fmt.Sprintf("/settings/%s/%s", groupId, optionId))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resp.IsSuccess() {
|
||||
err = jsoniter.Unmarshal(resp.Body(), &item)
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user