forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
137 lines (117 loc) · 4.74 KB
/
page.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
package goshopify
import (
"fmt"
"time"
)
const pagesBasePath = "pages"
const pagesResourceName = "pages"
// PagesPageService is an interface for interacting with the pages
// endpoints of the Shopify API.
// See https://help.shopify.com/api/reference/online_store/page
type PageService interface {
List(interface{}) ([]Page, error)
Count(interface{}) (int, error)
Get(int64, interface{}) (*Page, error)
Create(Page) (*Page, error)
Update(Page) (*Page, error)
Delete(int64) error
// MetafieldsService used for Pages resource to communicate with Metafields
// resource
MetafieldsService
}
// PageServiceOp handles communication with the page related methods of the
// Shopify API.
type PageServiceOp struct {
client *Client
}
// Page represents a Shopify page.
type Page struct {
ID int64 `json:"id"`
Author string `json:"author"`
Handle string `json:"handle"`
Title string `json:"title"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
BodyHTML string `json:"body_html"`
TemplateSuffix string `json:"template_suffix"`
PublishedAt *time.Time `json:"published_at"`
ShopID int64 `json:"shop_id"`
Metafields []Metafield `json:"metafields"`
}
// PageResource represents the result from the pages/X.json endpoint
type PageResource struct {
Page *Page `json:"page"`
}
// PagesResource represents the result from the pages.json endpoint
type PagesResource struct {
Pages []Page `json:"pages"`
}
// List pages
func (s *PageServiceOp) List(options interface{}) ([]Page, error) {
path := fmt.Sprintf("%s/%s.json", globalApiPathPrefix, pagesBasePath)
resource := new(PagesResource)
err := s.client.Get(path, resource, options)
return resource.Pages, err
}
// Count pages
func (s *PageServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/%s/count.json", globalApiPathPrefix, pagesBasePath)
return s.client.Count(path, options)
}
// Get individual page
func (s *PageServiceOp) Get(pageID int64, options interface{}) (*Page, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, pagesBasePath, pageID)
resource := new(PageResource)
err := s.client.Get(path, resource, options)
return resource.Page, err
}
// Create a new page
func (s *PageServiceOp) Create(page Page) (*Page, error) {
path := fmt.Sprintf("%s/%s.json", globalApiPathPrefix, pagesBasePath)
wrappedData := PageResource{Page: &page}
resource := new(PageResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Page, err
}
// Update an existing page
func (s *PageServiceOp) Update(page Page) (*Page, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, pagesBasePath, page.ID)
wrappedData := PageResource{Page: &page}
resource := new(PageResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Page, err
}
// Delete an existing page.
func (s *PageServiceOp) Delete(pageID int64) error {
return s.client.Delete(fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, pagesBasePath, pageID))
}
// List metafields for a page
func (s *PageServiceOp) ListMetafields(pageID int64, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: pagesResourceName, resourceID: pageID}
return metafieldService.List(options)
}
// Count metafields for a page
func (s *PageServiceOp) CountMetafields(pageID int64, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: pagesResourceName, resourceID: pageID}
return metafieldService.Count(options)
}
// Get individual metafield for a page
func (s *PageServiceOp) GetMetafield(pageID int64, metafieldID int64, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: pagesResourceName, resourceID: pageID}
return metafieldService.Get(metafieldID, options)
}
// Create a new metafield for a page
func (s *PageServiceOp) CreateMetafield(pageID int64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: pagesResourceName, resourceID: pageID}
return metafieldService.Create(metafield)
}
// Update an existing metafield for a page
func (s *PageServiceOp) UpdateMetafield(pageID int64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: pagesResourceName, resourceID: pageID}
return metafieldService.Update(metafield)
}
// Delete an existing metafield for a page
func (s *PageServiceOp) DeleteMetafield(pageID int64, metafieldID int64) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: pagesResourceName, resourceID: pageID}
return metafieldService.Delete(metafieldID)
}