This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
smartcollection.go
146 lines (125 loc) · 5.99 KB
/
smartcollection.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package goshopify
import (
"fmt"
"time"
)
const smartCollectionsBasePath = "admin/smart_collections"
const smartCollectionsResourceName = "collections"
// SmartCollectionService is an interface for interacting with the smart
// collection endpoints of the Shopify API.
// See https://help.shopify.com/api/reference/smartcollection
type SmartCollectionService interface {
List(interface{}) ([]SmartCollection, error)
Count(interface{}) (int, error)
Get(int, interface{}) (*SmartCollection, error)
Create(SmartCollection) (*SmartCollection, error)
Update(SmartCollection) (*SmartCollection, error)
Delete(int) error
// MetafieldsService used for SmartCollection resource to communicate with Metafields resource
MetafieldsService
}
// SmartCollectionServiceOp handles communication with the smart collection
// related methods of the Shopify API.
type SmartCollectionServiceOp struct {
client *Client
}
type Rule struct {
Column string `json:"column"`
Relation string `json:"relation"`
Condition string `json:"condition"`
}
// SmartCollection represents a Shopify smart collection.
type SmartCollection struct {
ID int `json:"id"`
Handle string `json:"handle"`
Title string `json:"title"`
UpdatedAt *time.Time `json:"updated_at"`
BodyHTML string `json:"body_html"`
SortOrder string `json:"sort_order"`
TemplateSuffix string `json:"template_suffix"`
Image Image `json:"image"`
Published bool `json:"published"`
PublishedAt *time.Time `json:"published_at"`
PublishedScope string `json:"published_scope"`
Rules []Rule `json:"rules"`
Disjunctive bool `json:"disjunctive"`
Metafields []Metafield `json:"metafields,omitempty"`
}
// SmartCollectionResource represents the result from the smart_collections/X.json endpoint
type SmartCollectionResource struct {
Collection *SmartCollection `json:"smart_collection"`
}
// SmartCollectionsResource represents the result from the smart_collections.json endpoint
type SmartCollectionsResource struct {
Collections []SmartCollection `json:"smart_collections"`
}
// List smart collections
func (s *SmartCollectionServiceOp) List(options interface{}) ([]SmartCollection, error) {
path := fmt.Sprintf("%s.json", smartCollectionsBasePath)
resource := new(SmartCollectionsResource)
err := s.client.Get(path, resource, options)
return resource.Collections, err
}
// Count smart collections
func (s *SmartCollectionServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", smartCollectionsBasePath)
return s.client.Count(path, options)
}
// Get individual smart collection
func (s *SmartCollectionServiceOp) Get(collectionID int, options interface{}) (*SmartCollection, error) {
path := fmt.Sprintf("%s/%d.json", smartCollectionsBasePath, collectionID)
resource := new(SmartCollectionResource)
err := s.client.Get(path, resource, options)
return resource.Collection, err
}
// Create a new smart collection
// See Image for the details of the Image creation for a collection.
func (s *SmartCollectionServiceOp) Create(collection SmartCollection) (*SmartCollection, error) {
path := fmt.Sprintf("%s.json", smartCollectionsBasePath)
wrappedData := SmartCollectionResource{Collection: &collection}
resource := new(SmartCollectionResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Collection, err
}
// Update an existing smart collection
func (s *SmartCollectionServiceOp) Update(collection SmartCollection) (*SmartCollection, error) {
path := fmt.Sprintf("%s/%d.json", smartCollectionsBasePath, collection.ID)
wrappedData := SmartCollectionResource{Collection: &collection}
resource := new(SmartCollectionResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Collection, err
}
// Delete an existing smart collection.
func (s *SmartCollectionServiceOp) Delete(collectionID int) error {
return s.client.Delete(fmt.Sprintf("%s/%d.json", smartCollectionsBasePath, collectionID))
}
// List metafields for a smart collection
func (s *SmartCollectionServiceOp) ListMetafields(smartCollectionID int, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: smartCollectionsResourceName, resourceID: smartCollectionID}
return metafieldService.List(options)
}
// Count metafields for a smart collection
func (s *SmartCollectionServiceOp) CountMetafields(smartCollectionID int, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: smartCollectionsResourceName, resourceID: smartCollectionID}
return metafieldService.Count(options)
}
// Get individual metafield for a smart collection
func (s *SmartCollectionServiceOp) GetMetafield(smartCollectionID int, metafieldID int, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: smartCollectionsResourceName, resourceID: smartCollectionID}
return metafieldService.Get(metafieldID, options)
}
// Create a new metafield for a smart collection
func (s *SmartCollectionServiceOp) CreateMetafield(smartCollectionID int, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: smartCollectionsResourceName, resourceID: smartCollectionID}
return metafieldService.Create(metafield)
}
// Update an existing metafield for a smart collection
func (s *SmartCollectionServiceOp) UpdateMetafield(smartCollectionID int, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: smartCollectionsResourceName, resourceID: smartCollectionID}
return metafieldService.Update(metafield)
}
// // Delete an existing metafield for a smart collection
func (s *SmartCollectionServiceOp) DeleteMetafield(smartCollectionID int, metafieldID int) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: smartCollectionsResourceName, resourceID: smartCollectionID}
return metafieldService.Delete(metafieldID)
}