forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pages.go
131 lines (112 loc) · 4.07 KB
/
pages.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 wordpress
import (
"fmt"
"net/http"
)
type Page struct {
collection *PagesCollection `json:"-"`
ID int `json:"id,omitempty"`
Date string `json:"date,omitempty"`
DateGMT string `json:"date_gmt,omitempty"`
GUID GUID `json:"guid,omitempty"`
Link string `json:"link,omitempty"`
Modified string `json:"modified,omitempty"`
ModifiedGMT string `json:"modifiedGMT,omitempty"`
Password string `json:"password,omitempty"`
Slug string `json:"slug,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
Parent int `json:"parent,omitempty"`
Title Title `json:"title,omitempty"`
Content Content `json:"content,omitempty"`
Author int `json:"author,omitempty"`
Excerpt Excerpt `json:"excerpt,omitempty"`
FeaturedImage int `json:"featured_image,omitempty"`
CommentStatus string `json:"comment_status,omitempty"`
PingStatus string `json:"ping_status,omitempty"`
MenuOrder int `json:"menu_order,omitempty"`
Template string `json:"template,omitempty"`
}
func (entity *Page) setCollection(col *PagesCollection) {
entity.collection = col
}
func (entity *Page) Meta() *MetaCollection {
if entity.collection == nil {
// missing page.collection parent. Probably Page struct was initialized manually.
_warning("Missing parent page collection")
return nil
}
return &MetaCollection{
client: entity.collection.client,
parent: entity,
parentType: CollectionPages,
url: fmt.Sprintf("%v/%v/%v", entity.collection.url, entity.ID, CollectionMeta),
}
}
func (entity *Page) Revisions() *RevisionsCollection {
if entity.collection == nil {
// missing page.collection parent. Probably Page struct was initialized manually, not fetched from API
_warning("Missing parent page collection")
return nil
}
return &RevisionsCollection{
client: entity.collection.client,
parent: entity,
parentType: CollectionPages,
url: fmt.Sprintf("%v/%v/%v", entity.collection.url, entity.ID, CollectionRevisions),
}
}
func (entity *Page) Populate(params interface{}) (*Page, *http.Response, []byte, error) {
return entity.collection.Get(entity.ID, params)
}
type PagesCollection struct {
client *Client
url string
entityURL string
}
func (col *PagesCollection) List(params interface{}) ([]Page, *http.Response, []byte, error) {
var pages []Page
resp, body, err := col.client.List(col.url, params, &pages)
// set collection object for each entity which has sub-collection
for _, p := range pages {
p.setCollection(col)
}
return pages, resp, body, err
}
func (col *PagesCollection) Create(new *Page) (*Page, *http.Response, []byte, error) {
var created Page
resp, body, err := col.client.Create(col.url, new, &created)
created.setCollection(col)
return &created, resp, body, err
}
func (col *PagesCollection) Get(id int, params interface{}) (*Page, *http.Response, []byte, error) {
var entity Page
entityURL := fmt.Sprintf("%v/%v", col.url, id)
resp, body, err := col.client.Get(entityURL, params, &entity)
// set collection object for each entity which has sub-collection
entity.setCollection(col)
return &entity, resp, body, err
}
func (col *PagesCollection) Entity(id int) *Page {
entity := Page{
collection: col,
ID: id,
}
return &entity
}
func (col *PagesCollection) Update(id int, page *Page) (*Page, *http.Response, []byte, error) {
var updated Page
entityURL := fmt.Sprintf("%v/%v", col.url, id)
resp, body, err := col.client.Update(entityURL, page, &updated)
// set collection object for each entity which has sub-collection
updated.setCollection(col)
return &updated, resp, body, err
}
func (col *PagesCollection) Delete(id int, params interface{}) (*Page, *http.Response, []byte, error) {
var deleted Page
entityURL := fmt.Sprintf("%v/%v", col.url, id)
resp, body, err := col.client.Delete(entityURL, params, &deleted)
// set collection object for each entity which has sub-collection
deleted.setCollection(col)
return &deleted, resp, body, err
}