diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 486795cb..a6884e6e 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -78,6 +78,12 @@ backend: tag: not_found: 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: not_found: other: "Theme not found." diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go index 7ac8f556..79c8b5fe 100644 --- a/internal/base/reason/reason.go +++ b/internal/base/reason/reason.go @@ -14,40 +14,41 @@ const ( ) const ( - EmailOrPasswordWrong = "error.object.email_or_password_incorrect" - CommentNotFound = "error.comment.not_found" - QuestionNotFound = "error.question.not_found" - QuestionCannotDeleted = "error.question.cannot_deleted" - AnswerNotFound = "error.answer.not_found" - AnswerCannotDeleted = "error.answer.cannot_deleted" - 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" + QuestionCannotDeleted = "error.question.cannot_deleted" + AnswerNotFound = "error.answer.not_found" + AnswerCannotDeleted = "error.answer.cannot_deleted" + 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 10db1fed..4adb0330 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 {