-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
257 lines (211 loc) · 6.77 KB
/
api.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
package post
import (
"github.com/ottemo/commerce/api"
"github.com/ottemo/commerce/db"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/app/models"
"github.com/ottemo/commerce/app/models/blog/post"
)
func setupAPI() error {
service := api.GetRestService()
service.GET("blog/posts", APIListPosts)
service.GET("blog/post/:id", APIPostByID)
service.GET("blog/posts/attributes", APIListPostAttributes)
// admin
service.POST("blog/post", api.IsAdminHandler(APICreateBlogPost))
service.PUT("blog/post/:id", api.IsAdminHandler(APIUpdateByID))
service.DELETE("blog/post/:id", api.IsAdminHandler(APIDeleteByID))
return nil
}
// APIListPosts returns list of posts
func APIListPosts(context api.InterfaceApplicationContext) (interface{}, error) {
collection, err := db.GetCollection(ConstBlogPostCollectionName)
if err != nil {
return nil, env.ErrorDispatch(err)
}
// filters handle
if err = models.ApplyFilters(context, collection); err != nil {
return nil, env.ErrorDispatch(err)
}
// not allowing to see disabled articles if not admin
if !api.IsAdminSession(context) {
if err = collection.AddGroupFilter("and_published", "published", "=", true); err != nil {
return nil, env.ErrorDispatch(err)
}
}
// check "count" request
if context.GetRequestArgument(api.ConstRESTActionParameter) == "count" {
if count, err := collection.Count(); err == nil {
return count, nil
}
return nil, env.ErrorDispatch(err)
}
result, err := collection.Load()
if err != nil {
return nil, env.ErrorDispatch(err)
}
return result, nil
}
// APIPostByID returns post by id
func APIPostByID(context api.InterfaceApplicationContext) (interface{}, error) {
blogPostID := context.GetRequestArgument("id")
if blogPostID == "" {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "331e8869-8d6a-4603-8cf8-9a84266fdad1", "post id was not specified")
}
postModel, err := post.GetBlogPostModel()
if err != nil {
return nil, env.ErrorDispatch(err)
}
if err = postModel.Load(blogPostID); err != nil {
return nil, env.ErrorDispatch(err)
}
result := postModel.ToHashMap()
var resultExtra = map[string]interface{}{
"next": nil,
"prev": nil,
}
result["extra"] = resultExtra
// check for admin rights to see particular article
if !api.IsAdminSession(context) && result["published"] == false {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "a3c29bd2-f4eb-4df2-bd75-722cb246def4", "no rights to see this post")
}
// Next / prev post
collection, err := db.GetCollection(ConstBlogPostCollectionName)
if err != nil {
return nil, env.ErrorDispatch(err)
}
// Next post
if err = collection.ClearFilters(); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.ClearSort(); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.AddFilter("updated_at", ">", postModel.GetUpdatedAt()); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.AddSort("updated_at", false); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.SetLimit(0, 1); err != nil {
return nil, env.ErrorDispatch(err)
}
if !api.IsAdminSession(context) {
if err = collection.AddFilter("published", "=", true); err != nil {
return nil, env.ErrorDispatch(err)
}
}
posts, err := collection.Load()
if err != nil {
return nil, env.ErrorDispatch(err)
}
if len(posts) > 0 {
resultExtra["next"] = posts[0]
}
// Prev post
if err = collection.ClearFilters(); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.ClearSort(); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.AddFilter("updated_at", "<", postModel.GetUpdatedAt()); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.AddSort("updated_at", true); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = collection.SetLimit(0, 1); err != nil {
return nil, env.ErrorDispatch(err)
}
if !api.IsAdminSession(context) {
if err = collection.AddFilter("published", "=", true); err != nil {
return nil, env.ErrorDispatch(err)
}
}
posts, err = collection.Load()
if err != nil {
return nil, env.ErrorDispatch(err)
}
if len(posts) > 0 {
resultExtra["prev"] = posts[0]
}
return result, nil
}
// APIListPostAttributes returns a list of blog post attributes
func APIListPostAttributes(context api.InterfaceApplicationContext) (interface{}, error) {
postModel, err := post.GetBlogPostModel()
if err != nil {
return nil, env.ErrorDispatch(err)
}
return postModel.GetAttributesInfo(), nil
}
// APICreateBlogPost creates blog post by provided data
func APICreateBlogPost(context api.InterfaceApplicationContext) (interface{}, error) {
requestData, err := api.GetRequestContentAsMap(context)
if err != nil {
return nil, env.ErrorDispatch(err)
}
blogPostModel, err := post.GetBlogPostModel()
if err != nil {
return nil, env.ErrorDispatch(err)
}
for attribute, value := range requestData {
if err := blogPostModel.Set(attribute, value); err != nil {
return nil, env.ErrorDispatch(err)
}
}
if err = blogPostModel.Save(); err != nil {
return nil, env.ErrorDispatch(err)
}
return blogPostModel.ToHashMap(), nil
}
// APIUpdateByID updates post by id
func APIUpdateByID(context api.InterfaceApplicationContext) (interface{}, error) {
blogPostID := context.GetRequestArgument("id")
if blogPostID == "" {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "f3ad6310-8b5b-428c-bfbd-107479d708f0", "post id was not specified")
}
requestData, err := api.GetRequestContentAsMap(context)
if err != nil {
return nil, env.ErrorDispatch(err)
}
blogPostModel, err := post.GetBlogPostModel()
if err != nil {
return nil, env.ErrorDispatch(err)
}
if err = blogPostModel.Load(blogPostID); err != nil {
return nil, env.ErrorDispatch(err)
}
for attribute, value := range requestData {
if err := blogPostModel.Set(attribute, value); err != nil {
return nil, env.ErrorDispatch(err)
}
}
if err = blogPostModel.Save(); err != nil {
return nil, env.ErrorDispatch(err)
}
return blogPostModel.ToHashMap(), nil
}
// APIDeleteByID deletes post by id
func APIDeleteByID(context api.InterfaceApplicationContext) (interface{}, error) {
// checking request context
//-------------------------
blogPostID := context.GetRequestArgument("id")
if blogPostID == "" {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "2bc9a20e-8d44-4381-97f1-13774c041838", "post id was not specified")
}
// operation
//-------------------------
blogPostModel, err := post.GetBlogPostModel()
if err != nil {
return nil, env.ErrorDispatch(err)
}
if err = blogPostModel.SetID(blogPostID); err != nil {
return nil, env.ErrorDispatch(err)
}
if err = blogPostModel.Delete(); err != nil {
return nil, env.ErrorDispatch(err)
}
return "Delete Successful", nil
}