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 tag count

aichy12683b4dfe4f9f3
diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go
index 17cff250..f15eb088 100644
--- a/internal/migrations/migrations.go
+++ b/internal/migrations/migrations.go
@@ -62,7 +62,7 @@ var migrations = []Migration{
NewMigration("update user pin hide features", updateRolePinAndHideFeatures, true),
NewMigration("update question post time", updateQuestionPostTime, true),
NewMigration("add login limitations", addLoginLimitations, true),
- NewMigration("update question answer count", updateQuestionCount, true),
+ NewMigration("update count", updateCount, true),
}
// GetCurrentDBVersion returns the current db version
diff --git a/internal/migrations/v13.go b/internal/migrations/v13.go
index 843f5dd0..d5aba487 100644
--- a/internal/migrations/v13.go
+++ b/internal/migrations/v13.go
@@ -4,10 +4,20 @@ import (
"fmt"
"github.com/answerdev/answer/internal/entity"
+ "github.com/davecgh/go-spew/spew"
"github.com/segmentfault/pacman/log"
"xorm.io/xorm"
)
+func updateCount(x *xorm.Engine) error {
+ spew.Dump("update count")
+ // updateQuestionCount(x)
+ updateTagCount(x)
+ // updateUserQuestionCount(x)
+ // updateUserAnswerCount(x)
+ return nil
+}
+
func updateQuestionCount(x *xorm.Engine) error {
//question answer count
answers := make([]entity.Answer, 0)
@@ -40,11 +50,85 @@ func updateQuestionCount(x *xorm.Engine) error {
}
}
- //tag question count
+ return nil
+}
- //user question count
+// updateTagCount update tag count
+func updateTagCount(x *xorm.Engine) error {
+ tagRelList := make([]entity.TagRel, 0)
+ err := x.Find(&tagRelList, &entity.TagRel{})
+ if err != nil {
+ return fmt.Errorf("get tag rel failed: %w", err)
+ }
+ questionIDs := make([]string, 0)
+ questionsAvailableMap := make(map[string]bool)
+ for _, item := range tagRelList {
+ questionIDs = append(questionIDs, item.ObjectID)
+ questionsAvailableMap[item.ObjectID] = false
+ }
+ questionList := make([]entity.Question, 0)
+ err = x.In("id", questionIDs).In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).Find(&questionList, &entity.Question{})
+ if err != nil {
+ return fmt.Errorf("get questions failed: %w", err)
+ }
+ for _, question := range questionList {
+ _, ok := questionsAvailableMap[question.ID]
+ if ok {
+ questionsAvailableMap[question.ID] = true
+ }
+ }
+ for id, ok := range questionsAvailableMap {
+ if !ok {
+ if _, err = x.Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusDeleted}, &entity.TagRel{ObjectID: id}); err != nil {
+ log.Errorf("update %+v config failed: %s", id, err)
+ }
+ }
+ }
+ //select tag count
+ newTagRelList := make([]entity.TagRel, 0)
+ err = x.Find(&newTagRelList, &entity.TagRel{Status: entity.TagRelStatusAvailable})
+ if err != nil {
+ return fmt.Errorf("get tag rel failed: %w", err)
+ }
+ tagCountMap := make(map[string]int)
+ for _, v := range newTagRelList {
+ _, ok := tagCountMap[v.TagID]
+ if !ok {
+ tagCountMap[v.TagID] = 1
+ } else {
+ tagCountMap[v.TagID]++
+ }
+ }
+ TagList := make([]entity.Tag, 0)
+ err = x.Find(&TagList, &entity.Tag{})
+ if err != nil {
+ return fmt.Errorf("get tag failed: %w", err)
+ }
+ for _, tag := range TagList {
+ _, ok := tagCountMap[tag.ID]
+ if ok {
+ tag.QuestionCount = tagCountMap[tag.ID]
+ if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
+ log.Errorf("update %+v tag failed: %s", tag.ID, err)
+ return fmt.Errorf("update tag failed: %w", err)
+ }
+ } else {
+ tag.QuestionCount = 0
+ if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
+ log.Errorf("update %+v tag failed: %s", tag.ID, err)
+ return fmt.Errorf("update tag failed: %w", err)
+ }
+ }
+ }
+ return nil
+}
- //user answer count
+// updateUserQuestionCount update user question count
+func updateUserQuestionCount(x *xorm.Engine) error {
+ return nil
+}
+// updateUserAnswerCount update user answer count
+func updateUserAnswerCount(x *xorm.Engine) error {
return nil
}