-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
276 lines (219 loc) · 6.24 KB
/
post.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package docbase
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// PostService implements interface with API /posts endpoint.
// See https://help.docbase.io/posts/45703#%E3%82%BF%E3%82%B0
type PostService interface {
List(opts *PostListOptions) ([]*Post, *Response, error)
Get(postID int) (*Post, *Response, error)
Create(postRequest *PostCreateRequest) (*Post, *Response, error)
Update(postID int, postUpdateRequest *PostUpdateRequest) (*Post, *Response, error)
Delete(postID string) (*Response, error)
Archive(postID int) (*Response, error)
Unarchive(postID int) (*Response, error)
}
// postService handles communication with API
type postService struct {
client *Client
}
// PostCreateRequest identifies Post for the Create request
type PostCreateRequest struct {
Title string `json:"title"`
Body string `json:"body"`
Draft bool `json:"draft"` // optional, default: false
Notice bool `json:"notice"` // optional, default: true
Tags []string `json:"tags"`
Scope string `json:"scope"` // optional, default: everyone
Groups []string `json:"groups"`
AuthorID string `json:"author_id"`
PublishedAt time.Time `json:"published_at"`
}
// PostUpdateRequest identifies Post for the Update request
type PostUpdateRequest struct {
Title string `json:"title"`
Body string `json:"body"`
Draft bool `json:"draft"` // optional, default: false
Notice bool `json:"notice"` // optional, default: true
Tags []string `json:"tags"`
Scope string `json:"scope"` // optional, default: everyone
Groups []string `json:"groups"`
PublishedAt time.Time `json:"published_at"`
}
type PostListResponse struct {
Posts []*Post `json:"posts"`
Meta struct {
PreviousPage string `json:"previous_page"`
NextPage string `json:"next_page"`
Total int `json:"total"`
} `json:"meta"`
}
// Post represents a DocBase Post
type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Draft bool `json:"draft"`
Archived bool `json:"archived"`
URL string `json:"url"`
CreatedAt time.Time `json:"created_at"`
Tags []Tag `json:"tags"`
Scope string `json:"scope"`
SharingURL string `json:"sharing_url"`
User SimpleUser `json:"user"`
StarsCount int `json:"stars_count"`
GoodJobsCount int `json:"good_jobs_count"`
Comments []Comment `json:"comments"`
Groups []SimpleGroup `json:"groups"`
Attachments []Attachment `json:"attachments"`
}
// PostListOptions identifies as query params of Post List request
type PostListOptions struct {
Q string `url:"q,omitempty"`
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
}
func (opts *PostListOptions) SetDefaultSort() {
if !strings.Contains(opts.Q, "desc:") {
opts.Q += " desc:score"
}
}
// List Post
func (s *postService) List(opts *PostListOptions) ([]*Post, *Response, error) {
u, err := url.Parse("/posts")
if err != nil {
return nil, nil, err
}
opts.SetDefaultSort()
q := u.Query()
q.Set("per_page", strconv.Itoa(opts.PerPage))
q.Set("page", strconv.Itoa(opts.Page))
q.Set("q", opts.Q)
u.RawQuery = q.Encode()
req, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}
posts := &PostListResponse{}
resp, err := s.client.Do(req, posts)
if err != nil {
return nil, nil, err
}
resp.Total = posts.Meta.Total
resp.NextPage = posts.Meta.NextPage
resp.PreviousPage = posts.Meta.PreviousPage
return posts.Posts, resp, err
}
// Get Post
func (s *postService) Get(postID int) (*Post, *Response, error) {
u, err := url.Parse(fmt.Sprintf("/posts/%d", postID))
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}
post := &Post{}
resp, err := s.client.Do(req, post)
if err != nil {
return nil, nil, err
}
return post, resp, err
}
// Create Post
func (s *postService) Create(memoReq *PostCreateRequest) (*Post, *Response, error) {
u, err := url.Parse("/posts")
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodPost, u.String(), memoReq)
if err != nil {
return nil, nil, err
}
mResp := &Post{}
resp, err := s.client.Do(req, mResp)
if err != nil {
return nil, nil, err
}
return mResp, resp, err
}
// Update Post
func (s *postService) Update(postID int, postUpdateRequest *PostUpdateRequest) (*Post, *Response, error) {
u, err := url.Parse(fmt.Sprintf("/posts/%d", postID))
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodPatch, u.String(), postUpdateRequest)
if err != nil {
return nil, nil, err
}
mResp := &Post{}
resp, err := s.client.Do(req, mResp)
if err != nil {
return nil, nil, err
}
if err != nil {
return nil, nil, err
}
return mResp, resp, err
}
// Delete Post
func (s *postService) Delete(postID string) (*Response, error) {
u, err := url.Parse(fmt.Sprintf("/posts/%s", postID))
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodDelete, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// Archive Post
func (s *postService) Archive(postID int) (*Response, error) {
u, err := url.Parse(fmt.Sprintf("/posts/%d/archive", postID))
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodPut, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return resp, err
}
// Unarchive Post
func (s *postService) Unarchive(postID int) (*Response, error) {
u, err := url.Parse(fmt.Sprintf("/posts/%d/unarchive", postID))
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodPut, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return resp, err
}