/
productOption.go
94 lines (75 loc) · 2.1 KB
/
productOption.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
package prestashopApi
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"net/url"
"github.com/lciprian/prestashopApi/models"
)
var productOptionBasePath = "product_options"
type ProductOptionList struct {
Limit int
Page int
Data []models.ProductOption `json:"product_options,omitempty"`
}
type ProductOptionService struct {
client *Client
}
func newProductOptionService(client *Client) ProductOptionService {
return ProductOptionService{
client: client,
}
}
func (s *ProductOptionService) ListProductOptions(prodOptionId string, limit, page int) (*ProductOptionList, error) {
if page > 0 {
page -= 1
}
offset := limit * page
queryParams := url.Values{}
queryParams.Add("display", "full")
queryParams.Add("limit", fmt.Sprintf("%d,%d", offset, limit))
if prodOptionId == "" {
queryParams.Add("filter[id_attribute_group]", prodOptionId)
}
result := ProductOptionList{
Limit: limit,
Page: page,
}
if err := s.client.Get(productOptionBasePath, queryParams, &result); err != nil {
return nil, err
}
return &result, nil
}
func (s *ProductOptionService) CreateProductOption(productOption models.ProductOptionReq) (*models.ProductOption, error) {
queryParams := url.Values{}
buf, err := xml.Marshal(PrestashopReq{ProductOption: &productOption})
if err != nil {
return nil, err
}
data, err := s.client.Post(productOptionBasePath, queryParams, bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
psResponse := Prestashop{}
if err := json.Unmarshal(data, &psResponse); err != nil {
return nil, err
}
return &psResponse.ProductOption, nil
}
func (s *ProductOptionService) UpdateProductOption(productOption models.ProductOptionReq) (*models.ProductOption, error) {
queryParams := url.Values{}
buf, err := xml.Marshal(PrestashopReq{ProductOption: &productOption})
if err != nil {
return nil, err
}
data, err := s.client.Post(productOptionBasePath, queryParams, bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
psResponse := Prestashop{}
if err := json.Unmarshal(data, &psResponse); err != nil {
return nil, err
}
return &psResponse.ProductOption, nil
}