diff --git a/collection.go b/collection.go index 5256a44..4adb7c3 100644 --- a/collection.go +++ b/collection.go @@ -141,6 +141,16 @@ func (col *Collection) ToSpace() []*Space { return spaces } +// ToScheduledAction cast Items to ScheduledActions model +func (col *Collection) ToScheduledAction() []*ScheduledActions { + var scheduledActions []*ScheduledActions + + byteArray, _ := json.Marshal(col.Items) + json.NewDecoder(bytes.NewReader(byteArray)).Decode(&scheduledActions) + + return scheduledActions +} + // ToEntry cast Items to Entry model func (col *Collection) ToEntry() []*Entry { var entries []*Entry diff --git a/contentful.go b/contentful.go index da64a3d..9792b36 100644 --- a/contentful.go +++ b/contentful.go @@ -25,13 +25,14 @@ type Contentful struct { BaseURL string Environment string - Spaces *SpacesService - APIKeys *APIKeyService - Assets *AssetsService - ContentTypes *ContentTypesService - Entries *EntriesService - Locales *LocalesService - Webhooks *WebhooksService + Spaces *SpacesService + APIKeys *APIKeyService + Assets *AssetsService + ContentTypes *ContentTypesService + Entries *EntriesService + Locales *LocalesService + Webhooks *WebhooksService + ScheduledActions *ScheduledActionsService } type service struct { @@ -68,6 +69,7 @@ func NewCMA(token string) *Contentful { c.Entries = &EntriesService{c: c} c.Locales = &LocalesService{c: c} c.Webhooks = &WebhooksService{c: c} + c.ScheduledActions = &ScheduledActionsService{c: c} return c } diff --git a/scheduled_actions.go b/scheduled_actions.go new file mode 100644 index 0000000..02d5ddf --- /dev/null +++ b/scheduled_actions.go @@ -0,0 +1,53 @@ +package contentful + +import ( + "fmt" + "net/url" +) + +// SpacesService model +type ScheduledActionsService service + +type ScheduledFor struct { + Datetime string `json:"datetime,omitempty"` + Timezone string `json:"timezone,omitempty"` +} + +// ScheduledActions model +type ScheduledActions struct { + Sys *Sys `json:"sys,omitempty"` + Action string `json:"action,omitempty"` + ScheduledFor *ScheduledFor `json:"scheduledFor,omitempty"` +} + +// Get returns a single scheduledActions entity +func (service *ScheduledActionsService) Get(spaceID string, entryID string, environmentID string) (*ScheduledActions, error) { + path := fmt.Sprintf("/spaces/%s/scheduled_actions", spaceID) + + query := url.Values{} + + query.Add("entity.sys.id", entryID) + query.Add("environment.sys.id", environmentID) + query.Add("sys.status[in]", "scheduled") + + method := "GET" + + req, err := service.c.newRequest(method, path, query, nil) + if err != nil { + return &ScheduledActions{}, err + } + + col := NewCollection(&CollectionOptions{}) + col.c = service.c + col.req = req + + if ok := service.c.do(req, &col); ok != nil { + return &ScheduledActions{}, ok + } + + for _, ct := range col.ToScheduledAction() { + fmt.Println(ct) + } + + return &ScheduledActions{}, nil +} diff --git a/scheduled_actions_test.go b/scheduled_actions_test.go new file mode 100644 index 0000000..31402e2 --- /dev/null +++ b/scheduled_actions_test.go @@ -0,0 +1,24 @@ +package contentful + +import ( + "fmt" + "log" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_ExampleScheduledActionsService_Get(t *testing.T) { + + cma := NewCMA("cma-token") + assert.NotNil(t, cma) + assert.NotNil(t, cma.ScheduledActions) + + scheduledActions, err := cma.ScheduledActions.Get("space-id", "entry-id", "env") + + if err != nil { + log.Fatal(err) + } + + fmt.Printf("%v", scheduledActions) +}