forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.go
84 lines (72 loc) · 2.73 KB
/
collect.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
package goshopify
import (
"context"
"fmt"
"time"
)
const collectsBasePath = "collects"
// CollectService is an interface for interfacing with the collect endpoints
// of the Shopify API.
// See: https://help.shopify.com/api/reference/products/collect
type CollectService interface {
List(context.Context, interface{}) ([]Collect, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*Collect, error)
Create(context.Context, Collect) (*Collect, error)
Delete(context.Context, uint64) error
}
// CollectServiceOp handles communication with the collect related methods of
// the Shopify API.
type CollectServiceOp struct {
client *Client
}
// Collect represents a Shopify collect
type Collect struct {
Id uint64 `json:"id,omitempty"`
CollectionId uint64 `json:"collection_id,omitempty"`
ProductId uint64 `json:"product_id,omitempty"`
Featured bool `json:"featured,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Position int `json:"position,omitempty"`
SortValue string `json:"sort_value,omitempty"`
}
// Represents the result from the collects/X.json endpoint
type CollectResource struct {
Collect *Collect `json:"collect"`
}
// Represents the result from the collects.json endpoint
type CollectsResource struct {
Collects []Collect `json:"collects"`
}
// List collects
func (s *CollectServiceOp) List(ctx context.Context, options interface{}) ([]Collect, error) {
path := fmt.Sprintf("%s.json", collectsBasePath)
resource := new(CollectsResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Collects, err
}
// Count collects
func (s *CollectServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", collectsBasePath)
return s.client.Count(ctx, path, options)
}
// Get individual collect
func (s *CollectServiceOp) Get(ctx context.Context, collectId uint64, options interface{}) (*Collect, error) {
path := fmt.Sprintf("%s/%d.json", collectsBasePath, collectId)
resource := new(CollectResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Collect, err
}
// Create collects
func (s *CollectServiceOp) Create(ctx context.Context, collect Collect) (*Collect, error) {
path := fmt.Sprintf("%s.json", collectsBasePath)
wrappedData := CollectResource{Collect: &collect}
resource := new(CollectResource)
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.Collect, err
}
// Delete an existing collect
func (s *CollectServiceOp) Delete(ctx context.Context, collectId uint64) error {
return s.client.Delete(ctx, fmt.Sprintf("%s/%d.json", collectsBasePath, collectId))
}