Skip to content

Commit

Permalink
Fix not to set published based on published_at
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yomo committed Aug 29, 2024
1 parent 2842304 commit 72300a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
40 changes: 14 additions & 26 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ type PageServiceOp struct {

// Page represents a Shopify page.
type Page struct {
Id uint64 `json:"id,omitempty"`
Author string `json:"author,omitempty"`
Handle string `json:"handle,omitempty"`
Title string `json:"title,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
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"`
Id uint64 `json:"id,omitempty"`
Author string `json:"author,omitempty"`
Handle string `json:"handle,omitempty"`
Title string `json:"title,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
BodyHTML string `json:"body_html,omitempty"`
TemplateSuffix string `json:"template_suffix,omitempty"`
PublishedAt *time.Time `json:"published_at,omitempty"`
// Published can be set when creating a new page.
// It's not returned in the response
Published *bool `json:"published,omitempty"`
ShopId uint64 `json:"shop_id,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
}

// PageResource represents the result from the pages/X.json endpoint
Expand All @@ -64,11 +66,6 @@ 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 @@ -83,9 +80,6 @@ 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 @@ -95,9 +89,6 @@ 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 @@ -107,9 +98,6 @@ 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
6 changes: 3 additions & 3 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestPageList(t *testing.T) {
}

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},
{Id: 1, PublishedAt: TimePtr(time.Date(2008, 7, 15, 20, 0, 0, 0, time.UTC))},
{Id: 2, PublishedAt: TimePtr(time.Date(2008, 7, 15, 21, 0, 0, 0, time.UTC))},
}
if !reflect.DeepEqual(pages, expected) {
t.Errorf("Page.List returned %+v, expected %+v", pages, expected)
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestPageGet(t *testing.T) {
t.Errorf("Page.Get returned error: %v", err)
}

expected := &Page{Id: 1, PublishedAt: TimePtr(time.Date(2008, 7, 15, 20, 0, 0, 0, time.UTC)), Published: true}
expected := &Page{Id: 1, PublishedAt: TimePtr(time.Date(2008, 7, 15, 20, 0, 0, 0, time.UTC))}
if !reflect.DeepEqual(page, expected) {
t.Errorf("Page.Get returned %+v, expected %+v", page, expected)
}
Expand Down

0 comments on commit 72300a2

Please sign in to comment.