forked from contentful-labs/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
149 lines (115 loc) · 2.93 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package contentful
import (
"bytes"
"encoding/json"
"net/http"
)
// CollectionOptions holds init options
type CollectionOptions struct {
Limit uint16
}
// Collection model
type Collection struct {
Query
c *Contentful
s *Space
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"`
}
// 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 := col.Query.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)
for _, contentType := range contentTypes {
contentType.s = col.s
contentType.c = col.c
}
return contentTypes
}
// ToSpace cast Items to ContentType model
func (col *Collection) ToSpace() []*Space {
var spaces []*Space
byteArray, _ := json.Marshal(col.Items)
json.NewDecoder(bytes.NewReader(byteArray)).Decode(&spaces)
for _, space := range spaces {
space.c = col.c
}
return spaces
}
// 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)
for _, locale := range locales {
locale.s = col.s
locale.c = col.c
}
return locales
}
// ToAsset cast Items to Asset model
func (col *Collection) ToAsset() []*Asset {
var assets []*Asset
for _, rawAsset := range col.Items {
asset, _ := newAssetFromAPIResponse(rawAsset.(map[string]interface{}), "en-US")
asset.s = col.s
asset.c = col.c
assets = append(assets, asset)
}
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)
for _, apiKey := range apiKeys {
apiKey.c = col.c
apiKey.s = col.s
}
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)
for _, webhook := range webhooks {
webhook.c = col.c
webhook.s = col.s
}
return webhooks
}