/
post.go
149 lines (127 loc) · 3.81 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
package docbase
import (
"context"
"fmt"
"time"
)
// PostService handles communication with the Post related
// methods of the Docbase API.
type PostService service
// PostListOptions specifies the optional parameters to the
// PostService.List method.
type PostListOptions struct {
// Query filters Post.
//
// docs: https://help.docbase.io/posts/59432?list=%2Fsearch&q=%E6%A4%9C%E7%B4%A2#%E6%A4%9C%E7%B4%A2%E3%82%AA%E3%83%97%E3%82%B7%E3%83%A7%E3%83%B3
Query string `url:"q,omitempty"`
ListOptions
}
// List posts in a domain.
// To build query, use PostQueryXXX functions.
//
// Docbase API docs: https://help.docbase.io/posts/92984
// Docbase query docs: https://help.docbase.io/posts/59432
func (s *PostService) List(ctx context.Context, domain Domain, opt *PostListOptions) ([]Post, *Response, error) {
u := fmt.Sprintf("teams/%v/posts", domain)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var posts struct {
Meta Meta `json:"meta"`
Posts []Post `json:"posts"`
}
resp, err := s.client.Do(ctx, req, &posts)
if err != nil {
return nil, resp, err
}
resp.Meta = posts.Meta
return posts.Posts, resp, nil
}
// Get a single post.
//
// Docbase API docs: https://help.docbase.io/posts/97204
func (s *PostService) Get(ctx context.Context, domain Domain, id PostID) (*Post, *Response, error) {
u := fmt.Sprintf("teams/%v/posts/%v", domain, id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
post := new(Post)
resp, err := s.client.Do(ctx, req, post)
if err != nil {
return nil, resp, err
}
return post, resp, nil
}
// CreatePostOptions represents a Docbase Post.
type CreatePostOptions struct {
// Required.
Title string `json:"title"`
Body string `json:"body"`
// Option.
Draft bool `json:"draft,omitempty"`
Groups []GroupID `json:"groups,omitempty"`
Scope Scope `json:"scope,omitempty"`
Tags []string `json:"tags,omitempty"`
Notice bool `json:"notice,omitempty"`
// Parameters that only owners can use.
AuthorID *UserID `json:"author_id,omitempty"`
PublishedAt *time.Time `json:"published_at,omitempty"`
}
// Create a post for a domain.
//
// Docbase API docs: https://help.docbase.io/posts/92980
func (s *PostService) Create(ctx context.Context, domain Domain, post *CreatePostOptions) (*Post, *Response, error) {
u := fmt.Sprintf("teams/%v/posts", domain)
req, err := s.client.NewRequest("POST", u, post)
if err != nil {
return nil, nil, err
}
g := new(Post)
resp, err := s.client.Do(ctx, req, g)
if err != nil {
return nil, resp, err
}
return g, resp, nil
}
// Edit a post.
//
// Docbase API docs: https://help.docbase.io/posts/92981
func (s *PostService) Edit(ctx context.Context, domain Domain, id PostID, post *PostEditRequest) (*Post, *Response, error) {
u := fmt.Sprintf("teams/%v/posts/%v", domain, id)
req, err := s.client.NewRequest("PATCH", u, post)
if err != nil {
return nil, nil, err
}
g := new(Post)
resp, err := s.client.Do(ctx, req, g)
if err != nil {
return nil, resp, err
}
return g, resp, nil
}
// PostEditRequest represents a request body for `Edit` function.
type PostEditRequest struct {
Body *string `json:"body,omitempty"`
Draft *bool `json:"draft,omitempty"`
Groups []GroupID `json:"groups,omitempty"`
Scope *Scope `json:"scope,omitempty"`
Tags []string `json:"tags,omitempty"`
Title *string `json:"title,omitempty"`
}
// Delete a post.
//
// Docbase API docs: https://help.docbase.io/posts/92982
func (s *PostService) Delete(ctx context.Context, domain Domain, id PostID) (*Response, error) {
u := fmt.Sprintf("teams/%v/posts/%v", domain, id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}