forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customcollection.go
141 lines (121 loc) · 6.31 KB
/
customcollection.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
package goshopify
import (
"context"
"fmt"
"time"
)
const (
customCollectionsBasePath = "custom_collections"
customCollectionsResourceName = "collections"
)
// CustomCollectionService is an interface for interacting with the custom
// collection endpoints of the Shopify API.
// See https://help.shopify.com/api/reference/customcollection
type CustomCollectionService interface {
List(context.Context, interface{}) ([]CustomCollection, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*CustomCollection, error)
Create(context.Context, CustomCollection) (*CustomCollection, error)
Update(context.Context, CustomCollection) (*CustomCollection, error)
Delete(context.Context, uint64) error
// MetafieldsService used for CustomCollection resource to communicate with Metafields resource
MetafieldsService
}
// CustomCollectionServiceOp handles communication with the custom collection
// related methods of the Shopify API.
type CustomCollectionServiceOp struct {
client *Client
}
// CustomCollection represents a Shopify custom collection.
type CustomCollection struct {
Id uint64 `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"`
Metafields []Metafield `json:"metafields,omitempty"`
}
// CustomCollectionResource represents the result form the custom_collections/X.json endpoint
type CustomCollectionResource struct {
Collection *CustomCollection `json:"custom_collection"`
}
// CustomCollectionsResource represents the result from the custom_collections.json endpoint
type CustomCollectionsResource struct {
Collections []CustomCollection `json:"custom_collections"`
}
// List custom collections
func (s *CustomCollectionServiceOp) List(ctx context.Context, options interface{}) ([]CustomCollection, error) {
path := fmt.Sprintf("%s.json", customCollectionsBasePath)
resource := new(CustomCollectionsResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Collections, err
}
// Count custom collections
func (s *CustomCollectionServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", customCollectionsBasePath)
return s.client.Count(ctx, path, options)
}
// Get individual custom collection
func (s *CustomCollectionServiceOp) Get(ctx context.Context, collectionId uint64, options interface{}) (*CustomCollection, error) {
path := fmt.Sprintf("%s/%d.json", customCollectionsBasePath, collectionId)
resource := new(CustomCollectionResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Collection, err
}
// Create a new custom collection
// See Image for the details of the Image creation for a collection.
func (s *CustomCollectionServiceOp) Create(ctx context.Context, collection CustomCollection) (*CustomCollection, error) {
path := fmt.Sprintf("%s.json", customCollectionsBasePath)
wrappedData := CustomCollectionResource{Collection: &collection}
resource := new(CustomCollectionResource)
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.Collection, err
}
// Update an existing custom collection
func (s *CustomCollectionServiceOp) Update(ctx context.Context, collection CustomCollection) (*CustomCollection, error) {
path := fmt.Sprintf("%s/%d.json", customCollectionsBasePath, collection.Id)
wrappedData := CustomCollectionResource{Collection: &collection}
resource := new(CustomCollectionResource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.Collection, err
}
// Delete an existing custom collection.
func (s *CustomCollectionServiceOp) Delete(ctx context.Context, collectionId uint64) error {
return s.client.Delete(ctx, fmt.Sprintf("%s/%d.json", customCollectionsBasePath, collectionId))
}
// List metafields for a custom collection
func (s *CustomCollectionServiceOp) ListMetafields(ctx context.Context, customCollectionId uint64, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customCollectionsResourceName, resourceId: customCollectionId}
return metafieldService.List(ctx, options)
}
// Count metafields for a custom collection
func (s *CustomCollectionServiceOp) CountMetafields(ctx context.Context, customCollectionId uint64, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customCollectionsResourceName, resourceId: customCollectionId}
return metafieldService.Count(ctx, options)
}
// Get individual metafield for a custom collection
func (s *CustomCollectionServiceOp) GetMetafield(ctx context.Context, customCollectionId uint64, metafieldId uint64, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customCollectionsResourceName, resourceId: customCollectionId}
return metafieldService.Get(ctx, metafieldId, options)
}
// Create a new metafield for a custom collection
func (s *CustomCollectionServiceOp) CreateMetafield(ctx context.Context, customCollectionId uint64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customCollectionsResourceName, resourceId: customCollectionId}
return metafieldService.Create(ctx, metafield)
}
// Update an existing metafield for a custom collection
func (s *CustomCollectionServiceOp) UpdateMetafield(ctx context.Context, customCollectionId uint64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customCollectionsResourceName, resourceId: customCollectionId}
return metafieldService.Update(ctx, metafield)
}
// // Delete an existing metafield for a custom collection
func (s *CustomCollectionServiceOp) DeleteMetafield(ctx context.Context, customCollectionId uint64, metafieldId uint64) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customCollectionsResourceName, resourceId: customCollectionId}
return metafieldService.Delete(ctx, metafieldId)
}