MCPcopy
hub / github.com/apache/answer / VoteUp

Method VoteUp

internal/controller/vote_controller.go:68–110  ·  view source on GitHub ↗

VoteUp godoc @Summary vote up @Description add vote @Tags Activity @Accept json @Produce json @Security ApiKeyAuth @Param data body schema.VoteReq true "vote" @Success 200 {object} handler.RespBody{data=schema.VoteResp} @Router /answer/api/v1/vote/up [post]

(ctx *gin.Context)

Source from the content-addressed store, hash-verified

66// @Success 200 {object} handler.RespBody{data=schema.VoteResp}
67// @Router /answer/api/v1/vote/up [post]
68func (vc *VoteController) VoteUp(ctx *gin.Context) {
69 req := &schema.VoteReq{}
70 if handler.BindAndCheck(ctx, req) {
71 return
72 }
73 req.ObjectID = uid.DeShortID(req.ObjectID)
74 req.UserID = middleware.GetLoginUserIDFromContext(ctx)
75
76 can, needRank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, true)
77 if err != nil {
78 handler.HandleResponse(ctx, err, nil)
79 return
80 }
81 if !can {
82 lang := handler.GetLangByCtx(ctx)
83 msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: needRank})
84 handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
85 return
86 }
87
88 isAdmin := middleware.GetUserIsAdminModerator(ctx)
89 if !isAdmin {
90 captchaPass := vc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionVote, req.UserID, req.CaptchaID, req.CaptchaCode)
91 if !captchaPass {
92 errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
93 ErrorField: "captcha_code",
94 ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
95 })
96 handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
97 return
98 }
99 }
100
101 if !isAdmin {
102 vc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionVote, req.UserID)
103 }
104 resp, err := vc.VoteService.VoteUp(ctx, req)
105 if err != nil {
106 handler.HandleResponse(ctx, err, schema.ErrTypeToast)
107 } else {
108 handler.HandleResponse(ctx, err, resp)
109 }
110}
111
112// VoteDown godoc
113// @Summary vote down

Callers

nothing calls this directly

Calls 11

BindAndCheckFunction · 0.92
DeShortIDFunction · 0.92
HandleResponseFunction · 0.92
GetLangByCtxFunction · 0.92
TrWithDataFunction · 0.92
GetUserIsAdminModeratorFunction · 0.92
TrFunction · 0.92
CheckVotePermissionMethod · 0.80
ActionRecordAddMethod · 0.80

Tested by

no test coverage detected