forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_item.go
70 lines (59 loc) · 2.43 KB
/
inventory_item.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
package goshopify
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
const inventoryItemsBasePath = "inventory_items"
// InventoryItemService is an interface for interacting with the
// inventory items endpoints of the Shopify API
// See https://help.shopify.com/en/api/reference/inventory/inventoryitem
type InventoryItemService interface {
List(interface{}) ([]InventoryItem, error)
Get(int64, interface{}) (*InventoryItem, error)
Update(InventoryItem) (*InventoryItem, error)
}
// InventoryItemServiceOp is the default implementation of the InventoryItemService interface
type InventoryItemServiceOp struct {
client *Client
}
// InventoryItem represents a Shopify inventory item
type InventoryItem struct {
ID int64 `json:"id,omitempty"`
SKU string `json:"sku,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Cost *decimal.Decimal `json:"cost,omitempty"`
Tracked *bool `json:"tracked,omitempty"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
}
// InventoryItemResource is used for handling single item requests and responses
type InventoryItemResource struct {
InventoryItem *InventoryItem `json:"inventory_item"`
}
// InventoryItemsResource is used for handling multiple item responsees
type InventoryItemsResource struct {
InventoryItems []InventoryItem `json:"inventory_items"`
}
// List inventory items
func (s *InventoryItemServiceOp) List(options interface{}) ([]InventoryItem, error) {
path := fmt.Sprintf("%s.json", inventoryItemsBasePath)
resource := new(InventoryItemsResource)
err := s.client.Get(path, resource, options)
return resource.InventoryItems, err
}
// Get a inventory item
func (s *InventoryItemServiceOp) Get(id int64, options interface{}) (*InventoryItem, error) {
path := fmt.Sprintf("%s/%d.json", inventoryItemsBasePath, id)
resource := new(InventoryItemResource)
err := s.client.Get(path, resource, options)
return resource.InventoryItem, err
}
// Update a inventory item
func (s *InventoryItemServiceOp) Update(item InventoryItem) (*InventoryItem, error) {
path := fmt.Sprintf("%s/%d.json", inventoryItemsBasePath, item.ID)
wrappedData := InventoryItemResource{InventoryItem: &item}
resource := new(InventoryItemResource)
err := s.client.Put(path, wrappedData, resource)
return resource.InventoryItem, err
}