Skip to content

Commit

Permalink
Merge pull request #18 from yujular/main
Browse files Browse the repository at this point in the history
feat(question): Change Answer to Index of Options
  • Loading branch information
JingYiJun authored Apr 2, 2024
2 parents 832cb7f + a7271f5 commit 0283261
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
18 changes: 9 additions & 9 deletions apis/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/goccy/go-json"
"github.com/opentreehole/go-common"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"

"auth_next/config"
Expand Down Expand Up @@ -159,11 +158,9 @@ LOAD_FILES:
valid = false
continue
}
if !slices.Contains(currentQuestion.Options, currentQuestion.Answer[0]) {
currentQuestion.Options = append(currentQuestion.Options, currentQuestion.Answer[0])
}
currentQuestion.AnswerOptions = append(currentQuestion.AnswerOptions, currentQuestion.Options[currentQuestion.Answer[0]])
case TrueOrFalse:
if len(currentQuestion.Answer) != 1 || (currentQuestion.Answer[0] != "true" && currentQuestion.Answer[0] != "false") {
if len(currentQuestion.Answer) != 1 || (currentQuestion.Answer[0] != 0 && currentQuestion.Answer[0] != 1) {
log.Warn().
Str("filename", file.Name()).
Str("question", currentQuestion.Question).
Expand All @@ -172,6 +169,8 @@ LOAD_FILES:
valid = false
continue
}
currentQuestion.AnswerOptions = append(currentQuestion.AnswerOptions,
currentQuestion.Options[currentQuestion.Answer[0]])
case MultiSelection:
if len(currentQuestion.Answer) < 1 {
log.Warn().
Expand All @@ -182,12 +181,13 @@ LOAD_FILES:
valid = false
continue
}
for _, answer := range currentQuestion.Answer {
if !slices.Contains(currentQuestion.Options, answer) {
currentQuestion.Options = append(currentQuestion.Options, answer)
currentQuestion.AnswerOptions = make([]string, 0, len(currentQuestion.Answer))
for _, index := range currentQuestion.Answer {
if index < len(currentQuestion.Options) {
currentQuestion.AnswerOptions = append(currentQuestion.AnswerOptions, currentQuestion.Options[index])
}
}
sort.Strings(currentQuestion.Answer)
sort.Strings(currentQuestion.AnswerOptions)
}
}
if !valid {
Expand Down
4 changes: 2 additions & 2 deletions apis/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ func AnswerQuestions(c *fiber.Ctx) (err error) {
case SingleSelection:
fallthrough
case TrueOrFalse:
if len(answer.Answer) != 1 || answer.Answer[0] != question.Answer[0] {
if len(answer.Answer) != 1 || answer.Answer[0] != question.AnswerOptions[0] {
wrongQuestions = append(wrongQuestions, answer.ID)
continue
}
case MultiSelection:
sortedAnswer := make([]string, len(answer.Answer))
copy(sortedAnswer, answer.Answer)
sort.Strings(sortedAnswer)
if slices.Equal(sortedAnswer, question.Answer) {
if !slices.Equal(sortedAnswer, question.AnswerOptions) {
wrongQuestions = append(wrongQuestions, answer.ID)
continue
}
Expand Down
7 changes: 5 additions & 2 deletions apis/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,14 @@ type Question struct {
// 问题描述
Question string `json:"question" yaml:"question" validate:"required"`

// 答案描述,单选、判断、多选
// 答案索引,单选、判断、多选,从 0 开始
Answer []int `json:"answer,omitempty" yaml:"answer,omitempty" validate:"min=1"`

// 根据Answer的索引解析出的答案, 单选、判断、多选
// 如果是单选题,则只有一个答案
// 如果是多选题,则有一个或多个答案
// 如果是判断题,则只有一个答案,且只能是 true 或者 false
Answer []string `json:"answer,omitempty" yaml:"answer,omitempty" validate:"min=1"`
AnswerOptions []string `json:"-" yaml:"-"`

// 选项描述,单选、判断、多选
// 有一个或多个选项,如果是判断题,则选项只能是 true 或者 false
Expand Down

0 comments on commit 0283261

Please sign in to comment.