From 6b983d472d404cd74eadcb2a4b3c8eeeff5a657b Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Tue, 22 Nov 2022 11:37:37 +0800 Subject: [PATCH] fix tag update error --- i18n/en_US.yaml | 2 + internal/base/reason/reason.go | 65 ++++++++++++----------- internal/service/question_service.go | 18 +++++-- internal/service/tag_common/tag_common.go | 2 +- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 26bef9a4..06e450a1 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -76,6 +76,8 @@ backend: other: "Tag not found." recommend_tag_not_found: other: "Recommend Tag is not exist." + recommend_tag_enter: + other: "Please enter at least one required tag." not_contain_synonym_tags: other: "Should not contain synonym tags." theme: diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go index 23e7bae2..c5bcd9a6 100644 --- a/internal/base/reason/reason.go +++ b/internal/base/reason/reason.go @@ -14,38 +14,39 @@ const ( ) const ( - EmailOrPasswordWrong = "error.object.email_or_password_incorrect" - CommentNotFound = "error.comment.not_found" - QuestionNotFound = "error.question.not_found" - AnswerNotFound = "error.answer.not_found" - CommentEditWithoutPermission = "error.comment.edit_without_permission" - DisallowVote = "error.object.disallow_vote" - DisallowFollow = "error.object.disallow_follow" - DisallowVoteYourSelf = "error.object.disallow_vote_your_self" - CaptchaVerificationFailed = "error.object.captcha_verification_failed" + EmailOrPasswordWrong = "error.object.email_or_password_incorrect" + CommentNotFound = "error.comment.not_found" + QuestionNotFound = "error.question.not_found" + AnswerNotFound = "error.answer.not_found" + CommentEditWithoutPermission = "error.comment.edit_without_permission" + DisallowVote = "error.object.disallow_vote" + DisallowFollow = "error.object.disallow_follow" + DisallowVoteYourSelf = "error.object.disallow_vote_your_self" + CaptchaVerificationFailed = "error.object.captcha_verification_failed" OldPasswordVerificationFailed = "error.object.old_password_verification_failed" NewPasswordSameAsPreviousSetting = "error.object.new_password_same_as_previous_setting" - UserNotFound = "error.user.not_found" - UsernameInvalid = "error.user.username_invalid" - UsernameDuplicate = "error.user.username_duplicate" - UserSetAvatar = "error.user.set_avatar" - EmailDuplicate = "error.email.duplicate" - EmailVerifyURLExpired = "error.email.verify_url_expired" - EmailNeedToBeVerified = "error.email.need_to_be_verified" - UserSuspended = "error.user.suspended" - ObjectNotFound = "error.object.not_found" - TagNotFound = "error.tag.not_found" - TagNotContainSynonym = "error.tag.not_contain_synonym_tags" - RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition" - ThemeNotFound = "error.theme.not_found" - LangNotFound = "error.lang.not_found" - ReportHandleFailed = "error.report.handle_failed" - ReportNotFound = "error.report.not_found" - ReadConfigFailed = "error.config.read_config_failed" - DatabaseConnectionFailed = "error.database.connection_failed" - InstallCreateTableFailed = "error.database.create_table_failed" - InstallConfigFailed = "error.install.create_config_failed" - SiteInfoNotFound = "error.site_info.not_found" - UploadFileSourceUnsupported = "error.upload.source_unsupported" - RecommendTagNotExist = "error.tag.recommend_tag_not_found" + UserNotFound = "error.user.not_found" + UsernameInvalid = "error.user.username_invalid" + UsernameDuplicate = "error.user.username_duplicate" + UserSetAvatar = "error.user.set_avatar" + EmailDuplicate = "error.email.duplicate" + EmailVerifyURLExpired = "error.email.verify_url_expired" + EmailNeedToBeVerified = "error.email.need_to_be_verified" + UserSuspended = "error.user.suspended" + ObjectNotFound = "error.object.not_found" + TagNotFound = "error.tag.not_found" + TagNotContainSynonym = "error.tag.not_contain_synonym_tags" + RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition" + ThemeNotFound = "error.theme.not_found" + LangNotFound = "error.lang.not_found" + ReportHandleFailed = "error.report.handle_failed" + ReportNotFound = "error.report.not_found" + ReadConfigFailed = "error.config.read_config_failed" + DatabaseConnectionFailed = "error.database.connection_failed" + InstallCreateTableFailed = "error.database.create_table_failed" + InstallConfigFailed = "error.install.create_config_failed" + SiteInfoNotFound = "error.site_info.not_found" + UploadFileSourceUnsupported = "error.upload.source_unsupported" + RecommendTagNotExist = "error.tag.recommend_tag_not_found" + RecommendTagEnter = "error.tag.recommend_tag_enter" ) diff --git a/internal/service/question_service.go b/internal/service/question_service.go index 6f36fbe7..3811c8ae 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -227,6 +227,15 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest return } + recommendExist, err := qs.tagCommon.ExistRecommend(ctx, req.Tags) + if err != nil { + return + } + if !recommendExist { + err = errors.BadRequest(reason.RecommendTagEnter).WithError(err).WithStack() + return + } + //CheckChangeTag oldTags, err := qs.tagCommon.GetObjectEntityTag(ctx, question.ID) if err != nil { @@ -240,9 +249,10 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest if err != nil { return } - CheckTag, CheckTaglist := qs.CheckChangeTag(ctx, oldTags, Tags) + + CheckTag, CheckTaglist := qs.CheckChangeReservedTag(ctx, oldTags, Tags) if !CheckTag { - err = errors.BadRequest(reason.UnauthorizedError).WithMsg(fmt.Sprintf("tag [%s] cannot be modified", + err = errors.BadRequest(reason.RequestFormatError).WithMsg(fmt.Sprintf("The reserved tag \"%s\" must be present.", strings.Join(CheckTaglist, ","))) return } @@ -299,8 +309,8 @@ func (qs *QuestionService) ChangeTag(ctx context.Context, objectTagData *schema. return qs.tagCommon.ObjectChangeTag(ctx, objectTagData) } -func (qs *QuestionService) CheckChangeTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) { - return qs.tagCommon.ObjectCheckChangeTag(ctx, oldobjectTagData, objectTagData) +func (qs *QuestionService) CheckChangeReservedTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) { + return qs.tagCommon.CheckChangeReservedTag(ctx, oldobjectTagData, objectTagData) } func (qs *QuestionService) SearchUserList(ctx context.Context, userName, order string, page, pageSize int, loginUserID string) ([]*schema.UserQuestionInfo, int64, error) { diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go index 42a156a7..3b3efc34 100644 --- a/internal/service/tag_common/tag_common.go +++ b/internal/service/tag_common/tag_common.go @@ -403,7 +403,7 @@ func (ts *TagCommonService) CheckTag(ctx context.Context, tags []string, userID return nil } -func (ts *TagCommonService) ObjectCheckChangeTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) { +func (ts *TagCommonService) CheckChangeReservedTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) { reservedTagsMap := make(map[string]bool) needTagsMap := make([]string, 0) for _, tag := range objectTagData {