update tag revision

This commit is contained in:
aichy126 2022-11-24 15:48:04 +08:00
parent 977f9999a9
commit 7506806b67
5 changed files with 124 additions and 79 deletions

View File

@ -121,9 +121,10 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo) questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo)
tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo) tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo)
tagRelRepo := tag.NewTagRelRepo(dataData) tagRelRepo := tag.NewTagRelRepo(dataData)
tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo)
revisionRepo := revision.NewRevisionRepo(dataData, uniqueIDRepo) revisionRepo := revision.NewRevisionRepo(dataData, uniqueIDRepo)
revisionService := revision_common.NewRevisionService(revisionRepo, userRepo) 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) objService := object_info.NewObjService(answerRepo, questionRepo, commentCommonRepo, tagCommonRepo, tagCommonService)
voteRepo := activity_common.NewVoteRepo(dataData, activityRepo) voteRepo := activity_common.NewVoteRepo(dataData, activityRepo)
commentService := comment2.NewCommentService(commentRepo, commentCommonRepo, userCommon, objService, voteRepo) 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) serviceVoteRepo := activity.NewVoteRepo(dataData, uniqueIDRepo, configRepo, activityRepo, userRankRepo, voteRepo)
voteService := service.NewVoteService(serviceVoteRepo, uniqueIDRepo, configRepo, questionRepo, answerRepo, commentCommonRepo, objService) voteService := service.NewVoteService(serviceVoteRepo, uniqueIDRepo, configRepo, questionRepo, answerRepo, commentCommonRepo, objService)
voteController := controller.NewVoteController(voteService) voteController := controller.NewVoteController(voteService)
tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo)
followRepo := activity_common.NewFollowRepo(dataData, uniqueIDRepo, activityRepo) followRepo := activity_common.NewFollowRepo(dataData, uniqueIDRepo, activityRepo)
tagService := tag2.NewTagService(tagRepo, tagCommonService, revisionService, followRepo, siteInfoCommonService) tagService := tag2.NewTagService(tagRepo, tagCommonService, revisionService, followRepo, siteInfoCommonService)
tagController := controller.NewTagController(tagService, tagCommonService, rankService) tagController := controller.NewTagController(tagService, tagCommonService, rankService)

View File

