diff --git a/internal/handlers/exam_handler.go b/internal/handlers/exam_handler.go index a9ed86c..11b6a18 100644 --- a/internal/handlers/exam_handler.go +++ b/internal/handlers/exam_handler.go @@ -140,6 +140,9 @@ func GetExam(c *gin.Context) { return } + // 是否显示答案 + showAnswer := c.Query("show_answer") == "true" + // 查询题目详情 var questions []models.PracticeQuestion if err := db.Where("id IN ?", questionIDs).Find(&questions).Error; err != nil { @@ -148,24 +151,20 @@ func GetExam(c *gin.Context) { return } - // 按原始顺序排序题目 + // 按原始顺序排序题目并转换为DTO questionMap := make(map[uint]models.PracticeQuestion) for _, q := range questions { questionMap[q.ID] = q } - orderedQuestions := make([]models.PracticeQuestion, 0, len(questionIDs)) + orderedDTOs := make([]models.PracticeQuestionDTO, 0, len(questionIDs)) for _, id := range questionIDs { if q, ok := questionMap[id]; ok { - orderedQuestions = append(orderedQuestions, q) - } - } - - // 是否显示答案 - showAnswer := c.Query("show_answer") == "true" - if !showAnswer { - // 不显示答案时,隐藏答案字段 - for i := range orderedQuestions { - orderedQuestions[i].AnswerData = "" + dto := convertToDTO(q) + // 是否显示答案 + if !showAnswer { + dto.Answer = nil + } + orderedDTOs = append(orderedDTOs, dto) } } @@ -173,7 +172,7 @@ func GetExam(c *gin.Context) { "success": true, "data": gin.H{ "exam": exam, - "questions": orderedQuestions, + "questions": orderedDTOs, }, }) }