From 7506806b67e412c93e8a32b704b387b04dd7a7ac Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Thu, 24 Nov 2022 15:48:04 +0800 Subject: [PATCH] update tag revision --- cmd/answer/wire_gen.go | 4 +- internal/repo/tag/tag_repo.go | 4 +- internal/service/revision_service.go | 42 +++++++++++- internal/service/tag/tag_service.go | 78 ++--------------------- internal/service/tag_common/tag_common.go | 75 ++++++++++++++++++++++ 5 files changed, 124 insertions(+), 79 deletions(-) diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go index 54a54058..217edea1 100644 --- a/cmd/answer/wire_gen.go +++ b/cmd/answer/wire_gen.go @@ -121,9 +121,10 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo) tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo) tagRelRepo := tag.NewTagRelRepo(dataData) + tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo) revisionRepo := revision.NewRevisionRepo(dataData, uniqueIDRepo) revisionService := revision_common.NewRevisionService(revisionRepo, userRepo) - tagCommonService := tag_common2.NewTagCommonService(tagCommonRepo, tagRelRepo, revisionService, siteInfoCommonService) + tagCommonService := tag_common2.NewTagCommonService(tagCommonRepo, tagRelRepo, tagRepo, revisionService, siteInfoCommonService) objService := object_info.NewObjService(answerRepo, questionRepo, commentCommonRepo, tagCommonRepo, tagCommonService) voteRepo := activity_common.NewVoteRepo(dataData, activityRepo) commentService := comment2.NewCommentService(commentRepo, commentCommonRepo, userCommon, objService, voteRepo) @@ -135,7 +136,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, serviceVoteRepo := activity.NewVoteRepo(dataData, uniqueIDRepo, configRepo, activityRepo, userRankRepo, voteRepo) voteService := service.NewVoteService(serviceVoteRepo, uniqueIDRepo, configRepo, questionRepo, answerRepo, commentCommonRepo, objService) voteController := controller.NewVoteController(voteService) - tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo) followRepo := activity_common.NewFollowRepo(dataData, uniqueIDRepo, activityRepo) tagService := tag2.NewTagService(tagRepo, tagCommonService, revisionService, followRepo, siteInfoCommonService) tagController := controller.NewTagController(tagService, tagCommonService, rankService) diff --git a/internal/repo/tag/tag_repo.go b/internal/repo/tag/tag_repo.go index 4a9222e3..0c1169e0 100644 --- a/internal/repo/tag/tag_repo.go +++ b/internal/repo/tag/tag_repo.go @@ -6,7 +6,7 @@ import ( "github.com/answerdev/answer/internal/base/data" "github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/entity" - "github.com/answerdev/answer/internal/service/tag" + "github.com/answerdev/answer/internal/service/tag_common" "github.com/answerdev/answer/internal/service/unique" "github.com/segmentfault/pacman/errors" "xorm.io/builder" @@ -22,7 +22,7 @@ type tagRepo struct { func NewTagRepo( data *data.Data, uniqueIDRepo unique.UniqueIDRepo, -) tag.TagRepo { +) tag_common.TagRepo { return &tagRepo{ data: data, uniqueIDRepo: uniqueIDRepo, diff --git a/internal/service/revision_service.go b/internal/service/revision_service.go index 3d434651..9a797954 100644 --- a/internal/service/revision_service.go +++ b/internal/service/revision_service.go @@ -15,12 +15,14 @@ import ( "github.com/answerdev/answer/internal/service/object_info" questioncommon "github.com/answerdev/answer/internal/service/question_common" "github.com/answerdev/answer/internal/service/revision" - "github.com/answerdev/answer/internal/service/tag" + "github.com/answerdev/answer/internal/service/tag_common" tagcommon "github.com/answerdev/answer/internal/service/tag_common" usercommon "github.com/answerdev/answer/internal/service/user_common" + "github.com/answerdev/answer/pkg/converter" "github.com/answerdev/answer/pkg/obj" "github.com/jinzhu/copier" "github.com/segmentfault/pacman/errors" + "github.com/segmentfault/pacman/log" ) // RevisionService user service @@ -32,7 +34,7 @@ type RevisionService struct { objectInfoService *object_info.ObjService questionRepo questioncommon.QuestionRepo answerRepo answercommon.AnswerRepo - tagRepo tag.TagRepo + tagRepo tag_common.TagRepo tagCommon *tagcommon.TagCommonService } @@ -44,7 +46,7 @@ func NewRevisionService( objectInfoService *object_info.ObjService, questionRepo questioncommon.QuestionRepo, answerRepo answercommon.AnswerRepo, - tagRepo tag.TagRepo, + tagRepo tag_common.TagRepo, tagCommon *tagcommon.TagCommonService, ) *RevisionService { @@ -168,14 +170,48 @@ func (rs *RevisionService) RevisionAudit(ctx context.Context, req *schema.Revisi if ok { tag := &entity.Tag{} tag.ID = taginfo.TagID + tag.DisplayName = taginfo.DisplayName + tag.SlugName = taginfo.SlugName tag.OriginalText = taginfo.OriginalText tag.ParsedText = taginfo.ParsedText saveerr := rs.tagRepo.UpdateTag(ctx, tag) if saveerr != nil { return saveerr } + + tagInfo, exist, err := rs.tagCommon.GetTagByID(ctx, taginfo.TagID) + if err != nil { + return err + } + if !exist { + return errors.BadRequest(reason.TagNotFound) + } + if tagInfo.MainTagID == 0 && len(tagInfo.SlugName) > 0 { + log.Debugf("tag %s update slug_name", tagInfo.SlugName) + tagList, err := rs.tagRepo.GetTagList(ctx, &entity.Tag{MainTagID: converter.StringToInt64(tagInfo.ID)}) + if err != nil { + return err + } + updateTagSlugNames := make([]string, 0) + for _, tag := range tagList { + updateTagSlugNames = append(updateTagSlugNames, tag.SlugName) + } + err = rs.tagRepo.UpdateTagSynonym(ctx, updateTagSlugNames, converter.StringToInt64(tagInfo.ID), tagInfo.MainTagSlugName) + if err != nil { + return err + } + } + + activity_queue.AddActivity(&schema.ActivityMsg{ + UserID: revisioninfo.UserID, + ObjectID: taginfo.TagID, + OriginalObjectID: taginfo.TagID, + ActivityTypeKey: constant.ActTagEdited, + RevisionID: revisioninfo.ID, + }) } } + err = rs.revisionRepo.UpdateStatus(ctx, req.ID, entity.RevisionReviewPassStatus) return } diff --git a/internal/service/tag/tag_service.go b/internal/service/tag/tag_service.go index 223342e2..057e1de8 100644 --- a/internal/service/tag/tag_service.go +++ b/internal/service/tag/tag_service.go @@ -8,7 +8,7 @@ import ( "github.com/answerdev/answer/internal/service/activity_queue" "github.com/answerdev/answer/internal/service/revision_common" "github.com/answerdev/answer/internal/service/siteinfo_common" - "github.com/answerdev/answer/internal/service/tag_common" + tagcommonser "github.com/answerdev/answer/internal/service/tag_common" "github.com/answerdev/answer/pkg/htmltext" "github.com/answerdev/answer/internal/base/pager" @@ -23,17 +23,10 @@ import ( "github.com/segmentfault/pacman/log" ) -type TagRepo interface { - RemoveTag(ctx context.Context, tagID string) (err error) - UpdateTag(ctx context.Context, tag *entity.Tag) (err error) - UpdateTagSynonym(ctx context.Context, tagSlugNameList []string, mainTagID int64, mainTagSlugName string) (err error) - GetTagList(ctx context.Context, tag *entity.Tag) (tagList []*entity.Tag, err error) -} - // TagService user service type TagService struct { - tagRepo TagRepo - tagCommonService *tag_common.TagCommonService + tagRepo tagcommonser.TagRepo + tagCommonService *tagcommonser.TagCommonService revisionService *revision_common.RevisionService followCommon activity_common.FollowRepo siteInfoService *siteinfo_common.SiteInfoCommonService @@ -41,8 +34,8 @@ type TagService struct { // NewTagService new tag service func NewTagService( - tagRepo TagRepo, - tagCommonService *tag_common.TagCommonService, + tagRepo tagcommonser.TagRepo, + tagCommonService *tagcommonser.TagCommonService, revisionService *revision_common.RevisionService, followCommon activity_common.FollowRepo, siteInfoService *siteinfo_common.SiteInfoCommonService) *TagService { @@ -68,66 +61,7 @@ func (ts *TagService) RemoveTag(ctx context.Context, tagID string) (err error) { // UpdateTag update tag func (ts *TagService) UpdateTag(ctx context.Context, req *schema.UpdateTagReq) (err error) { - _, existUnreviewed, err := ts.revisionService.ExistUnreviewedByObjectID(ctx, req.TagID) - if err != nil { - return err - } - if existUnreviewed { - err = errors.BadRequest(reason.AnswerCannotUpdate) - return err - } - - tag := &entity.Tag{} - _ = copier.Copy(tag, req) - tag.ID = req.TagID - err = ts.tagRepo.UpdateTag(ctx, tag) - if err != nil { - return err - } - - tagInfo, exist, err := ts.tagCommonService.GetTagByID(ctx, req.TagID) - if err != nil { - return err - } - if !exist { - return errors.BadRequest(reason.TagNotFound) - } - if tagInfo.MainTagID == 0 && len(req.SlugName) > 0 { - log.Debugf("tag %s update slug_name", tagInfo.SlugName) - tagList, err := ts.tagRepo.GetTagList(ctx, &entity.Tag{MainTagID: converter.StringToInt64(tagInfo.ID)}) - if err != nil { - return err - } - updateTagSlugNames := make([]string, 0) - for _, tag := range tagList { - updateTagSlugNames = append(updateTagSlugNames, tag.SlugName) - } - err = ts.tagRepo.UpdateTagSynonym(ctx, updateTagSlugNames, converter.StringToInt64(tagInfo.ID), tagInfo.MainTagSlugName) - if err != nil { - return err - } - } - - revisionDTO := &schema.AddRevisionDTO{ - UserID: req.UserID, - ObjectID: tag.ID, - Title: tag.SlugName, - Log: req.EditSummary, - } - tagInfoJson, _ := json.Marshal(tagInfo) - revisionDTO.Content = string(tagInfoJson) - revisionID, err := ts.revisionService.AddRevision(ctx, revisionDTO, true) - if err != nil { - return err - } - activity_queue.AddActivity(&schema.ActivityMsg{ - UserID: req.UserID, - ObjectID: tag.ID, - OriginalObjectID: tag.ID, - ActivityTypeKey: constant.ActTagEdited, - RevisionID: revisionID, - }) - return + return ts.tagCommonService.UpdateTag(ctx, req) } // GetTagInfo get tag one diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go index 6252623f..e72db785 100644 --- a/internal/service/tag_common/tag_common.go +++ b/internal/service/tag_common/tag_common.go @@ -15,6 +15,8 @@ import ( "github.com/answerdev/answer/internal/service/activity_queue" "github.com/answerdev/answer/internal/service/revision_common" "github.com/answerdev/answer/internal/service/siteinfo_common" + "github.com/answerdev/answer/pkg/converter" + "github.com/jinzhu/copier" "github.com/segmentfault/pacman/errors" "github.com/segmentfault/pacman/log" ) @@ -33,6 +35,13 @@ type TagCommonRepo interface { UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) } +type TagRepo interface { + RemoveTag(ctx context.Context, tagID string) (err error) + UpdateTag(ctx context.Context, tag *entity.Tag) (err error) + UpdateTagSynonym(ctx context.Context, tagSlugNameList []string, mainTagID int64, mainTagSlugName string) (err error) + GetTagList(ctx context.Context, tag *entity.Tag) (tagList []*entity.Tag, err error) +} + type TagRelRepo interface { AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error) @@ -48,6 +57,7 @@ type TagCommonService struct { revisionService *revision_common.RevisionService tagCommonRepo TagCommonRepo tagRelRepo TagRelRepo + tagRepo TagRepo siteInfoService *siteinfo_common.SiteInfoCommonService } @@ -55,12 +65,14 @@ type TagCommonService struct { func NewTagCommonService( tagCommonRepo TagCommonRepo, tagRelRepo TagRelRepo, + tagRepo TagRepo, revisionService *revision_common.RevisionService, siteInfoService *siteinfo_common.SiteInfoCommonService, ) *TagCommonService { return &TagCommonService{ tagCommonRepo: tagCommonRepo, tagRelRepo: tagRelRepo, + tagRepo: tagRepo, revisionService: revisionService, siteInfoService: siteInfoService, } @@ -584,3 +596,66 @@ func (ts *TagCommonService) CreateOrUpdateTagRelList(ctx context.Context, object } return nil } + +func (ts *TagCommonService) UpdateTag(ctx context.Context, req *schema.UpdateTagReq) (err error) { + _, existUnreviewed, err := ts.revisionService.ExistUnreviewedByObjectID(ctx, req.TagID) + if err != nil { + return err + } + if existUnreviewed { + err = errors.BadRequest(reason.AnswerCannotUpdate) + return err + } + + tag := &entity.Tag{} + _ = copier.Copy(tag, req) + tag.ID = req.TagID + err = ts.tagRepo.UpdateTag(ctx, tag) + if err != nil { + return err + } + + tagInfo, exist, err := ts.GetTagByID(ctx, req.TagID) + if err != nil { + return err + } + if !exist { + return errors.BadRequest(reason.TagNotFound) + } + if tagInfo.MainTagID == 0 && len(req.SlugName) > 0 { + log.Debugf("tag %s update slug_name", tagInfo.SlugName) + tagList, err := ts.tagRepo.GetTagList(ctx, &entity.Tag{MainTagID: converter.StringToInt64(tagInfo.ID)}) + if err != nil { + return err + } + updateTagSlugNames := make([]string, 0) + for _, tag := range tagList { + updateTagSlugNames = append(updateTagSlugNames, tag.SlugName) + } + err = ts.tagRepo.UpdateTagSynonym(ctx, updateTagSlugNames, converter.StringToInt64(tagInfo.ID), tagInfo.MainTagSlugName) + if err != nil { + return err + } + } + revisionDTO := &schema.AddRevisionDTO{ + UserID: req.UserID, + ObjectID: tag.ID, + Title: tag.SlugName, + Log: req.EditSummary, + } + tagInfoJson, _ := json.Marshal(tagInfo) + revisionDTO.Content = string(tagInfoJson) + revisionID, err := ts.revisionService.AddRevision(ctx, revisionDTO, true) + if err != nil { + return err + } + + activity_queue.AddActivity(&schema.ActivityMsg{ + UserID: req.UserID, + ObjectID: tag.ID, + OriginalObjectID: tag.ID, + ActivityTypeKey: constant.ActTagEdited, + RevisionID: revisionID, + }) + return +}