refactor(votes): refactor user vote service param

This commit is contained in:
LinkinStars 2023-06-29 14:53:39 +08:00
parent 71bab09445
commit 9a31d7e76d
4 changed files with 19 additions and 31 deletions

View File

@ -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)
}

View File

@ -388,12 +388,8 @@ func (vr *VoteRepo) GetVoteResultByObjectId(ctx context.Context, objectID string
return 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()
}

View File

@ -28,7 +28,7 @@ type GetVoteWithPageReq struct {
// page size
PageSize int `validate:"omitempty,min=1" form:"page_size"`
// user id
UserID string `validate:"required" form:"user_id"`
UserID string `json:"-"`
}
type VoteQuestion struct {

View File

@ -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.GetVoteWith
activityTypeMapping[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.GetVoteWith
if 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
}