This commit is contained in:
wucongxing8150 2024-10-29 13:33:38 +08:00
parent 2fbc7d7e1c
commit 031f5c3b85

View File

@ -74,14 +74,17 @@ func (r *QuestionService) AddQuestion(req requests.AddQuestion) (bool, error) {
} }
questionOption := "A.B.C.D.E.F.G.H.I.J.K." questionOption := "A.B.C.D.E.F.G.H.I.J.K."
for _, s := range req.QuestionOption { for _, option := range req.QuestionOption {
if s == "" { if option == "" {
return false, errors.New("存在为空的选项") return false, errors.New("存在为空的选项")
} }
// 取出首字母
s := option[0:1]
// 判断是否为正常选项 // 判断是否为正常选项
if !strings.Contains(questionOption, s) { if !strings.Contains(questionOption, s) {
return false, errors.New("选项存在错误标识") return false, errors.New("请正确输入选项序号")
} }
// 判断答案是否在选项中 // 判断答案是否在选项中
@ -404,7 +407,6 @@ func (r *QuestionService) PutQuestion(questionId int64, req requests.PutQuestion
} }
// 验证一级标签 // 验证一级标签
firstLabelId, err := strconv.ParseInt(req.FirstLabelId, 10, 64) firstLabelId, err := strconv.ParseInt(req.FirstLabelId, 10, 64)
if err != nil { if err != nil {
return false, err return false, err
@ -461,13 +463,43 @@ func (r *QuestionService) PutQuestion(questionId int64, req requests.PutQuestion
} }
// 答案 // 答案
if len(req.QuestionAnswer) > 0 {
result := make([]string, len(req.QuestionAnswer)) result := make([]string, len(req.QuestionAnswer))
for i, v := range req.QuestionAnswer { for i, v := range req.QuestionAnswer {
result[i] = v result[i] = v
} }
questionData["question_answer"] = strings.Join(result, ",") questionAnswer := strings.Join(result, ",")
questionData["question_answer"] = questionAnswer
// 判断选项
if req.QuestionType == 1 || req.QuestionType == 2 {
if len(req.QuestionOption) == 0 {
tx.Rollback()
return false, errors.New("请填入选项")
}
questionOption := "ABCDEFGHIJK"
for _, option := range req.QuestionOption {
if option == "" {
tx.Rollback()
return false, errors.New("存在为空的选项")
}
// 取出首字母
s := option[0:1]
// 判断是否为正常选项
if !strings.Contains(questionOption, s) {
tx.Rollback()
return false, errors.New("请正确输入选项序号")
}
// 判断答案是否在选项中
if !strings.Contains(questionAnswer, s) {
tx.Rollback()
return false, errors.New("答案不在选项中,请重新填写")
}
}
} }
// 验证重复 // 验证重复