A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
refactor(votes): refactor user vote service param
LinkinStars9a31d7e76db8
diff --git a/internal/controller/vote_controller.go b/internal/controller/vote_controller.goindex 6af80e35..00162c02 100644--- a/internal/controller/vote_controller.go+++ b/internal/controller/vote_controller.go@@ -103,9 +103,9 @@ func (vc *VoteController) VoteDown(ctx *gin.Context) {}}-// UserVotes godoc-// @Summary user's votes-// @Description user's vote+// UserVotes user votes+// @Summary get user personal votes+// @Description get user personal votes// @Tags Activity// @Accept json// @Produce json@@ -116,21 +116,12 @@ func (vc *VoteController) VoteDown(ctx *gin.Context) {// @Router /answer/api/v1/personal/vote/page [get]func (vc *VoteController) UserVotes(ctx *gin.Context) {req := schema.GetVoteWithPageReq{}- req.UserID = middleware.GetLoginUserIDFromContext(ctx)if handler.BindAndCheck(ctx, &req) {return}- if req.Page == 0 {- req.Page = 1- }- if req.PageSize == 0 {- req.PageSize = 30- }++ req.UserID = middleware.GetLoginUserIDFromContext(ctx)resp, err := vc.VoteService.ListUserVotes(ctx, req)- if err != nil {- handler.HandleResponse(ctx, err, schema.ErrTypeModal)- } else {- handler.HandleResponse(ctx, err, resp)- }+ handler.HandleResponse(ctx, err, resp)}diff --git a/internal/repo/activity/vote_repo.go b/internal/repo/activity/vote_repo.goindex e2a28e9c..22355235 100644--- a/internal/repo/activity/vote_repo.go+++ b/internal/repo/activity/vote_repo.go@@ -388,12 +388,8 @@ func (vr *VoteRepo) GetVoteResultByObjectId(ctx context.Context, objectID stringreturn resp, nil}-func (vr *VoteRepo) ListUserVotes(- ctx context.Context,- userID string,- req schema.GetVoteWithPageReq,- activityTypes []int,-) (voteList []entity.Activity, total int64, err error) {+func (vr *VoteRepo) ListUserVotes(ctx context.Context, userID string,+ page int, pageSize int, activityTypes []int) (voteList []entity.Activity, total int64, err error) {session := vr.data.DB.Context(ctx)cond := builder.And(@@ -402,9 +398,9 @@ func (vr *VoteRepo) ListUserVotes(builder.In("activity_type", activityTypes),)- session.Where(cond).OrderBy("updated_at desc")+ session.Where(cond).Desc("updated_at")- total, err = pager.Help(req.Page, req.PageSize, &voteList, &entity.Activity{}, session)+ total, err = pager.Help(page, pageSize, &voteList, &entity.Activity{}, session)if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}diff --git a/internal/schema/vote_schema.go b/internal/schema/vote_schema.goindex 4cb86c66..93775c29 100644--- a/internal/schema/vote_schema.go+++ b/internal/schema/vote_schema.go@@ -28,7 +28,7 @@ type GetVoteWithPageReq struct {// page sizePageSize int `validate:"omitempty,min=1" form:"page_size"`// user id- UserID string `validate:"required" form:"user_id"`+ UserID string `json:"-"`}type VoteQuestion struct {diff --git a/internal/service/vote_service.go b/internal/service/vote_service.goindex 022f9dca..1d39ff99 100644--- a/internal/service/vote_service.go+++ b/internal/service/vote_service.go@@ -31,7 +31,8 @@ type VoteRepo interface {VoteUpCancel(ctx context.Context, objectID string, userID, objectUserID string) (resp *schema.VoteResp, err error)VoteDownCancel(ctx context.Context, objectID string, userID, objectUserID string) (resp *schema.VoteResp, err error)GetVoteResultByObjectId(ctx context.Context, objectID string) (resp *schema.VoteResp, err error)- ListUserVotes(ctx context.Context, userID string, req schema.GetVoteWithPageReq, activityTypes []int) (voteList []entity.Activity, total int64, err error)+ ListUserVotes(ctx context.Context, userID string, page int, pageSize int, activityTypes []int) (+ voteList []entity.Activity, total int64, err error)}// VoteService user service@@ -153,7 +154,7 @@ func (vs *VoteService) GetObjectUserID(ctx context.Context, objectID string) (us}// ListUserVotes list user's votes-func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithPageReq) (model *pager.PageModel, err error) {+func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithPageReq) (resp *pager.PageModel, err error) {typeKeys := []string{activity_type.QuestionVoteUp,activity_type.QuestionVoteDown,@@ -172,14 +173,14 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithactivityTypeMapping[cfg.ID] = typeKey}- voteList, total, err := vs.voteRepo.ListUserVotes(ctx, req.UserID, req, activityTypes)+ voteList, total, err := vs.voteRepo.ListUserVotes(ctx, req.UserID, req.Page, req.PageSize, activityTypes)if err != nil {- return+ return nil, err}lang := handler.GetLangByCtx(ctx)- resp := make([]*schema.GetVoteWithPageResp, 0)+ votes := make([]*schema.GetVoteWithPageResp, 0)for _, voteInfo := range voteList {objInfo, err := vs.objectService.GetInfo(ctx, voteInfo.ObjectID)if err != nil {@@ -202,7 +203,7 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithif objInfo.QuestionStatus == entity.QuestionStatusDeleted {item.Title = translator.Tr(lang, constant.DeletedQuestionTitleTrKey)}- resp = append(resp, item)+ votes = append(votes, item)}- return pager.NewPageModel(total, resp), err+ return pager.NewPageModel(total, votes), err}