@ -6,7 +6,7 @@ import (
"github.com/answerdev/answer/internal/base/data" "github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity" "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/answerdev/answer/internal/service/unique"
"github.com/segmentfault/pacman/errors" "github.com/segmentfault/pacman/errors"
"xorm.io/builder" "xorm.io/builder"
@ -22,7 +22,7 @@ type tagRepo struct {
func NewTagRepo( func NewTagRepo(
data *data.Data, data *data.Data,
uniqueIDRepo unique.UniqueIDRepo, uniqueIDRepo unique.UniqueIDRepo,
) tag.TagRepo { ) tag_common.TagRepo {
return &tagRepo{ return &tagRepo{
data: data, data: data,
uniqueIDRepo: uniqueIDRepo, uniqueIDRepo: uniqueIDRepo,

View File

@ -15,12 +15,14 @@ import (
"github.com/answerdev/answer/internal/service/object_info" "github.com/answerdev/answer/internal/service/object_info"
questioncommon "github.com/answerdev/answer/internal/service/question_common" questioncommon "github.com/answerdev/answer/internal/service/question_common"
"github.com/answerdev/answer/internal/service/revision" "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" tagcommon "github.com/answerdev/answer/internal/service/tag_common"
usercommon "github.com/answerdev/answer/internal/service/user_common" usercommon "github.com/answerdev/answer/internal/service/user_common"
"github.com/answerdev/answer/pkg/converter"
"github.com/answerdev/answer/pkg/obj" "github.com/answerdev/answer/pkg/obj"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors" "github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
) )
// RevisionService user service // RevisionService user service
@ -32,7 +34,7 @@ type RevisionService struct {
objectInfoService *object_info.ObjService objectInfoService *object_info.ObjService
questionRepo questioncommon.QuestionRepo questionRepo questioncommon.QuestionRepo
answerRepo answercommon.AnswerRepo answerRepo answercommon.AnswerRepo
tagRepo tag.TagRepo tagRepo tag_common.TagRepo
tagCommon *tagcommon.TagCommonService tagCommon *tagcommon.TagCommonService
} }
@ -44,7 +46,7 @@ func NewRevisionService(
objectInfoService *object_info.ObjService, objectInfoService *object_info.ObjService,
questionRepo questioncommon.QuestionRepo, questionRepo questioncommon.QuestionRepo,
answerRepo answercommon.AnswerRepo, answerRepo answercommon.AnswerRepo,
tagRepo tag.TagRepo, tagRepo tag_common.TagRepo,
tagCommon *tagcommon.TagCommonService, tagCommon *tagcommon.TagCommonService,
) *RevisionService { ) *RevisionService {
@ -168,14 +170,48 @@ func (rs *RevisionService) RevisionAudit(ctx context.Context, req *schema.Revisi
if ok { if ok {
tag := &entity.Tag{} tag := &entity.Tag{}
tag.ID = taginfo.TagID tag.ID = taginfo.TagID
tag.DisplayName = taginfo.DisplayName
tag.SlugName = taginfo.SlugName
tag.OriginalText = taginfo.OriginalText tag.OriginalText = taginfo.OriginalText
tag.ParsedText = taginfo.ParsedText tag.ParsedText = taginfo.ParsedText
saveerr := rs.tagRepo.UpdateTag(ctx, tag) saveerr := rs.tagRepo.UpdateTag(ctx, tag)
if saveerr != nil { if saveerr != nil {
return saveerr 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) err = rs.revisionRepo.UpdateStatus(ctx, req.ID, entity.RevisionReviewPassStatus)
return return
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/answerdev/answer/internal/service/activity_queue" "github.com/answerdev/answer/internal/service/activity_queue"
"github.com/answerdev/answer/internal/service/revision_common" "github.com/answerdev/answer/internal/service/revision_common"
"github.com/answerdev/answer/internal/service/siteinfo_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/pkg/htmltext"
"github.com/answerdev/answer/internal/base/pager" "github.com/answerdev/answer/internal/base/pager"
@ -23,17 +23,10 @@ import (
"github.com/segmentfault/pacman/log" "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 // TagService user service
type TagService struct { type TagService struct {
tagRepo TagRepo tagRepo tagcommonser.TagRepo
tagCommonService *tag_common.TagCommonService tagCommonService *tagcommonser.TagCommonService
revisionService *revision_common.RevisionService revisionService *revision_common.RevisionService
followCommon activity_common.FollowRepo followCommon activity_common.FollowRepo
siteInfoService *siteinfo_common.SiteInfoCommonService siteInfoService *siteinfo_common.SiteInfoCommonService
@ -41,8 +34,8 @@ type TagService struct {
// NewTagService new tag service // NewTagService new tag service
func NewTagService( func NewTagService(
tagRepo TagRepo, tagRepo tagcommonser.TagRepo,
tagCommonService *tag_common.TagCommonService, tagCommonService *tagcommonser.TagCommonService,
revisionService *revision_common.RevisionService, revisionService *revision_common.RevisionService,
followCommon activity_common.FollowRepo, followCommon activity_common.FollowRepo,
siteInfoService *siteinfo_common.SiteInfoCommonService) *TagService { siteInfoService *siteinfo_common.SiteInfoCommonService) *TagService {
@ -68,66 +61,7 @@ func (ts *TagService) RemoveTag(ctx context.Context, tagID string) (err error) {
// UpdateTag update tag // UpdateTag update tag
func (ts *TagService) UpdateTag(ctx context.Context, req *schema.UpdateTagReq) (err error) { func (ts *TagService) UpdateTag(ctx context.Context, req *schema.UpdateTagReq) (err error) {
_, existUnreviewed, err := ts.revisionService.ExistUnreviewedByObjectID(ctx, req.TagID) return ts.tagCommonService.UpdateTag(ctx, req)
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
} }
// GetTagInfo get tag one // GetTagInfo get tag one

View File

@ -15,6 +15,8 @@ import (
"github.com/answerdev/answer/internal/service/activity_queue" "github.com/answerdev/answer/internal/service/activity_queue"
"github.com/answerdev/answer/internal/service/revision_common" "github.com/answerdev/answer/internal/service/revision_common"
"github.com/answerdev/answer/internal/service/siteinfo_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/errors"
"github.com/segmentfault/pacman/log" "github.com/segmentfault/pacman/log"
) )
@ -33,6 +35,13 @@ type TagCommonRepo interface {
UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) 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 { type TagRelRepo interface {
AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error) AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error)
RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error)
@ -48,6 +57,7 @@ type TagCommonService struct {
revisionService *revision_common.RevisionService revisionService *revision_common.RevisionService
tagCommonRepo TagCommonRepo tagCommonRepo TagCommonRepo
tagRelRepo TagRelRepo tagRelRepo TagRelRepo
tagRepo TagRepo
siteInfoService *siteinfo_common.SiteInfoCommonService siteInfoService *siteinfo_common.SiteInfoCommonService
} }
@ -55,12 +65,14 @@ type TagCommonService struct {
func NewTagCommonService( func NewTagCommonService(
tagCommonRepo TagCommonRepo, tagCommonRepo TagCommonRepo,
tagRelRepo TagRelRepo, tagRelRepo TagRelRepo,
tagRepo TagRepo,
revisionService *revision_common.RevisionService, revisionService *revision_common.RevisionService,
siteInfoService *siteinfo_common.SiteInfoCommonService, siteInfoService *siteinfo_common.SiteInfoCommonService,
) *TagCommonService { ) *TagCommonService {
return &TagCommonService{ return &TagCommonService{
tagCommonRepo: tagCommonRepo, tagCommonRepo: tagCommonRepo,
tagRelRepo: tagRelRepo, tagRelRepo: tagRelRepo,
tagRepo: tagRepo,
revisionService: revisionService, revisionService: revisionService,
siteInfoService: siteInfoService, siteInfoService: siteInfoService,
} }
@ -584,3 +596,66 @@ func (ts *TagCommonService) CreateOrUpdateTagRelList(ctx context.Context, object
} }
return nil 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
}