Skip to content

Commit

Permalink
Add published param to page
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yomo committed Aug 25, 2024
1 parent c22a746 commit 2842304
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 15 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Page struct {
BodyHTML string `json:"body_html,omitempty"`
TemplateSuffix string `json:"template_suffix,omitempty"`
PublishedAt *time.Time `json:"published_at,omitempty"`
Published bool `json:"published,omitempty"` // Mainly used for create/update
ShopId uint64 `json:"shop_id,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
}
Expand All @@ -63,6 +64,11 @@ func (s *PageServiceOp) List(ctx context.Context, options interface{}) ([]Page,
path := fmt.Sprintf("%s.json", pagesBasePath)
resource := new(PagesResource)
err := s.client.Get(ctx, path, resource, options)
for i, page := range resource.Pages {
if page.PublishedAt != nil {
resource.Pages[i].Published = true
}
}
return resource.Pages, err
}

Expand All @@ -77,6 +83,9 @@ func (s *PageServiceOp) Get(ctx context.Context, pageId uint64, options interfac
path := fmt.Sprintf("%s/%d.json", pagesBasePath, pageId)
resource := new(PageResource)
err := s.client.Get(ctx, path, resource, options)
if resource.Page != nil && resource.Page.PublishedAt != nil {
resource.Page.Published = true
}
return resource.Page, err
}

Expand All @@ -86,6 +95,9 @@ func (s *PageServiceOp) Create(ctx context.Context, page Page) (*Page, error) {
wrappedData := PageResource{Page: &page}
resource := new(PageResource)
err := s.client.Post(ctx, path, wrappedData, resource)
if resource.Page != nil && resource.Page.PublishedAt != nil {
resource.Page.Published = true
}
return resource.Page, err
}

Expand All @@ -95,6 +107,9 @@ func (s *PageServiceOp) Update(ctx context.Context, page Page) (*Page, error) {
wrappedData := PageResource{Page: &page}
resource := new(PageResource)
err := s.client.Put(ctx, path, wrappedData, resource)
if resource.Page != nil && resource.Page.PublishedAt != nil {
resource.Page.Published = true
}
return resource.Page, err
}

Expand Down
11 changes: 7 additions & 4 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ func TestPageList(t *testing.T) {
defer teardown()

httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/pages.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"pages": [{"id":1},{"id":2}]}`))
httpmock.NewStringResponder(200, `{"pages": [{"id":1,"published_at": "2008-07-15T20:00:00Z"},{"id":2,"published_at": "2008-07-15T21:00:00Z"}]}`))

pages, err := client.Page.List(context.Background(), nil)
if err != nil {
t.Errorf("Page.List returned error: %v", err)
}

expected := []Page{{Id: 1}, {Id: 2}}
expected := []Page{
{Id: 1, PublishedAt: TimePtr(time.Date(2008, 7, 15, 20, 0, 0, 0, time.UTC)), Published: true},
{Id: 2, PublishedAt: TimePtr(time.Date(2008, 7, 15, 21, 0, 0, 0, time.UTC)), Published: true},
}
if !reflect.DeepEqual(pages, expected) {
t.Errorf("Page.List returned %+v, expected %+v", pages, expected)
}
Expand Down Expand Up @@ -77,14 +80,14 @@ func TestPageGet(t *testing.T) {
defer teardown()

httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/pages/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"page": {"id":1}}`))
httpmock.NewStringResponder(200, `{"page": {"id":1,"published_at": "2008-07-15T20:00:00Z"}}`))

page, err := client.Page.Get(context.Background(), 1, nil)
if err != nil {
t.Errorf("Page.Get returned error: %v", err)
}

expected := &Page{Id: 1}
expected := &Page{Id: 1, PublishedAt: TimePtr(time.Date(2008, 7, 15, 20, 0, 0, 0, time.UTC)), Published: true}
if !reflect.DeepEqual(page, expected) {
t.Errorf("Page.Get returned %+v, expected %+v", page, expected)
}
Expand Down
4 changes: 4 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ func (c *OnlyDate) EncodeValues(key string, v *url.Values) error {
func (c *OnlyDate) String() string {
return `"` + c.Format("2006-01-02") + `"`
}

func TimePtr(v time.Time) *time.Time {
return &v
}

0 comments on commit 2842304

Please sign in to comment.