/
productOptionsValue.go
101 lines (81 loc) · 2.46 KB
/
productOptionsValue.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
package prestashopApi
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"net/url"
"github.com/lciprian/prestashopApi/models"
)
var productOptionValueBasePath = "product_option_values"
type ProductOptionValueList struct {
Limit int
Page int
Data []models.ProductOptionValueL `json:"product_option_values,omitempty"`
}
type ProductOptionValueService struct {
client *Client
}
func newProductOptionValueService(client *Client) ProductOptionValueService {
return ProductOptionValueService{
client: client,
}
}
type ProductOptionValueCombinationList struct {
Limit int
Page int
Data []models.Variant `json:"combinations,omitempty"`
}
func (s *ProductOptionValueService) ListProductOptionValues(prodOptionId string, limit, page int) (*ProductOptionValueList, 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)
}
productOptionValueList := ProductOptionValueList{
Limit: limit,
Page: page,
}
if err := s.client.Get(productOptionValueBasePath, queryParams, &productOptionValueList); err != nil {
return nil, err
}
// fmt.Println("-ListProducts---------", products)
return &productOptionValueList, nil
}
func (s *ProductOptionValueService) CreateProductOptionValue(productOption models.ProductOptionValueReq) (*models.ProductOptionValue, error) {
queryParams := url.Values{}
buf, err := xml.Marshal(PrestashopReq{ProductOptionValue: &productOption})
if err != nil {
return nil, err
}
data, err := s.client.Post(productOptionValueBasePath, 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.ProductOptionValue, nil
}
func (s *ProductOptionValueService) UpdateProductOptionValue(productOption models.ProductOptionValueReq) (*models.ProductOptionValue, error) {
queryParams := url.Values{}
buf, err := xml.Marshal(PrestashopReq{ProductOptionValue: &productOption})
if err != nil {
return nil, err
}
data, err := s.client.Post(productOptionValueBasePath, 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.ProductOptionValue, nil
}