-
Notifications
You must be signed in to change notification settings - Fork 3
/
post_api.go
328 lines (293 loc) · 8.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Copyright 2020 opensourceai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package v1
import (
"github.com/gin-gonic/gin"
"github.com/google/wire"
"github.com/jinzhu/gorm"
"github.com/opensourceai/go-api-service/api/v1/dto"
"github.com/opensourceai/go-api-service/internal/models"
"github.com/opensourceai/go-api-service/internal/service"
"github.com/opensourceai/go-api-service/middleware/jwt"
"github.com/opensourceai/go-api-service/pkg/app"
"github.com/opensourceai/go-api-service/pkg/e"
"github.com/opensourceai/go-api-service/pkg/page"
"github.com/unknwon/com"
"net/http"
"strconv"
)
type PostApi struct {
}
var ProviderPost = wire.NewSet(NewPostApi, service.ProviderPost)
var postService service.PostService
func NewPostApi(service2 service.PostService) (*PostApi, error) {
postService = service2
return &PostApi{}, nil
}
func NewPostRouter(router *gin.Engine) {
post := router.Group("/v1/posts")
// 无需认证
{
post.GET("/:id", getPost)
post.GET("/:id/comments", getPostComments)
}
// 需认证
post.Use(jwt.JWT())
{
post.POST("", addPost)
post.DELETE("", deletePost)
post.GET("", getPostList)
post.PUT("", updatePost)
post.PUT("/board", movePost)
}
}
// @Summary 移动主题帖到其他版块
// @Tags Post
// @Produce json
// @Param src_id query string true "源版块ID"
// @Param target_id query string true "目标版块ID"
// @Param post_ids body dto.Ids true "被移动主题帖IDs"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Security ApiKeyAuth
// @Router /v1/posts/board [put]
func movePost(context *gin.Context) {
appG := app.Gin{C: context}
// 获取查询参数
srcID, err := app.QueryWithInt(context, "src_id")
if err != nil {
appG.Fail(nil)
return
}
targetID, err := app.QueryWithInt(context, "target_id")
if err != nil {
appG.Fail(nil)
return
}
// 绑定参数
postIDs := &dto.Ids{}
httpCode, errCode := app.BindAndValid(context, postIDs)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
// 移动主题帖
err = postService.ServiceMovePosts(srcID, targetID, postIDs)
if err != nil {
appG.Fail(nil)
return
}
appG.Success(nil)
}
// @Summary 获取帖子的评论
// @Tags Post
// @Produce json
// @Param id path string true "ID"
// @Param page query page.Page false "page"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /v1/posts/{id}/comments [get]
func getPostComments(context *gin.Context) {
appG := app.Gin{C: context}
bindPage := page.BindPage(context)
id := context.Param("id")
if id == "" {
return
}
var idInt int // 主题ID
var err error // 错误
idInt, err = strconv.Atoi(id)
if err != nil {
return
}
comments, err := postService.ServiceGetPostComments(idInt, bindPage)
if err != nil {
appG.Fail(nil)
return
}
appG.Success(comments)
}
// @Summary 获取帖子信息
// @Tags Post
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /v1/posts/{id} [get]
func getPost(context *gin.Context) {
appG := app.Gin{C: context}
// 请求异常处理
defer app.Recover(&appG)
id := context.Param("id")
if id == "" {
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
//userInfo := app.GetUserInfo(context)
post, err := postService.ServiceGetPost(id)
if err != nil {
appG.Response(http.StatusNotFound, e.ERROR_POST_NOT_EXIST, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, post)
}
// @Summary 修改用户自身帖子
// @Tags Post
// @Produce json
// @Param post body models.Post true "post"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Security ApiKeyAuth
// @Router /v1/posts [put]
func updatePost(context *gin.Context) {
appG := app.Gin{C: context}
// 请求异常处理
//defer app.Recover(&appG)
post := models.Post{}
httpCode, errCode := app.BindAndValid(context, &post)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
userInfo := app.GetUserInfo(context)
if err := postService.ServiceUpdatePost(userInfo.UserId, &post); err == gorm.ErrRecordNotFound {
appG.Response(http.StatusNotFound, e.NOT_FOUND, nil)
return
} else if err != nil {
appG.Response(http.StatusBadRequest, e.ERROR, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, nil)
}
// @Summary 获取用户自身帖子
// @Tags Post
// @Produce json
// @Param page query string true "pageNum"
// @Param size query string true "pageSize"
// @Param orderBy query string false "orderBy"
// @Param sorter query string false "sorter"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Security ApiKeyAuth
// @Router /v1/posts [get]
func getPostList(context *gin.Context) {
appG := app.Gin{C: context}
// 请求异常处理
defer app.Recover(&appG)
var err error
// 第几页
pageNum := context.Query("page")
var pageNumInt int
if pageNum == "" {
panic("pageNum 不能为空.")
} else {
if pageNumInt, err = strconv.Atoi(pageNum); err != nil {
panic("pageNum 无法转换.")
}
}
// 每页数据个数
pageSize := context.Query("size")
var pageSizeInt int
if pageSize == "" {
panic("pageSize 不能为空.")
} else {
if pageSizeInt, err = strconv.Atoi(pageSize); err != nil {
panic("pageSize 无法转换.")
}
}
// 排序字段
sorter := context.Query("sorter")
if sorter == "" {
sorter = "asc"
}
// 排序方式
orderBy := context.Query("orderBy")
if orderBy == "" {
orderBy = "id"
}
pageObj := page.NewPage(pageNumInt, pageSizeInt, orderBy, sorter)
// 参数检验
httpCode, errCode := app.Valid(pageObj)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
info := app.GetUserInfo(context)
if post, err := postService.ServiceGetOwnPost(pageObj, com.ToStr(info.UserId)); err != nil {
appG.Response(http.StatusBadRequest, e.ERROR, post)
return
} else {
appG.Response(http.StatusOK, e.SUCCESS, post)
return
}
}
type postIds struct {
Ids []int `json:"ids"` // 帖子IDs
}
// @Summary 删除用户帖子
// @Tags Post
// @Produce json
// @Param postIds body postIds true "postIds"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Security ApiKeyAuth
// @Router /v1/posts [delete]
func deletePost(context *gin.Context) {
postIds := postIds{}
appG := app.Gin{C: context}
defer app.Recover(&appG)
httpCode, errCode := app.BindAndValid(context, &postIds)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
//token := context.GetHeader("Authorization")
//userId, exists := context.Get("userId")
userInfo := app.GetUserInfo(context)
if err := postService.ServiceDeletePost(com.ToStr(userInfo.UserId), postIds.Ids...); err != nil {
if err == gorm.ErrRecordNotFound {
appG.Response(http.StatusBadRequest, e.ERROR_POST_NOT_EXIST, nil)
return
}
appG.Response(http.StatusBadRequest, e.ERROR, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, nil)
}
// @Summary 新增帖子
// @Tags Post
// @Produce json
// @Param post body models.Post true "post"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Security ApiKeyAuth
// @Router /v1/posts [post]
func addPost(context *gin.Context) {
post := models.Post{}
appG := app.Gin{C: context}
defer app.Recover(&appG)
// 参数校验
httpCode, errCode := app.BindAndValid(context, &post)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
if err := postService.ServiceAddPost(&post); err != nil {
appG.Response(http.StatusBadRequest, e.ERROR, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, nil)
}