-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles_models.go
163 lines (145 loc) · 4.8 KB
/
articles_models.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
package reamaze
import (
"net/url"
"strconv"
"strings"
"time"
)
const articlesEndpoint string = "/api/v1/articles"
type ReamazeArticleStatus int
type ReamazeArticleQuery string
type ReamazeArticlePage int
const (
ReamazeArticleStatusPublished ReamazeArticleStatus = 0
ReamazeArticleStatusDraft ReamazeArticleStatus = 1
ReamazeArticleStatusInternal ReamazeArticleStatus = 2
)
type ArticlesOption interface {
Apply(*ReamazeArticlesOptions)
}
func (w ReamazeArticleStatus) Apply(o *ReamazeArticlesOptions) {
if w > 0 {
o.ReamazeArticlesStatus = "status=" + strconv.Itoa(int(w))
}
}
func (w ReamazeArticlePage) Apply(o *ReamazeArticlesOptions) {
if w > 0 {
o.ReamazeArticlesPage = "page=" + strconv.Itoa(int(w))
}
}
func (w ReamazeArticleQuery) Apply(o *ReamazeArticlesOptions) {
if len(w) > 0 {
o.ReamazeArticlesQuery = "q=" + url.PathEscape(string(w))
}
}
func WithArticleQuery(query string) ReamazeArticleQuery {
return ReamazeArticleQuery(query)
}
func WithArticleStatus(status ReamazeArticleStatus) ReamazeArticleStatus {
return status
}
func WithArticlePage(page int) ReamazeArticlePage {
return ReamazeArticlePage(page)
}
func newArticlesSettings(opts []ArticlesOption) (*ReamazeArticlesOptions, error) {
var o ReamazeArticlesOptions
for _, opt := range opts {
opt.Apply(&o)
}
return &o, nil
}
func (r ReamazeArticlesOptions) GetQuery() string {
output := ""
var queryParams []string
// checking if status is set
if len(r.ReamazeArticlesStatus) > 0 {
queryParams = append(queryParams, r.ReamazeArticlesStatus)
}
// checking if query is set
if len(r.ReamazeArticlesQuery) > 0 {
queryParams = append(queryParams, r.ReamazeArticlesQuery)
}
// checking if page is set
if len(r.ReamazeArticlesPage) > 0 {
queryParams = append(queryParams, r.ReamazeArticlesPage)
}
output = strings.Join(queryParams, "&")
if len(output) > 0 {
output = "?" + output
}
return output
}
type ReamazeArticlesOptions struct {
ReamazeArticlesStatus string
ReamazeArticlesQuery string
ReamazeArticlesPage string
}
type GetArticlesResponse struct {
PageSize int `json:"page_size,omitempty"`
PageCount int `json:"page_count,omitempty"`
TotalCount int `json:"total_count,omitempty"`
Articles []struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Slug string `json:"slug,omitempty"`
Status int `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
Author struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Data any `json:"data,omitempty"`
Email string `json:"email,omitempty"`
Twitter string `json:"twitter,omitempty"`
Facebook string `json:"facebook,omitempty"`
Instagram string `json:"instagram,omitempty"`
Mobile string `json:"mobile,omitempty"`
FriendlyName string `json:"friendly_name,omitempty"`
DisplayName string `json:"display_name,omitempty"`
} `json:"author,omitempty"`
EmbeddedURL string `json:"embedded_url,omitempty"`
Topic struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
} `json:"topic,omitempty"`
} `json:"articles,omitempty"`
}
type ReamazeArticle struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Slug string `json:"slug,omitempty"`
Status int `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
Author struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Data any `json:"data,omitempty"`
Email string `json:"email,omitempty"`
Twitter string `json:"twitter,omitempty"`
Facebook string `json:"facebook,omitempty"`
Instagram string `json:"instagram,omitempty"`
Mobile string `json:"mobile,omitempty"`
FriendlyName string `json:"friendly_name,omitempty"`
DisplayName string `json:"display_name,omitempty"`
} `json:"author,omitempty"`
EmbeddedURL string `json:"embedded_url,omitempty"`
Topic struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
} `json:"topic,omitempty"`
}
type GetArticleResponse ReamazeArticle
type UpdateArticleResponse ReamazeArticle
type UpdateArticleRequest ReamazeArticle
type CreateArticleResponse ReamazeArticle
type CreateArticleRequest struct {
Article struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Status ReamazeArticleStatus `json:"status,omitempty"`
TopicID string `json:"topic_id,omitempty"`
} `json:"article,omitempty"`
}