-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
405 lines (368 loc) · 9.91 KB
/
Copy pathhandler.go
File metadata and controls
405 lines (368 loc) · 9.91 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package api
import (
"Activity/constant"
"Activity/models"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
type Handler struct {
gameService GameService
activityService ActivityService
}
func NewHandler(gameService GameService, activityService ActivityService) *Handler {
return &Handler{
gameService: gameService,
activityService: activityService,
}
}
// RegisterRoutes 注册所有API路由
func (h *Handler) RegisterRoutes(r *gin.Engine) {
// API版本分组
v1 := r.Group("")
{
// 活动相关接口
activity := v1.Group("/activity")
{
activity.POST("", h.CreateActivity)
activity.PUT("/:id", h.UpdateActivity)
activity.GET("/:id", h.GetActivity)
activity.POST("/:id/participate", h.Participate)
activity.GET("/:id/participation", h.GetParticipation)
}
// 游戏相关接口
game := v1.Group("/game")
{
game.POST("/participate", h.ParticipateGame)
game.GET("/status", h.GetGameStatus)
game.GET("/prize", h.GetUserPrize)
}
}
}
// @Summary 创建活动
// @Description 创建一个新的活动
// @Tags 活动管理
// @Accept json
// @Produce json
// @Param activity body CreateActivityRequest true "活动信息"
// @Success 200 {object} BaseResp{data=CreateActivityResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /activity [post]
func (h *Handler) CreateActivity(c *gin.Context) {
var req CreateActivityRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid params",
})
return
}
resp, err := h.activityService.CreateActivity(c, &req)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}
// @Summary 更新活动
// @Description 更新指定ID的活动信息
// @Tags 活动管理
// @Accept json
// @Produce json
// @Param id path string true "活动ID"
// @Param activity body UpdateActivityRequest true "活动信息"
// @Success 200 {object} BaseResp{data=UpdateActivityResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /activity/{id} [put]
func (h *Handler) UpdateActivity(c *gin.Context) {
var req UpdateActivityRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid params",
})
return
}
resp, err := h.activityService.UpdateActivity(c, &req)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}
// @Summary 获取活动信息
// @Description 获取指定ID的活动详细信息
// @Tags 活动管理
// @Accept json
// @Produce json
// @Param id path string true "活动ID"
// @Success 200 {object} BaseResp{data=GetActivityResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /activity/{id} [get]
func (h *Handler) GetActivity(c *gin.Context) {
activityID := c.Param("id")
if activityID == "" {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "activity_id is required",
})
return
}
// 将字符串ID转换为int64
id, err := strconv.ParseInt(activityID, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid activity_id",
})
return
}
resp, err := h.activityService.GetActivity(c, id)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}
// @Summary 参与活动
// @Description 用户参与指定活动
// @Tags 活动管理
// @Accept json
// @Produce json
// @Param id path string true "活动ID"
// @Param participation body ParticipateRequest true "参与信息"
// @Success 200 {object} BaseResp{data=ParticipateResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /activity/{id}/participate [post]
func (h *Handler) Participate(c *gin.Context) {
var req ParticipateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid params",
})
return
}
resp, err := h.activityService.Participate(c, &req)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}
// @Summary 获取参与记录
// @Description 获取用户在指定活动中的参与记录
// @Tags 活动管理
// @Accept json
// @Produce json
// @Param id path string true "活动ID"
// @Param user_id query string true "用户ID"
// @Success 200 {object} BaseResp{data=GetParticipationResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /activity/{id}/participation [get]
func (h *Handler) GetParticipation(c *gin.Context) {
activityID := c.Param("id")
if activityID == "" {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "activity_id is required",
})
return
}
// 将字符串ID转换为int64
id, err := strconv.ParseInt(activityID, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid activity_id",
})
return
}
userID := c.Query("user_id")
if userID == "" {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "user_id is required",
})
return
}
resp, err := h.activityService.GetParticipation(c, id, userID)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}
// @Summary 参与玩法
// @Description 用户参与指定玩法
// @Tags 玩法管理
// @Accept json
// @Produce json
// @Param participation body ParticipateGameReq true "参与信息"
// @Success 200 {object} BaseResp{data=ParticipateGameResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /game/participate [post]
func (h *Handler) ParticipateGame(c *gin.Context) {
var req ParticipateGameReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid params",
})
return
}
// 获取用户信息(实际项目中应该从中间件获取)
user := models.User{
Uid: c.GetString("uid"),
}
// 根据玩法类型创建对应的动作
var action models.ActionInterface
switch req.GameName {
case "post":
action = &models.CommunityPostAction{
// PostID: req.PostID,
}
case "checkin":
action = &models.CheckinAction{
CheckinTime: time.Now(),
}
default:
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "unsupported game type",
})
return
}
// 执行玩法逻辑
result, err := h.gameService.ParticipateGame(c, user, req.ActivityID, req.GameName, action)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: result,
})
}
// @Summary 获取玩法状态
// @Description 获取用户在指定玩法中的状态
// @Tags 玩法管理
// @Accept json
// @Produce json
// @Param activity_id query string true "活动ID"
// @Param game_name query string true "玩法名称"
// @Success 200 {object} BaseResp{data=GetGameStatusResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /game/status [get]
func (h *Handler) GetGameStatus(c *gin.Context) {
var req GetGameStatusReq
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid params",
})
return
}
// 获取用户信息(实际项目中应该从中间件获取)
user := models.User{
Uid: c.GetString("uid"),
}
resp, err := h.gameService.GetGameStatus(c, user, req.ActivityID, req.GameName)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}
// @Summary 获取用户奖品
// @Description 获取用户在指定玩法中获得的奖品
// @Tags 玩法管理
// @Accept json
// @Produce json
// @Param activity_id query string true "活动ID"
// @Param game_name query string true "玩法名称"
// @Success 200 {object} BaseResp{data=GetUserPrizeResponse}
// @Failure 400 {object} BaseResp
// @Failure 500 {object} BaseResp
// @Router /game/prize [get]
func (h *Handler) GetUserPrize(c *gin.Context) {
var req GetUserPrizeReq
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, BaseResp{
Code: constant.ErrInvalidParam,
Message: "invalid params",
})
return
}
// 获取用户信息(实际项目中应该从中间件获取)
user := models.User{
Uid: c.GetString("uid"),
}
resp, err := h.gameService.GetUserPrize(c, user, req.ActivityID, req.GameName)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResp{
Code: constant.ErrSystem,
Message: err.Error(),
})
return
}
c.JSON(http.StatusOK, BaseResp{
Code: 0,
Message: "success",
Data: resp,
})
}