Skip to content

Commit

Permalink
Merge pull request #25 from ppolariss/main
Browse files Browse the repository at this point in the history
chore: Refactor RetrieveQuestions function to improve code readabilit…
  • Loading branch information
JingYiJun authored Jul 29, 2024
2 parents 0b32381 + 48f59fb commit ff95466
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
2 changes: 0 additions & 2 deletions apis/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ LOAD_FILES:
GlobalQuestionConfig.Lock()
GlobalQuestionConfig.Questions = newQuestions
GlobalQuestionConfig.CurrentVersion = newQuestionCurrentVersion
jsonQuestions, _ := json.Marshal(GlobalQuestionConfig.Questions)
log.Debug().Msgf("questionsResponse: %s", string(jsonQuestions))
GlobalQuestionConfig.Unlock()

return nil
Expand Down
27 changes: 4 additions & 23 deletions apis/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func RetrieveQuestions(c *fiber.Ctx) (err error) {
return common.InternalServerError("[retrieve questions]: number of campus questions invalid")
}

if number == 0 {
return common.InternalServerError("[retrieve questions]: number of questions too small")
}

var questionsResponse = QuestionConfig{
Version: version,
Spec: QuestionSpec{
Expand All @@ -89,25 +93,8 @@ func RetrieveQuestions(c *fiber.Ctx) (err error) {

questionsResponse.Questions = make([]Question, number)
tmpQuestions := make([]*Question, 0, number)

if number == 0 {
return common.InternalServerError("[retrieve questions]: number of questions too small")
}

tmpQuestions = append(tmpQuestions, requiredQuestions...)

jsonTmpQuestions, _ := json.Marshal(questionConfig)
log.Debug().Msgf("questionsResponse: %s", string(jsonTmpQuestions))

jsonTmpQuestions, _ = json.Marshal(tmpQuestions)
log.Debug().Msgf("questionsResponse: %s", string(jsonTmpQuestions))

// for i, question := range requiredQuestions {
// tmpQuestions[i] = question
// // questionsResponse.Questions[i] = *question
// }

// questionConfig.Questions = append(questionConfig.Questions, optionalQuestions...)
if numberOfOptionalQuestions == -1 {
// send all opntional questions
tmpQuestions = append(tmpQuestions, optionalQuestions...)
Expand Down Expand Up @@ -136,9 +123,6 @@ func RetrieveQuestions(c *fiber.Ctx) (err error) {
tmpQuestions = append(tmpQuestions, chosenCampusQuestions[:numberOfCampusQuestions]...)
}

jsonTmpQuestions, _ = json.Marshal(tmpQuestions)
log.Debug().Msgf("questionsResponse: %s", string(jsonTmpQuestions))

if !inOrder {
rand.Shuffle(len(tmpQuestions), func(i, j int) {
tmpQuestions[i], tmpQuestions[j] = tmpQuestions[j], tmpQuestions[i]
Expand All @@ -149,9 +133,6 @@ func RetrieveQuestions(c *fiber.Ctx) (err error) {
})
}

jsonTmpQuestions, _ = json.Marshal(tmpQuestions)
log.Debug().Msgf("questionsResponse: %s", string(jsonTmpQuestions))

for i, question := range tmpQuestions {
questionsResponse.Questions[i] = *question
}
Expand Down
13 changes: 8 additions & 5 deletions apis/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,19 @@ type Question struct {

// QuestionSpec 题库的发题、判题的规格 schema
type QuestionSpec struct {
// 总题目数量
// Deprecated
NumberOfQuestions int `json:"number_of_questions" yaml:"number_of_questions"`

// 表示可选题的题目数量。
// 发送题目时,题库中的必做题都会发送,可选题会根据题目数量随机发送
// 如果总的题目数量小于题库中的必做题数量,将会在解析时返回错误
// 如果设置为 0 或者不设置,则题库中的所有题目都会发送
// 如果设置为 -1,则题库中的必做题都会发送,可选题不会发送
// 可选题的题目数量
// 发送题目时,题库中的必做题都会发送,可选题会根据题目数量随机发送
// 如果可选题的题目数量大于题库中的可选题数量,将会在解析时返回错误
// 如果设置为 0 或者不设置,可选题不会发送
// 如果设置为 -1,则题库中的可选题都会发送
NumberOfOptionalQuestions int `json:"number_of_optional_questions" yaml:"number_of_optional_questions"`

// 校园题的数量
// 其余规则同可选题
NumberOfCampusQuestions int `json:"number_of_campus_questions" yaml:"number_of_campus_questions"`

// 表示是否由题目声明顺序由上到下顺序出题,默认为 false,即乱序出题
Expand Down

0 comments on commit ff95466

Please sign in to comment.