-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_api.go
256 lines (223 loc) · 5.36 KB
/
post_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
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/smtc/glog"
"io/ioutil"
//"net/http"
)
/*
# 文章:
获取默认文章列表:
GET /api/topics?start=xx&count=xx
*/
func getTopics(c *gin.Context) {
getTopicByPoint(c, "default")
}
/*
获取优质文章列表:
GET /api/topics/digest
*/
func getTopicsByDigest(c *gin.Context) {
getTopicByPoint(c, "digest")
}
/*
获取最新创建文章列表:
GET /api/topics/latest
*/
func getTopicsByLatest(c *gin.Context) {
getTopicByPoint(c, "latest")
}
/*
获取上升最快
GET /api/topics/rocket
*/
func getTopicsByRocket(c *gin.Context) {
getTopicByPoint(c, "rocket")
}
/*
获取争议
GET /api/topics/controversy
*/
func getTopicsByControversy(c *gin.Context) {
getTopicByPoint(c, "controversy")
}
func getTopicByPoint(c *gin.Context, point string) {
start := getQueryIntDefault(c, "start", 0)
count := getQueryIntDefault(c, "count", 20)
posts, ret, err := getPostsByPoint(point, start, count)
if ret != 0 {
glog.Error("getPostsByPoint failed: point=%s ret=%d err=%s\n", point, ret, err.Error())
c.JSON(200, RespResult{ret, "get post by " + point + " failed: " + err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", posts})
}
/*
GET /api/topic/{id}
*/
func getTopic(c *gin.Context) {
post, err := getPostById(c.Param("id"), true)
if err != nil {
c.JSON(200, RespResult{ErrCodeNotFound, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", post})
return
}
/*
创建文章:
POST /api/topic/{id}
1 post的body是一个json结构体,post从这个结构体unmarshal得到
2 post的类别信息也在这个结构体中,字段设置如下:
taxonomy string
name string
或者:
tax_id string
*/
func createNewTopic(c *gin.Context) {
if c.Request.Method != "POST" {
c.JSON(200, RespResult{ErrCodeInvalidMethod, "invalid method, should be post", nil})
return
}
user := getCurrent(c)
if user == nil {
c.JSON(200, RespResult{ErrCodeNeedLogin, "need login", nil})
return
}
// read request post body
defer c.Request.Body.Close()
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(200, RespResult{ErrCodeReadRequest, err.Error(), nil})
return
}
// read post from body
post, err := readPostFromRequest(body)
if err != nil {
return
}
id := c.Param("id")
if post.Id != id {
c.JSON(200, RespResult{ErrCodeParam, "post id not equal with path param id", nil})
return
}
if post.AuthorId != "" {
if user.Id != post.AuthorId {
c.JSON(200, RespResult{ErrCodeNeeAuthen, "you have no permission", nil})
return
}
} else {
post.AuthorId = user.Id
post.AuthorName = user.Name
}
tax, _ := readTaxFromRequest(body)
if err := createPost(post, user); err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
} else {
// 保存post与taxnomy的关系
err = setPostTaxonmoy(post, tax)
if err != nil {
glog.Error("set post-taxonomy failed: post id %s\n", post.Id)
}
}
c.JSON(200, RespResult{0, "ok", nil})
}
// 从post header中解出post和post的taxonomy
func readPostFromRequest(body []byte) (*Post, error) {
var post Post
err := json.Unmarshal(body, &post)
if err != nil {
return &post, err
}
return &post, nil
}
func readTaxFromRequest(body []byte) ([]*Taxonomy, error) {
var (
taxes []*Taxonomy
taxInfo CategoryInfo
)
if err := json.Unmarshal(body, &taxInfo); err != nil {
glog.Error("unmarshal categroy info failed: %s %s\n", err.Error(), string(body))
taxes = []*Taxonomy{&UnCategory}
} else {
//glog.Info("category info: %v %s\n", taxInfo, string(body))
taxes = getTaxFromInfos(taxInfo.Infoes)
}
return taxes, nil
}
/*
修改文章:
PUT /api/topic/{id}
*/
func modifyTopic(c *gin.Context) {
if c.Request.Method != "PUT" {
c.JSON(200, RespResult{ErrCodeInvalidMethod, "invalid method, should be put", nil})
return
}
post, err := getPostById(c.Param("id"), false)
if err != nil {
c.JSON(200, RespResult{ErrCodeNotFound, err.Error(), nil})
return
}
user := getCurrent(c)
if user == nil {
c.JSON(200, RespResult{ErrCodeNeedLogin, "need login", nil})
return
}
// Todo: 管理员帐号授权
if post.AuthorId != user.Id {
c.JSON(200, RespResult{ErrCodeNeeAuthen, "you have no permission", nil})
return
}
// read from request
defer c.Request.Body.Close()
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(200, RespResult{ErrCodeReadRequest, err.Error(), nil})
return
}
mpost, err := readPostFromRequest(body)
if err != nil {
return
}
post.modifyTitle(mpost.Title)
post.modifyContent(mpost.Content)
if err = post.flush(); err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", nil})
}
/*
删除文章
DELETE /api/topic/{id}
*/
func deleteTopic(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(200, RespResult{ErrCodeParam, "param id is empty", nil})
return
}
// todo: 鉴权
user := getCurrent(c)
if user == nil {
c.JSON(200, RespResult{ErrCodeNeePerm, "need permission", nil})
return
}
post, err := getPostById(id, true)
if err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
}
err = deletePost(post, user, true)
if err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, "delete post " + id + " failed: " + err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", nil})
}
/*
todo: 设置文章的类别,label等属性
*/