forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartcollection.go
105 lines (91 loc) · 3.76 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
package goshopify
import (
"fmt"
"time"
)
const smartCollectionsBasePath = "admin/smart_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
}
// 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"`
}
// 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))
}