forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_item.go
75 lines (64 loc) · 2.96 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
71
72
73
74
75
package goshopify
import (
"context"
"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(context.Context, interface{}) ([]InventoryItem, error)
Get(context.Context, uint64, interface{}) (*InventoryItem, error)
Update(context.Context, 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 uint64 `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"`
CountryCodeOfOrigin *string `json:"country_code_of_origin"`
CountryHarmonizedSystemCodes []string `json:"country_harmonized_system_codes"`
HarmonizedSystemCode *string `json:"harmonized_system_code"`
ProvinceCodeOfOrigin *string `json:"province_code_of_origin"`
}
// 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(ctx context.Context, options interface{}) ([]InventoryItem, error) {
path := fmt.Sprintf("%s.json", inventoryItemsBasePath)
resource := new(InventoryItemsResource)
err := s.client.Get(ctx, path, resource, options)
return resource.InventoryItems, err
}
// Get a inventory item
func (s *InventoryItemServiceOp) Get(ctx context.Context, id uint64, options interface{}) (*InventoryItem, error) {
path := fmt.Sprintf("%s/%d.json", inventoryItemsBasePath, id)
resource := new(InventoryItemResource)
err := s.client.Get(ctx, path, resource, options)
return resource.InventoryItem, err
}
// Update a inventory item
func (s *InventoryItemServiceOp) Update(ctx context.Context, item InventoryItem) (*InventoryItem, error) {
path := fmt.Sprintf("%s/%d.json", inventoryItemsBasePath, item.Id)
wrappedData := InventoryItemResource{InventoryItem: &item}
resource := new(InventoryItemResource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.InventoryItem, err
}