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.
fix(tag): recover delete tag when tag recreate
LinkinStars133907b59e07
diff --git a/internal/repo/tag_common/tag_common_repo.go b/internal/repo/tag_common/tag_common_repo.goindex 9218ba47..3160d428 100644--- a/internal/repo/tag_common/tag_common_repo.go+++ b/internal/repo/tag_common/tag_common_repo.go@@ -117,10 +117,9 @@ func (tr *tagCommonRepo) GetReservedTagList(ctx context.Context) (tagList []*ent// GetTagListByNames get tag list all like namefunc (tr *tagCommonRepo) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) {-tagList = make([]*entity.Tag, 0)session := tr.data.DB.Context(ctx).In("slug_name", names).UseBool("recommend", "reserved")- // session.Where(builder.Eq{"status": entity.TagStatusAvailable})+ session.Where(builder.Eq{"status": entity.TagStatusAvailable})err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList)if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()@@ -176,20 +175,50 @@ func (tr *tagCommonRepo) GetTagPage(ctx context.Context, page, pageSize int, tag// AddTagList add tagfunc (tr *tagCommonRepo) AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) {+ addTags := make([]*entity.Tag, 0)for _, item := range tagList {+ exist, err := tr.updateDeletedTag(ctx, item)+ if err != nil {+ return err+ }+ if exist {+ continue+ }+ addTags = append(addTags, item)item.ID, err = tr.uniqueIDRepo.GenUniqueIDStr(ctx, item.TableName())if err != nil {return err}item.RevisionID = "0"}- _, err = tr.data.DB.Context(ctx).Insert(tagList)+ if len(addTags) == 0 {+ return nil+ }+ _, err = tr.data.DB.Context(ctx).Insert(addTags)if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}return}+func (tr *tagCommonRepo) updateDeletedTag(ctx context.Context, tag *entity.Tag) (exist bool, err error) {+ old := &entity.Tag{SlugName: tag.SlugName}+ exist, err = tr.data.DB.Context(ctx).Get(old)+ if err != nil {+ return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ if !exist || old.Status != entity.TagStatusDeleted {+ return false, nil+ }+ tag.ID = old.ID+ tag.Status = entity.TagStatusAvailable+ tag.RevisionID = "0"+ if _, err = tr.data.DB.Context(ctx).ID(tag.ID).Update(tag); err != nil {+ return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return true, nil+}+// UpdateTagQuestionCount update tag question countfunc (tr *tagCommonRepo) UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) {cond := &entity.Tag{QuestionCount: questionCount}diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.goindex 52fd7f96..122a534b 100644--- a/internal/schema/tag_schema.go+++ b/internal/schema/tag_schema.go@@ -208,9 +208,6 @@ type UpdateTagReq struct {}func (r *UpdateTagReq) Check() (errFields []*validator.FormErrorField, err error) {- if len(r.EditSummary) == 0 {- r.EditSummary = "tag.edit.summary"- }r.ParsedText = converter.Markdown2HTML(r.OriginalText)return nil, nil}