Skip to content

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.

update user question count

aichy126299b95b2f23f
diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.go
index da9d048f..4e44dad8 100644
--- a/internal/repo/question/question_repo.go
+++ b/internal/repo/question/question_repo.go
@@ -219,6 +219,24 @@ func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err
return
}
+func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {
+ questionList := make([]*entity.Question, 0)
+ count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).And("user_id = ?", userID).Count(&questionList)
+ if err != nil {
+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
+func (qr *questionRepo) GetQuestionCountByIDs(ctx context.Context, ids []string) (count int64, err error) {
+ questionList := make([]*entity.Question, 0)
+ count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).In("id = ?", ids).Count(&questionList)
+ if err != nil {
+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
func (qr *questionRepo) GetQuestionIDsPage(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error) {
questionIDList = make([]*schema.SiteMapQuestionInfo, 0)
rows := make([]*entity.Question, 0)
diff --git a/internal/repo/user/user_repo.go b/internal/repo/user/user_repo.go
index e5a33314..18bebbfa 100644
--- a/internal/repo/user/user_repo.go
+++ b/internal/repo/user/user_repo.go
@@ -55,6 +55,26 @@ func (ur *userRepo) IncreaseQuestionCount(ctx context.Context, userID string, am
return nil
}
+func (ur *userRepo) UpdateQuestionCount(ctx context.Context, userID string, count int64) (err error) {
+ user := &entity.User{}
+ user.QuestionCount = int(count)
+ _, err = ur.data.DB.Where("id = ?", userID).Cols("question_count").Update(user)
+ if err != nil {
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return nil
+}
+
+func (ur *userRepo) UpdateAnswerCount(ctx context.Context, userID string, count int) (err error) {
+ user := &entity.User{}
+ user.AnswerCount = count
+ _, err = ur.data.DB.Where("id = ?", userID).Cols("answer_count").Update(user)
+ if err != nil {
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return nil
+}
+
// UpdateLastLoginDate update last login date
func (ur *userRepo) UpdateLastLoginDate(ctx context.Context, userID string) (err error) {
user := &entity.User{LastLoginDate: time.Now()}
diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.go
index 842670f4..2ca0549a 100644
--- a/internal/service/question_common/question.go
+++ b/internal/service/question_common/question.go
@@ -46,6 +46,8 @@ type QuestionRepo interface {
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
AdminSearchList(ctx context.Context, search *schema.AdminQuestionSearch) ([]*entity.Question, int64, error)
GetQuestionCount(ctx context.Context) (count int64, err error)
+ GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error)
+ GetQuestionCountByIDs(ctx context.Context, ids []string) (count int64, err error)
GetQuestionIDsPage(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error)
}
@@ -88,6 +90,10 @@ func NewQuestionCommon(questionRepo QuestionRepo,
}
}
+func (qs *QuestionCommon) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {
+ return qs.questionRepo.GetUserQuestionCount(ctx, userID)
+}
+
func (qs *QuestionCommon) UpdatePv(ctx context.Context, questionID string) error {
return qs.questionRepo.UpdatePvCount(ctx, questionID)
}
@@ -431,14 +437,16 @@ func (qs *QuestionCommon) RemoveQuestion(ctx context.Context, req *schema.Remove
return err
}
- // user add question count
- err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, -1)
+ userQuestionCount, err := qs.GetUserQuestionCount(ctx, questionInfo.UserID)
if err != nil {
- log.Error("user UpdateQuestionCount error", err.Error())
+ log.Error("user GetUserQuestionCount error", err.Error())
+ } else {
+ err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, userQuestionCount)
+ if err != nil {
+ log.Error("user IncreaseQuestionCount error", err.Error())
+ }
}
- // todo rank remove
-
return nil
}
diff --git a/internal/service/question_service.go b/internal/service/question_service.go
index c66497be..52ec1e33 100644
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@ -304,9 +304,14 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
}
// user add question count
- err = qs.userCommon.UpdateQuestionCount(ctx, question.UserID, 1)
+ userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, question.UserID)
if err != nil {
- log.Error("user IncreaseQuestionCount error", err.Error())
+ log.Error("user GetUserQuestionCount error", err.Error())
+ } else {
+ err = qs.userCommon.UpdateQuestionCount(ctx, question.UserID, userQuestionCount)
+ if err != nil {
+ log.Error("user IncreaseQuestionCount error", err.Error())
+ }
}
activity_queue.AddActivity(&schema.ActivityMsg{
@@ -419,10 +424,14 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
return err
}
- // user add question count
- err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, -1)
+ userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, questionInfo.UserID)
if err != nil {
- log.Error("user IncreaseQuestionCount error", err.Error())
+ log.Error("user GetUserQuestionCount error", err.Error())
+ } else {
+ err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, userQuestionCount)
+ if err != nil {
+ log.Error("user IncreaseQuestionCount error", err.Error())
+ }
}
err = qs.answerActivityService.DeleteQuestion(ctx, questionInfo.ID, questionInfo.CreatedAt, questionInfo.VoteCount)
diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.go
index 9eb91d9d..97b31b70 100644
--- a/internal/service/user_common/user.go
+++ b/internal/service/user_common/user.go
@@ -18,6 +18,8 @@ type UserRepo interface {
AddUser(ctx context.Context, user *entity.User) (err error)
IncreaseAnswerCount(ctx context.Context, userID string, amount int) (err error)
IncreaseQuestionCount(ctx context.Context, userID string, amount int) (err error)
+ UpdateQuestionCount(ctx context.Context, userID string, count int64) (err error)
+ UpdateAnswerCount(ctx context.Context, userID string, count int) (err error)
UpdateLastLoginDate(ctx context.Context, userID string) (err error)
UpdateEmailStatus(ctx context.Context, userID string, emailStatus int) error
UpdateNoticeStatus(ctx context.Context, userID string, noticeStatus int) error
@@ -37,7 +39,9 @@ type UserCommon struct {
userRepo UserRepo
}
-func NewUserCommon(userRepo UserRepo) *UserCommon {
+func NewUserCommon(
+ userRepo UserRepo,
+) *UserCommon {
return &UserCommon{
userRepo: userRepo,
}
@@ -63,11 +67,11 @@ func (us *UserCommon) GetUserBasicInfoByUserName(ctx context.Context, username s
}
func (us *UserCommon) UpdateAnswerCount(ctx context.Context, userID string, num int) error {
- return us.userRepo.IncreaseAnswerCount(ctx, userID, num)
+ return us.userRepo.UpdateAnswerCount(ctx, userID, num)
}
-func (us *UserCommon) UpdateQuestionCount(ctx context.Context, userID string, num int) error {
- return us.userRepo.IncreaseQuestionCount(ctx, userID, num)
+func (us *UserCommon) UpdateQuestionCount(ctx context.Context, userID string, num int64) error {
+ return us.userRepo.UpdateQuestionCount(ctx, userID, num)
}
func (us *UserCommon) BatchUserBasicInfoByID(ctx context.Context, IDs []string) (map[string]*schema.UserBasicInfo, error) {