From 5b005e652f851614ac2634bab77987573c2a33ba Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Wed, 23 Nov 2022 11:38:39 +0800 Subject: [PATCH] fix admin update question --- internal/service/question_service.go | 56 ++++++++++++++-------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/internal/service/question_service.go b/internal/service/question_service.go index b4ce1e03..18a657e9 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -270,13 +270,40 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest if !has { return } + // If it's not admin if !req.IsAdmin { if dbinfo.UserID != req.UserID { return questionInfo, errors.BadRequest(reason.QuestionCannotUpdate) } - } + //CheckChangeTag + oldTags, tagerr := qs.tagCommon.GetObjectEntityTag(ctx, question.ID) + if err != nil { + return questionInfo, tagerr + } + tagNameList := make([]string, 0) + for _, tag := range req.Tags { + tagNameList = append(tagNameList, tag.SlugName) + } + Tags, tagerr := qs.tagCommon.GetTagListByNames(ctx, tagNameList) + if err != nil { + return questionInfo, tagerr + } + CheckTag, CheckTaglist := qs.CheckChangeReservedTag(ctx, oldTags, Tags) + if !CheckTag { + errMsg := fmt.Sprintf(`The reserved tag %s must be present.`, + strings.Join(CheckTaglist, ",")) + errorlist := make([]*validator.FormErrorField, 0) + errorlist = append(errorlist, &validator.FormErrorField{ + ErrorField: "tags", + ErrorMsg: errMsg, + }) + err = errors.BadRequest(reason.RequestFormatError).WithMsg(errMsg) + return errorlist, err + } + } + // Check whether mandatory labels are selected recommendExist, err := qs.tagCommon.ExistRecommend(ctx, req.Tags) if err != nil { return @@ -291,33 +318,6 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest return errorlist, err } - //CheckChangeTag - oldTags, err := qs.tagCommon.GetObjectEntityTag(ctx, question.ID) - if err != nil { - return - } - tagNameList := make([]string, 0) - for _, tag := range req.Tags { - tagNameList = append(tagNameList, tag.SlugName) - } - Tags, err := qs.tagCommon.GetTagListByNames(ctx, tagNameList) - if err != nil { - return - } - - CheckTag, CheckTaglist := qs.CheckChangeReservedTag(ctx, oldTags, Tags) - if !CheckTag { - errMsg := fmt.Sprintf(`The reserved tag %s must be present.`, - strings.Join(CheckTaglist, ",")) - errorlist := make([]*validator.FormErrorField, 0) - errorlist = append(errorlist, &validator.FormErrorField{ - ErrorField: "tags", - ErrorMsg: errMsg, - }) - err = errors.BadRequest(reason.RequestFormatError).WithMsg(errMsg) - return errorlist, err - } - //update question to db err = qs.questionRepo.UpdateQuestion(ctx, question, []string{"title", "original_text", "parsed_text", "updated_at"}) if err != nil {