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

aichy126955a2a6fea96
diff --git a/internal/entity/tag_rel_entity.go b/internal/entity/tag_rel_entity.go
index 859f8bf7..5f2992a8 100644
--- a/internal/entity/tag_rel_entity.go
+++ b/internal/entity/tag_rel_entity.go
@@ -4,6 +4,7 @@ import "time"
const (
TagRelStatusAvailable = 1
+ TagRelStatusHide = 2
TagRelStatusDeleted = 10
)
diff --git a/internal/migrations/v13.go b/internal/migrations/v13.go
index d5aba487..147d5178 100644
--- a/internal/migrations/v13.go
+++ b/internal/migrations/v13.go
@@ -62,9 +62,11 @@ func updateTagCount(x *xorm.Engine) error {
}
questionIDs := make([]string, 0)
questionsAvailableMap := make(map[string]bool)
+ questionsHideMap := make(map[string]bool)
for _, item := range tagRelList {
questionIDs = append(questionIDs, item.ObjectID)
questionsAvailableMap[item.ObjectID] = false
+ questionsHideMap[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{})
@@ -75,8 +77,20 @@ func updateTagCount(x *xorm.Engine) error {
_, ok := questionsAvailableMap[question.ID]
if ok {
questionsAvailableMap[question.ID] = true
+ if question.Show == entity.QuestionHide {
+ questionsHideMap[question.ID] = true
+ }
}
}
+
+ for id, ok := range questionsHideMap {
+ if ok {
+ if _, err = x.Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusHide}, &entity.TagRel{ObjectID: id}); err != nil {
+ log.Errorf("update %+v config failed: %s", id, err)
+ }
+ }
+ }
+
for id, ok := range questionsAvailableMap {
if !ok {
if _, err = x.Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusDeleted}, &entity.TagRel{ObjectID: id}); err != nil {
@@ -84,6 +98,7 @@ func updateTagCount(x *xorm.Engine) error {
}
}
}
+
//select tag count
newTagRelList := make([]entity.TagRel, 0)
err = x.Find(&newTagRelList, &entity.TagRel{Status: entity.TagRelStatusAvailable})
diff --git a/internal/repo/tag/tag_rel_repo.go b/internal/repo/tag/tag_rel_repo.go
index 22d26621..38cf353b 100644
--- a/internal/repo/tag/tag_rel_repo.go
+++ b/internal/repo/tag/tag_rel_repo.go
@@ -9,6 +9,7 @@ import (
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
"github.com/answerdev/answer/internal/service/unique"
"github.com/answerdev/answer/pkg/uid"
+ "github.com/davecgh/go-spew/spew"
"github.com/segmentfault/pacman/errors"
)
@@ -52,6 +53,26 @@ func (tr *tagRelRepo) RemoveTagRelListByObjectID(ctx context.Context, objectID s
return
}
+func (tr *tagRelRepo) HideTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
+ spew.Dump("====== HideTagRelListByObjectID")
+ objectID = uid.DeShortID(objectID)
+ _, err = tr.data.DB.Where("object_id = ?", objectID).Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusHide})
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
+func (tr *tagRelRepo) ShowTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
+ spew.Dump("====== ShowTagRelListByObjectID")
+ objectID = uid.DeShortID(objectID)
+ _, err = tr.data.DB.Where("object_id = ?", objectID).Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusAvailable})
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
// RemoveTagRelListByIDs delete tag list
func (tr *tagRelRepo) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error) {
_, err = tr.data.DB.In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
@@ -90,7 +111,7 @@ func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string)
objectID = uid.DeShortID(objectID)
tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.Where("object_id = ?", objectID)
- session.Where("status = ?", entity.TagRelStatusAvailable)
+ session.In("status", []int{entity.TagRelStatusAvailable, entity.TagRelStatusHide})
err = session.Find(&tagListList)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
diff --git a/internal/service/question_service.go b/internal/service/question_service.go
index f9158b3c..a1377882 100644
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@ -349,8 +349,24 @@ func (qs *QuestionService) OperationQuestion(ctx context.Context, req *schema.Op
switch req.Operation {
case schema.QuestionOperationHide:
questionInfo.Show = entity.QuestionHide
+ err = qs.tagCommon.HideTagRelListByObjectID(ctx, req.ID)
+ if err != nil {
+ return err
+ }
+ err = qs.tagCommon.RefreshTagCountByQuestionID(ctx, req.ID)
+ if err != nil {
+ return err
+ }
case schema.QuestionOperationShow:
questionInfo.Show = entity.QuestionShow
+ err = qs.tagCommon.ShowTagRelListByObjectID(ctx, req.ID)
+ if err != nil {
+ return err
+ }
+ err = qs.tagCommon.RefreshTagCountByQuestionID(ctx, req.ID)
+ if err != nil {
+ return err
+ }
case schema.QuestionOperationPin:
questionInfo.Pin = entity.QuestionPin
case schema.QuestionOperationUnPin:
diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go
index 51ecbddb..41350b14 100644
--- a/internal/service/tag_common/tag_common.go
+++ b/internal/service/tag_common/tag_common.go
@@ -45,6 +45,8 @@ type TagRepo interface {
type TagRelRepo interface {
AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error)
RemoveTagRelListByObjectID(ctx context.Context, objectID string) (err error)
+ ShowTagRelListByObjectID(ctx context.Context, objectID string) (err error)
+ HideTagRelListByObjectID(ctx context.Context, objectID string) (err error)
RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error)
EnableTagRelByIDs(ctx context.Context, ids []int64) (err error)
GetObjectTagRelWithoutStatus(ctx context.Context, objectId, tagID string) (tagRel *entity.TagRel, exist bool, err error)
@@ -654,11 +656,35 @@ func (ts *TagCommonService) RefreshTagQuestionCount(ctx context.Context, tagIDs
return nil
}
+func (ts *TagCommonService) RefreshTagCountByQuestionID(ctx context.Context, questionID string) (err error) {
+ tagListList, err := ts.tagRelRepo.GetObjectTagRelList(ctx, questionID)
+ if err != nil {
+ return err
+ }
+ tagIDs := make([]string, 0)
+ for _, item := range tagListList {
+ tagIDs = append(tagIDs, item.TagID)
+ }
+ err = ts.RefreshTagQuestionCount(ctx, tagIDs)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
// RemoveTagRelListByObjectID remove tag relation by object id
func (ts *TagCommonService) RemoveTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
return ts.tagRelRepo.RemoveTagRelListByObjectID(ctx, objectID)
}
+func (ts *TagCommonService) HideTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
+ return ts.tagRelRepo.HideTagRelListByObjectID(ctx, objectID)
+}
+
+func (ts *TagCommonService) ShowTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
+ return ts.tagRelRepo.ShowTagRelListByObjectID(ctx, objectID)
+}
+
// CreateOrUpdateTagRelList if tag relation is exists update status, if not create it
func (ts *TagCommonService) CreateOrUpdateTagRelList(ctx context.Context, objectId string, tagIDs []string) (err error) {
addTagIDMapping := make(map[string]bool)