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 answer count
aichy12657153f8e7d9f
diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.goindex b5350cbe..76585a07 100644--- a/internal/repo/answer/answer_repo.go+++ b/internal/repo/answer/answer_repo.go@@ -196,6 +196,25 @@ func (ar *answerRepo) GetByID(ctx context.Context, id string) (*entity.Answer, breturn &resp, has, nil}+func (ar *answerRepo) GetCountByQuestionID(ctx context.Context, questionID string) (int64, error) {+ questionID = uid.DeShortID(questionID)+ rows := make([]*entity.Answer, 0)+ count, err := ar.data.DB.Context(ctx).Where("question_id =? and status = ?", questionID, entity.AnswerStatusAvailable).FindAndCount(&rows)+ if err != nil {+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return count, nil+}++func (ar *answerRepo) GetCountByUserID(ctx context.Context, userID string) (int64, error) {+ rows := make([]*entity.Answer, 0)+ count, err := ar.data.DB.Context(ctx).Where(" user_id = ? and status = ?", userID, entity.AnswerStatusAvailable).FindAndCount(&rows)+ if err != nil {+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return count, nil+}+func (ar *answerRepo) GetByUserIDQuestionID(ctx context.Context, userID string, questionID string) (*entity.Answer, bool, error) {questionID = uid.DeShortID(questionID)var resp entity.Answerdiff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.goindex 01dd98e4..2932e7ff 100644--- a/internal/repo/question/question_repo.go+++ b/internal/repo/question/question_repo.go@@ -88,7 +88,8 @@ func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (efunc (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string, num int) (err error) {questionID = uid.DeShortID(questionID)question := &entity.Question{}- _, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Incr("answer_count", num).Update(question)+ question.AnswerCount = num+ _, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Cols("answer_count").Update(question)if err != nil {return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}diff --git a/internal/service/answer_common/answer.go b/internal/service/answer_common/answer.goindex 357484f2..c23e6822 100644--- a/internal/service/answer_common/answer.go+++ b/internal/service/answer_common/answer.go@@ -17,6 +17,8 @@ type AnswerRepo interface {GetAnswerPage(ctx context.Context, page, pageSize int, answer *entity.Answer) (answerList []*entity.Answer, total int64, err error)UpdateAccepted(ctx context.Context, id string, questionID string) errorGetByID(ctx context.Context, id string) (*entity.Answer, bool, error)+ GetCountByQuestionID(ctx context.Context, questionID string) (int64, error)+ GetCountByUserID(ctx context.Context, userID string) (int64, error)GetByUserIDQuestionID(ctx context.Context, userID string, questionID string) (*entity.Answer, bool, error)SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)AdminSearchList(ctx context.Context, search *entity.AdminAnswerSearch) ([]*entity.Answer, int64, error)diff --git a/internal/service/answer_service.go b/internal/service/answer_service.goindex 92afbbca..27fc28f0 100644--- a/internal/service/answer_service.go+++ b/internal/service/answer_service.go@@ -112,20 +112,23 @@ func (as *AnswerService) RemoveAnswer(ctx context.Context, req *schema.RemoveAns}+ err = as.answerRepo.RemoveAnswer(ctx, req.ID)+ if err != nil {+ return err+ }+// user add question count- err = as.questionCommon.UpdateAnswerCount(ctx, answerInfo.QuestionID, -1)+ err = as.questionCommon.UpdateAnswerCount(ctx, answerInfo.QuestionID)if err != nil {log.Error("IncreaseAnswerCount error", err.Error())}-- err = as.userCommon.UpdateAnswerCount(ctx, answerInfo.UserID, -1)+ userAnswerCount, err := as.answerRepo.GetCountByUserID(ctx, answerInfo.UserID)if err != nil {- log.Error("user IncreaseAnswerCount error", err.Error())+ log.Error("GetCountByUserID error", err.Error())}-- err = as.answerRepo.RemoveAnswer(ctx, req.ID)+ err = as.userCommon.UpdateAnswerCount(ctx, answerInfo.UserID, int(userAnswerCount))if err != nil {- return err+ log.Error("user IncreaseAnswerCount error", err.Error())}// #2372 In order to simplify the process and complexity, as well as to consider if it is in-house,// facing the problem of recovery.@@ -167,7 +170,7 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (if err = as.answerRepo.AddAnswer(ctx, insertData); err != nil {return "", err}- err = as.questionCommon.UpdateAnswerCount(ctx, req.QuestionID, 1)+ err = as.questionCommon.UpdateAnswerCount(ctx, req.QuestionID)if err != nil {log.Error("IncreaseAnswerCount error", err.Error())}@@ -179,8 +182,11 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (if err != nil {return insertData.ID, err}-- err = as.userCommon.UpdateAnswerCount(ctx, req.UserID, 1)+ userAnswerCount, err := as.answerRepo.GetCountByUserID(ctx, req.UserID)+ if err != nil {+ log.Error("GetCountByUserID error", err.Error())+ }+ err = as.userCommon.UpdateAnswerCount(ctx, req.UserID, int(userAnswerCount))if err != nil {log.Error("user IncreaseAnswerCount error", err.Error())}diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.goindex 6ec1176f..8d96ab43 100644--- a/internal/service/question_common/question.go+++ b/internal/service/question_common/question.go@@ -99,8 +99,12 @@ func (qs *QuestionCommon) UpdatePv(ctx context.Context, questionID string) errorreturn qs.questionRepo.UpdatePvCount(ctx, questionID)}-func (qs *QuestionCommon) UpdateAnswerCount(ctx context.Context, questionID string, num int) error {- return qs.questionRepo.UpdateAnswerCount(ctx, questionID, num)+func (qs *QuestionCommon) UpdateAnswerCount(ctx context.Context, questionID string) error {+ count, err := qs.answerRepo.GetCountByQuestionID(ctx, questionID)+ if err != nil {+ return err+ }+ return qs.questionRepo.UpdateAnswerCount(ctx, questionID, int(count))}func (qs *QuestionCommon) UpdateCollectionCount(ctx context.Context, questionID string, num int) error {@@ -523,12 +527,15 @@ func (as *QuestionCommon) RemoveAnswer(ctx context.Context, id string) (err erro// user add question count- err = as.UpdateAnswerCount(ctx, answerinfo.QuestionID, -1)+ err = as.UpdateAnswerCount(ctx, answerinfo.QuestionID)if err != nil {log.Error("UpdateAnswerCount error", err.Error())}-- err = as.userCommon.UpdateAnswerCount(ctx, answerinfo.UserID, -1)+ userAnswerCount, err := as.answerRepo.GetCountByUserID(ctx, answerinfo.UserID)+ if err != nil {+ log.Error("GetCountByUserID error", err.Error())+ }+ err = as.userCommon.UpdateAnswerCount(ctx, answerinfo.UserID, int(userAnswerCount))if err != nil {log.Error("user UpdateAnswerCount error", err.Error())}