This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
collection.go
131 lines (99 loc) · 2.73 KB
/
collection.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
package contentful
import (
"bytes"
"encoding/json"
"net/http"
)
// CollectionOptions holds init options
type CollectionOptions struct {
Limit uint16
}
// Collection model
type Collection struct {
Query
c *Client
req *http.Request
page uint16
Sys *Sys `json:"sys"`
Total int `json:"total"`
Skip int `json:"skip"`
Limit int `json:"limit"`
Items []interface{} `json:"items"`
Includes interface{} `json:"includes"`
}
// NewCollection initilazies a new collection
func NewCollection(options *CollectionOptions) *Collection {
query := NewQuery()
query.Order("sys.createdAt", true)
if options.Limit > 0 {
query.Limit(options.Limit)
}
return &Collection{
Query: *query,
page: 1,
}
}
// Next makes the col.req
func (col *Collection) Next() (*Collection, error) {
// setup query params
skip := uint16(col.Limit) * (col.page - 1)
col.Query.Skip(skip)
// override request query
col.req.URL.RawQuery = col.Query.String()
// makes api call
err := col.c.do(col.req, col)
if err != nil {
return nil, err
}
col.page++
return col, nil
}
// ToContentType cast Items to ContentType model
func (col *Collection) ToContentType() []*ContentType {
var contentTypes []*ContentType
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&contentTypes)
return contentTypes
}
// ToSpace cast Items to Space model
func (col *Collection) ToSpace() []*Space {
var spaces []*Space
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&spaces)
return spaces
}
// ToEntry cast Items to Entry model
func (col *Collection) ToEntry() []*Entry {
var entries []*Entry
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&entries)
return entries
}
// ToLocale cast Items to Locale model
func (col *Collection) ToLocale() []*Locale {
var locales []*Locale
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&locales)
return locales
}
// ToAsset cast Items to Asset model
func (col *Collection) ToAsset() []*Asset {
var assets []*Asset
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&assets)
return assets
}
// ToAPIKey cast Items to APIKey model
func (col *Collection) ToAPIKey() []*APIKey {
var apiKeys []*APIKey
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&apiKeys)
return apiKeys
}
// ToWebhook cast Items to Webhook model
func (col *Collection) ToWebhook() []*Webhook {
var webhooks []*Webhook
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&webhooks)
return webhooks
}