forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discount_code.go
118 lines (99 loc) · 4.21 KB
/
discount_code.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
package goshopify
import (
"fmt"
"net/http"
"time"
)
const discountCodeBasePath = "price_rules/%d/discount_codes"
// DiscountCodeService is an interface for interfacing with the discount endpoints
// of the Shopify API.
// See: https://help.shopify.com/en/api/reference/discounts/PriceRuleDiscountCode
type DiscountCodeService interface {
Create(int64, PriceRuleDiscountCode) (*PriceRuleDiscountCode, error)
Update(int64, PriceRuleDiscountCode) (*PriceRuleDiscountCode, error)
List(int64) ([]PriceRuleDiscountCode, error)
ListWithPagination(int64, interface{}) ([]PriceRuleDiscountCode, *Pagination, error)
Count(int64, interface{}) (int, error)
Get(int64, int64) (*PriceRuleDiscountCode, error)
Delete(int64, int64) error
}
// DiscountCodeServiceOp handles communication with the discount code
// related methods of the Shopify API.
type DiscountCodeServiceOp struct {
client *Client
}
type PriceRuleDiscountCodeListOptions struct {
ListOptions
}
// PriceRuleDiscountCode represents a Shopify Discount Code
type PriceRuleDiscountCode struct {
ID int64 `json:"id,omitempty"`
PriceRuleID int64 `json:"price_rule_id,omitempty"`
Code string `json:"code,omitempty"`
UsageCount int `json:"usage_count,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
// DiscountCodesResource is the result from the discount_codes.json endpoint
type DiscountCodesResource struct {
DiscountCodes []PriceRuleDiscountCode `json:"discount_codes"`
}
// DiscountCodeResource represents the result from the discount_codes/X.json endpoint
type DiscountCodeResource struct {
PriceRuleDiscountCode *PriceRuleDiscountCode `json:"discount_code"`
}
// Create a discount code
func (s *DiscountCodeServiceOp) Create(priceRuleID int64, dc PriceRuleDiscountCode) (*PriceRuleDiscountCode, error) {
path := fmt.Sprintf(discountCodeBasePath+".json", priceRuleID)
wrappedData := DiscountCodeResource{PriceRuleDiscountCode: &dc}
resource := new(DiscountCodeResource)
err := s.client.Post(path, wrappedData, resource)
return resource.PriceRuleDiscountCode, err
}
// Update an existing discount code
func (s *DiscountCodeServiceOp) Update(priceRuleID int64, dc PriceRuleDiscountCode) (*PriceRuleDiscountCode, error) {
path := fmt.Sprintf(discountCodeBasePath+"/%d.json", priceRuleID, dc.ID)
wrappedData := DiscountCodeResource{PriceRuleDiscountCode: &dc}
resource := new(DiscountCodeResource)
err := s.client.Put(path, wrappedData, resource)
return resource.PriceRuleDiscountCode, err
}
// List of discount codes
func (s *DiscountCodeServiceOp) List(priceRuleID int64) ([]PriceRuleDiscountCode, error) {
path := fmt.Sprintf(discountCodeBasePath+".json", priceRuleID)
resource := new(DiscountCodesResource)
err := s.client.Get(path, resource, nil)
return resource.DiscountCodes, err
}
func (s *DiscountCodeServiceOp) ListWithPagination(priceRuleID int64, options interface{}) ([]PriceRuleDiscountCode, *Pagination, error) {
path := fmt.Sprintf(discountCodeBasePath+".json", priceRuleID)
resource := new(DiscountCodesResource)
headers := http.Header{}
headers, err := s.client.createAndDoGetHeaders("GET", path, nil, options, resource)
if err != nil {
return nil, nil, err
}
// Extract pagination info from header
linkHeader := headers.Get("Link")
pagination, err := extractPagination(linkHeader)
if err != nil {
return nil, nil, err
}
return resource.DiscountCodes, pagination, nil
}
// Count discount codes
func (s *DiscountCodeServiceOp) Count(priceRuleID int64, options interface{}) (int, error) {
path := fmt.Sprintf(discountCodeBasePath+"/count.json", priceRuleID)
return s.client.Count(path, options)
}
// Get a single discount code
func (s *DiscountCodeServiceOp) Get(priceRuleID int64, discountCodeID int64) (*PriceRuleDiscountCode, error) {
path := fmt.Sprintf(discountCodeBasePath+"/%d.json", priceRuleID, discountCodeID)
resource := new(DiscountCodeResource)
err := s.client.Get(path, resource, nil)
return resource.PriceRuleDiscountCode, err
}
// Delete a discount code
func (s *DiscountCodeServiceOp) Delete(priceRuleID int64, discountCodeID int64) error {
return s.client.Delete(fmt.Sprintf(discountCodeBasePath+"/%d.json", priceRuleID, discountCodeID))
}