/
productStock.go
64 lines (50 loc) · 1.56 KB
/
productStock.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
package prestashopApi
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"net/url"
"github.com/lciprian/prestashopApi/models"
)
var productStockAvailableBasePath = "stock_availables"
type ProductStockAvailableList struct {
Limit int
Page int
Data []models.ProductStock `json:"product_option_values,omitempty"`
}
type ProductStockAvailableService struct {
client *Client
}
func newProductStockAvailableService(client *Client) ProductStockAvailableService {
return ProductStockAvailableService{
client: client,
}
}
func (s *ProductStockAvailableService) GetProductStockAvailable(prodStockId string) (models.ProductStock, error) {
queryParams := url.Values{}
psResponse := Prestashop{}
paPath := fmt.Sprintf("%s/%s", productStockAvailableBasePath, prodStockId)
err := s.client.Get(paPath, queryParams, &psResponse)
if err != nil {
return models.ProductStock{}, err
}
return psResponse.ProductStock, nil
}
func (s *ProductStockAvailableService) UpdateProductStockAvailable(productStock models.ProductStock) (models.ProductStock, error) {
queryParams := url.Values{}
buf, err := xml.Marshal(PrestashopReq{ProductStock: &productStock})
if err != nil {
return models.ProductStock{}, err
}
paPath := fmt.Sprintf("%s/%s", productStockAvailableBasePath, productStock.ID)
data, err := s.client.Put(paPath, queryParams, bytes.NewBuffer(buf))
if err != nil {
return models.ProductStock{}, err
}
psResponse := Prestashop{}
if err := json.Unmarshal(data, &psResponse); err != nil {
return models.ProductStock{}, err
}
return psResponse.ProductStock, nil
}