From 89907acaeef6074710bdd884e084356f6d2f4e9f Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Tue, 29 Oct 2024 14:10:20 +0800 Subject: [PATCH] 12111 --- api/service/Question.go | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/api/service/Question.go b/api/service/Question.go index 7c9bf12..f1bffb1 100644 --- a/api/service/Question.go +++ b/api/service/Question.go @@ -86,9 +86,18 @@ func (r *QuestionService) AddQuestion(req requests.AddQuestion) (bool, error) { if !strings.Contains(questionOption, s) { return false, errors.New("请正确输入选项序号") } + } - // 判断答案是否在选项中 - if !strings.Contains(questionAnswer, s) { + // 判断答案是否在选项中 + for _, s := range req.QuestionAnswer { + isExist := false + for _, s2 := range req.QuestionOption { + if strings.Contains(s2, s) { + isExist = true + } + } + + if isExist == false { return false, errors.New("答案不在选项中,请重新填写") } } @@ -463,13 +472,12 @@ func (r *QuestionService) PutQuestion(questionId int64, req requests.PutQuestion } // 答案 - result := make([]string, len(req.QuestionAnswer)) + questionAnswer := make([]string, len(req.QuestionAnswer)) for i, v := range req.QuestionAnswer { - result[i] = v + questionAnswer[i] = v } - questionAnswer := strings.Join(result, ",") - questionData["question_answer"] = questionAnswer + questionData["question_answer"] = strings.Join(questionAnswer, ",") // 判断选项 if req.QuestionType == 1 || req.QuestionType == 2 { @@ -485,7 +493,12 @@ func (r *QuestionService) PutQuestion(questionId int64, req requests.PutQuestion return false, errors.New("存在为空的选项") } - // 取出首字母 + if option == "," { + tx.Rollback() + return false, errors.New("存在为空的选项") + } + + // 取出选项首字母 s := option[0:1] // 判断是否为正常选项 @@ -493,9 +506,18 @@ func (r *QuestionService) PutQuestion(questionId int64, req requests.PutQuestion tx.Rollback() return false, errors.New("请正确输入选项序号") } + } - // 判断答案是否在选项中 - if !strings.Contains(questionAnswer, s) { + // 判断答案是否在选项中 + for _, s := range req.QuestionAnswer { + isExist := false + for _, s2 := range req.QuestionOption { + if strings.Contains(s2, s) { + isExist = true + } + } + + if isExist == false { tx.Rollback() return false, errors.New("答案不在选项中,请重新填写") }