-
Notifications
You must be signed in to change notification settings - Fork 4
/
article.go
201 lines (161 loc) · 4.78 KB
/
article.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
package zed
import "fmt"
// Article struct
type Article struct {
ID *float64 `json:"id,omitempty"`
URL *string `json:"url,omitempty"`
HtmlURL *string `json:"html_url,omitempty"`
Title *string `json:"title,omitempty"`
Name *string `json:"name,omitempty"`
Body *string `json:"body,omitempty"`
Locale *string `json:"locale,omitempty"`
SourceLocale *string `json:"source_locale,omitempty"`
AuthorID *float64 `json:"author_id,omitempty"`
CommentsDisabled *bool `json:"comments_disabled,omitempty"`
Outdated *bool `json:"outdated,omitempty"`
Labels []string `json:"label_names,omitempty"`
Draft *bool `json:"draft,omitempty"`
Promoted *bool `json:"promoted,omitempty"`
Position *int64 `json:"position,omitempty"`
VoteSum *int64 `json:"vote_sum,omitempty"`
VoteCount *int64 `json:"vote_count,omitempty"`
SectionID *float64 `json:"section_id,omitempty"`
TranslationIds []int64 `json:"translation_ids,omitempty"`
CreatedAt *string `json:"created_at,omitempty"`
UpdatedAt *string `json:"updated_at,omitempty"`
}
// ArticleResponse struct
type ArticleResponse struct {
Article *Article `json:"article"`
}
// ArticleCollectionResponse struct
type ArticleCollectionResponse struct {
Results []Article `json:"articles,omitempty"`
Count *int64 `json:"count,omitempty"`
Next *string `json:"next_page,omitempty"`
Page *int64 `json:"page,omitempty"`
PageCount *int64 `json:"page_count,omitempty"`
PerPage *int64 `json:"per_page,omitempty"`
Previous *string `json:"previous_page,omitempty"`
}
// ArticleService struct
type ArticleService struct {
client *Client
}
// List function
func (s *ArticleService) List() ([]Article, error) {
resource := []Article{}
rp, next, _, err := s.getPage("", "")
if err != nil {
return resource, err
}
resource = append(resource, *rp...)
for next != nil {
rp, nx, _, err := s.getPage(*next, "")
if err != nil {
return resource, err
}
next = nx
resource = append(resource, *rp...)
}
return resource, err
}
func (s *ArticleService) getPage(url, locale string) (*[]Article, *string, *Response, error) {
if url == "" {
url = "help_center/articles.json"
}
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, nil, err
}
result := ArticleCollectionResponse{}
resp, err := s.client.Do(req, result)
if err != nil {
return nil, nil, resp, err
}
next := result.Next
resource := result.Results
return &resource, next, resp, err
}
// Create func creates a single new article
func (s *ArticleService) Create(a *Article) (*Article, error) {
article := &Article{}
if a.SectionID == nil {
return article, fmt.Errorf("missing required field: section id")
}
if a.Title == nil {
return article, fmt.Errorf("missing required field: article title")
}
if a.Body == nil {
return article, fmt.Errorf("missing required field: article body")
}
l := "en-us"
if a.Locale == nil {
a.Locale = &l
}
ar := &ArticleResponse{Article: a}
url := fmt.Sprintf("help_center/sections/%v/articles.json", int(*a.SectionID))
req, err := s.client.NewRequest("POST", url, ar)
if err != nil {
return article, err
}
result := ArticleResponse{}
_, err = s.client.Do(req, result)
if err != nil {
return article, err
}
article = result.Article
return article, err
}
// Update func updates a single article
func (s *ArticleService) Update(a *Article) error {
if a.ID == nil {
return fmt.Errorf("missing required field: article id")
}
if a.Title == nil {
return fmt.Errorf("missing required field: article title")
}
if a.Body == nil {
return fmt.Errorf("missing required field: article body")
}
l := "en-us"
if a.Locale == nil {
a.Locale = &l
}
t := articleToTranslation(*a)
tr := &TranslationResponse{Translation: t}
url := fmt.Sprintf("help_center/articles/%v/translations/%v.json", int(*a.ID), *a.Locale)
req, err := s.client.NewRequest("PUT", url, tr)
if err != nil {
return err
}
_, err = s.client.Do(req, nil)
if err != nil {
return err
}
return err
}
// Delete func deletes a single article
func (s *ArticleService) Delete(id *int64) error {
if id == nil {
return fmt.Errorf("missing article id")
}
url := fmt.Sprintf("help_center/articles/%v.json", int(*id))
req, err := s.client.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("creating new request failed: %v\n", err)
}
_, err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("request failed with: %v\n", err)
}
return err
}
func articleToTranslation(a Article) *Translation {
return &Translation{
Title: a.Title,
Body: a.Body,
Draft: a.Draft,
Outdated: a.Outdated,
}
}