AnCao/internal/models/practice_question.go
yanlongqi dd2b197516 优化题库管理系统:实现自动编号、动态表单和答案回显
**后端优化**
- 实现题目编号自动生成机制,按题型连续编号
- 移除分页限制,返回所有题目
- 支持题型筛选和关键词搜索
- 题目按题型和编号排序
- DTO 中包含答案字段,支持编辑时回显
- 选项按字母顺序排序

**前端优化**
- 移除手动输入题目ID,系统自动生成
- 实现动态表单,支持添加/删除选项和答案
- 添加题型筛选下拉框
- 添加搜索框,支持搜索题目内容和编号
- 优化答案回显逻辑,直接使用后端返回的答案数据
- 表格显示题目编号列

**修复问题**
- 修复 PostgreSQL SQL 语法错误
- 修复编辑题目时答案无法正确回显的问题
- 修复题目列表不完整的问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 22:00:29 +08:00

44 lines
2.0 KiB
Go

package models
import "gorm.io/gorm"
// PracticeQuestion 练习题目模型
type PracticeQuestion struct {
gorm.Model
QuestionID string `gorm:"size:50;not null" json:"question_id"` // 题目ID (原JSON中的id字段)
Type string `gorm:"index;size:30;not null" json:"type"` // 题目类型
TypeName string `gorm:"size:50" json:"type_name"` // 题型名称(中文)
Question string `gorm:"type:text;not null" json:"question"` // 题目内容
AnswerData string `gorm:"type:text;not null" json:"-"` // 答案数据(JSON格式存储)
OptionsData string `gorm:"type:text" json:"-"` // 选项数据(JSON格式存储,用于选择题)
}
// TableName 指定表名
func (PracticeQuestion) TableName() string {
return "practice_questions"
}
// PracticeQuestionDTO 用于前端返回的数据传输对象
type PracticeQuestionDTO struct {
ID uint `json:"id"` // 数据库自增ID
QuestionID string `json:"question_id"` // 题目编号(原JSON中的id)
Type string `json:"type"` // 前端使用的简化类型: single, multiple, judge, fill
Content string `json:"content"` // 题目内容
Options []Option `json:"options"` // 选择题选项数组
Category string `json:"category"` // 题目分类
Answer interface{} `json:"answer"` // 正确答案(用于题目管理编辑)
}
// PracticeAnswerSubmit 练习题答案提交
type PracticeAnswerSubmit struct {
QuestionID uint `json:"question_id" binding:"required"` // 数据库ID
Answer interface{} `json:"answer" binding:"required"` // 用户答案
}
// PracticeAnswerResult 练习题答案结果
type PracticeAnswerResult struct {
Correct bool `json:"correct"` // 是否正确
UserAnswer interface{} `json:"user_answer"` // 用户答案
CorrectAnswer interface{} `json:"correct_answer,omitempty"` // 正确答案(仅在错误时返回)
}