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.
feat(question): count question with show status
LinkinStarsb74f7813ac6d
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.goindex 3c17888f..a06d93eb 100644--- a/cmd/wire_gen.go+++ b/cmd/wire_gen.go@@ -134,14 +134,10 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,userNotificationConfigRepo := user_notification_config.NewUserNotificationConfigRepo(dataData)userNotificationConfigService := user_notification_config2.NewUserNotificationConfigService(userRepo, userNotificationConfigRepo)userExternalLoginService := user_external_login2.NewUserExternalLoginService(userRepo, userCommon, userExternalLoginRepo, emailService, siteInfoCommonService, userActiveActivityRepo, userNotificationConfigService)- userService := content.NewUserService(userRepo, userActiveActivityRepo, activityRepo, emailService, authService, siteInfoCommonService, userRoleRelService, userCommon, userExternalLoginService, userNotificationConfigRepo, userNotificationConfigService)- captchaRepo := captcha.NewCaptchaRepo(dataData)- captchaService := action.NewCaptchaService(captchaRepo)- userController := controller.NewUserController(authService, userService, captchaService, emailService, siteInfoCommonService, userNotificationConfigService)- commentRepo := comment.NewCommentRepo(dataData, uniqueIDRepo)- commentCommonRepo := comment.NewCommentCommonRepo(dataData, uniqueIDRepo)- answerRepo := answer.NewAnswerRepo(dataData, uniqueIDRepo, userRankRepo, activityRepo)questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo)+ answerRepo := answer.NewAnswerRepo(dataData, uniqueIDRepo, userRankRepo, activityRepo)+ voteRepo := activity_common.NewVoteRepo(dataData, activityRepo)+ followRepo := activity_common.NewFollowRepo(dataData, uniqueIDRepo, activityRepo)tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo)tagRelRepo := tag.NewTagRelRepo(dataData, uniqueIDRepo)tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo)@@ -149,8 +145,19 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,revisionService := revision_common.NewRevisionService(revisionRepo, userRepo)activityQueueService := activity_queue.NewActivityQueueService()tagCommonService := tag_common2.NewTagCommonService(tagCommonRepo, tagRelRepo, tagRepo, revisionService, siteInfoCommonService, activityQueueService)+ collectionRepo := collection.NewCollectionRepo(dataData, uniqueIDRepo)+ collectionCommon := collectioncommon.NewCollectionCommon(collectionRepo)+ answerCommon := answercommon.NewAnswerCommon(answerRepo)+ metaRepo := meta.NewMetaRepo(dataData)+ metaService := meta2.NewMetaService(metaRepo)+ questionCommon := questioncommon.NewQuestionCommon(questionRepo, answerRepo, voteRepo, followRepo, tagCommonService, userCommon, collectionCommon, answerCommon, metaService, configService, activityQueueService, revisionRepo, dataData)+ userService := content.NewUserService(userRepo, userActiveActivityRepo, activityRepo, emailService, authService, siteInfoCommonService, userRoleRelService, userCommon, userExternalLoginService, userNotificationConfigRepo, userNotificationConfigService, questionCommon)+ captchaRepo := captcha.NewCaptchaRepo(dataData)+ captchaService := action.NewCaptchaService(captchaRepo)+ userController := controller.NewUserController(authService, userService, captchaService, emailService, siteInfoCommonService, userNotificationConfigService)+ commentRepo := comment.NewCommentRepo(dataData, uniqueIDRepo)+ commentCommonRepo := comment.NewCommentCommonRepo(dataData, uniqueIDRepo)objService := object_info.NewObjService(answerRepo, questionRepo, commentCommonRepo, tagCommonRepo, tagCommonService)- voteRepo := activity_common.NewVoteRepo(dataData, activityRepo)notificationQueueService := notice_queue.NewNotificationQueueService()externalNotificationQueueService := notice_queue.NewNewQuestionNotificationQueueService()commentService := comment2.NewCommentService(commentRepo, commentCommonRepo, userCommon, objService, voteRepo, emailService, userRepo, notificationQueueService, externalNotificationQueueService, activityQueueService)@@ -161,13 +168,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,rateLimitMiddleware := middleware.NewRateLimitMiddleware(limitRepo)commentController := controller.NewCommentController(commentService, rankService, captchaService, rateLimitMiddleware)reportRepo := report.NewReportRepo(dataData, uniqueIDRepo)- followRepo := activity_common.NewFollowRepo(dataData, uniqueIDRepo, activityRepo)- collectionRepo := collection.NewCollectionRepo(dataData, uniqueIDRepo)- collectionCommon := collectioncommon.NewCollectionCommon(collectionRepo)- answerCommon := answercommon.NewAnswerCommon(answerRepo)- metaRepo := meta.NewMetaRepo(dataData)- metaService := meta2.NewMetaService(metaRepo)- questionCommon := questioncommon.NewQuestionCommon(questionRepo, answerRepo, voteRepo, followRepo, tagCommonService, userCommon, collectionCommon, answerCommon, metaService, configService, activityQueueService, revisionRepo, dataData)answerActivityRepo := activity.NewAnswerActivityRepo(dataData, activityRepo, userRankRepo, notificationQueueService)answerActivityService := activity2.NewAnswerActivityService(answerActivityRepo, configService)externalNotificationService := notification.NewExternalNotificationService(dataData, userNotificationConfigRepo, followRepo, emailService, userRepo, externalNotificationQueueService, userExternalLoginRepo, siteInfoCommonService)diff --git a/internal/controller/template_render/userinfo.go b/internal/controller/template_render/userinfo.goindex 731f6e8e..d2c222c1 100644--- a/internal/controller/template_render/userinfo.go+++ b/internal/controller/template_render/userinfo.go@@ -25,5 +25,5 @@ import ()func (q *TemplateRenderController) UserInfo(ctx context.Context, req *schema.GetOtherUserInfoByUsernameReq) (resp *schema.GetOtherUserInfoByUsernameResp, err error) {- return q.userService.GetOtherUserInfoByUsername(ctx, req.Username)+ return q.userService.GetOtherUserInfoByUsername(ctx, req)}diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.goindex b95e86bd..f0e70cfe 100644--- a/internal/controller/user_controller.go+++ b/internal/controller/user_controller.go@@ -115,7 +115,10 @@ func (uc *UserController) GetOtherUserInfoByUsername(ctx *gin.Context) {return}- resp, err := uc.userService.GetOtherUserInfoByUsername(ctx, req.Username)+ req.UserID = middleware.GetLoginUserIDFromContext(ctx)+ req.IsAdmin = middleware.GetUserIsAdminModerator(ctx)++ resp, err := uc.userService.GetOtherUserInfoByUsername(ctx, req)handler.HandleResponse(ctx, err, resp)}diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.goindex 920c0b08..29ab9c5d 100644--- a/internal/repo/question/question_repo.go+++ b/internal/repo/question/question_repo.go@@ -284,10 +284,10 @@ func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, errreturn count, nil}-func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {+func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string, show int) (count int64, err error) {session := qr.data.DB.Context(ctx)session.Where(builder.Lt{"status": entity.QuestionStatusDeleted})- count, err = session.Count(&entity.Question{UserID: userID})+ count, err = session.Count(&entity.Question{UserID: userID, Show: show})if err != nil {return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.goindex 53909066..7915e40c 100644--- a/internal/schema/user_schema.go+++ b/internal/schema/user_schema.go@@ -21,6 +21,7 @@ package schemaimport ("encoding/json"+"github.com/apache/incubator-answer/internal/base/reason""github.com/apache/incubator-answer/internal/base/translator""github.com/segmentfault/pacman/errors"@@ -355,6 +356,7 @@ type UserBasicInfo struct {type GetOtherUserInfoByUsernameReq struct {Username string `validate:"required,gt=0,lte=500" form:"username"`UserID string `json:"-"`+ IsAdmin bool `json:"-"`}type GetOtherUserInfoResp struct {diff --git a/internal/service/content/user_service.go b/internal/service/content/user_service.goindex 6c822c71..4c9843a3 100644--- a/internal/service/content/user_service.go+++ b/internal/service/content/user_service.go@@ -26,6 +26,7 @@ import ("time""github.com/apache/incubator-answer/internal/base/constant"+ questioncommon "github.com/apache/incubator-answer/internal/service/question_common""github.com/apache/incubator-answer/internal/service/user_notification_config""github.com/apache/incubator-answer/internal/base/handler"@@ -63,6 +64,7 @@ type UserService struct {userExternalLoginService *user_external_login.UserExternalLoginServiceuserNotificationConfigRepo user_notification_config.UserNotificationConfigRepouserNotificationConfigService *user_notification_config.UserNotificationConfigService+ questionService *questioncommon.QuestionCommon}func NewUserService(userRepo usercommon.UserRepo,@@ -76,6 +78,7 @@ func NewUserService(userRepo usercommon.UserRepo,userExternalLoginService *user_external_login.UserExternalLoginService,userNotificationConfigRepo user_notification_config.UserNotificationConfigRepo,userNotificationConfigService *user_notification_config.UserNotificationConfigService,+ questionService *questioncommon.QuestionCommon,) *UserService {return &UserService{userCommonService: userCommonService,@@ -89,6 +92,7 @@ func NewUserService(userRepo usercommon.UserRepo,userExternalLoginService: userExternalLoginService,userNotificationConfigRepo: userNotificationConfigRepo,userNotificationConfigService: userNotificationConfigService,+ questionService: questionService,}}@@ -118,9 +122,9 @@ func (us *UserService) GetUserInfoByUserID(ctx context.Context, token, userID streturn resp, nil}-func (us *UserService) GetOtherUserInfoByUsername(ctx context.Context, username string) (+func (us *UserService) GetOtherUserInfoByUsername(ctx context.Context, req *schema.GetOtherUserInfoByUsernameReq) (resp *schema.GetOtherUserInfoByUsernameResp, err error) {- userInfo, exist, err := us.userRepo.GetByUsername(ctx, username)+ userInfo, exist, err := us.userRepo.GetByUsername(ctx, req.Username)if err != nil {return nil, err}@@ -130,6 +134,13 @@ func (us *UserService) GetOtherUserInfoByUsername(ctx context.Context, usernameresp = &schema.GetOtherUserInfoByUsernameResp{}resp.ConvertFromUserEntity(userInfo)resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL()++ // Only the user himself and the administrator can see the hidden questions+ questionCount, err := us.questionService.GetPersonalUserQuestionCount(ctx, req.UserID, userInfo.ID, req.IsAdmin)+ if err != nil {+ return nil, err+ }+ resp.QuestionCount = int(questionCount)return resp, nil}diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.goindex 9f845c95..ff0b9da5 100644--- a/internal/service/question_common/question.go+++ b/internal/service/question_common/question.go@@ -70,7 +70,7 @@ type QuestionRepo interface {FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)AdminQuestionPage(ctx context.Context, search *schema.AdminQuestionPageReq) ([]*entity.Question, int64, error)GetQuestionCount(ctx context.Context) (count int64, err error)- GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error)+ GetUserQuestionCount(ctx context.Context, userID string, show int) (count int64, err error)SitemapQuestions(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error)RemoveAllUserQuestion(ctx context.Context, userID string) (err error)UpdateSearch(ctx context.Context, questionID string) (err error)@@ -125,7 +125,15 @@ func NewQuestionCommon(questionRepo QuestionRepo,}func (qs *QuestionCommon) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {- return qs.questionRepo.GetUserQuestionCount(ctx, userID)+ return qs.questionRepo.GetUserQuestionCount(ctx, userID, 0)+}++func (qs *QuestionCommon) GetPersonalUserQuestionCount(ctx context.Context, loginUserID, userID string, isAdmin bool) (count int64, err error) {+ show := entity.QuestionShow+ if loginUserID == userID || isAdmin {+ show = 0+ }+ return qs.questionRepo.GetUserQuestionCount(ctx, userID, show)}func (qs *QuestionCommon) UpdatePv(ctx context.Context, questionID string) error {diff --git a/internal/service/review/review_service.go b/internal/service/review/review_service.goindex 9992e59a..788de64c 100644--- a/internal/service/review/review_service.go+++ b/internal/service/review/review_service.go@@ -130,7 +130,7 @@ func (cs *ReviewService) getReviewContentAuthorInfo(ctx context.Context, userIDreturn}author.Rank = user.Rank- author.ApprovedQuestionAmount, _ = cs.questionRepo.GetUserQuestionCount(ctx, userID)+ author.ApprovedQuestionAmount, _ = cs.questionRepo.GetUserQuestionCount(ctx, userID, 0)author.ApprovedAnswerAmount, _ = cs.answerRepo.GetCountByUserID(ctx, userID)author.Role, _ = cs.userRoleService.GetUserRole(ctx, userID)return@@ -225,7 +225,7 @@ func (cs *ReviewService) updateObjectStatus(ctx context.Context, review *entity.cs.externalNotificationQueueService.Send(ctx,schema.CreateNewQuestionNotificationMsg(questionInfo.ID, questionInfo.Title, questionInfo.UserID, tags))}- userQuestionCount, err := cs.questionRepo.GetUserQuestionCount(ctx, questionInfo.UserID)+ userQuestionCount, err := cs.questionRepo.GetUserQuestionCount(ctx, questionInfo.UserID, 0)if err != nil {log.Errorf("get user question count failed, err: %v", err)} else {