本次提交重构了试卷分享功能,从"副本模式"改为"关联表模式",并修复了关键的类型不匹配问题。 ## 主要更新 ### 后端架构重构 - 新增 ExamShare 关联表模型,替代原有的试卷副本方式 - 修复 User.ID (int64) 与 ExamShare 外键的类型不匹配问题 - 更新所有相关 API 以使用新的关联表架构 - 添加 IsAccessibleBy 和 GetAccessibleExams 权限检查方法 ### 类型系统修复 - ExamShare.SharedByID/SharedToID: uint → int64 - IsAccessibleBy/GetAccessibleExams 参数: uint → int64 - 修复所有涉及用户ID类型转换的代码 ### 新增工具 - cmd/migrate_exam_shares.go: 数据迁移脚本(旧数据迁移) - cmd/cleanup/main.go: 数据库表清理工具 ### API 更新 - ShareExam: 创建分享关联记录而非复制试卷 - GetExamList: 返回分享人信息和参与人数统计 - GetExamRecord: 支持查看共享试卷的其他用户记录 - GetExamRecordList: 按试卷ID查询所有用户的考试记录 ### 前端更新 - 更新 TypeScript 类型定义以匹配新的 API 响应 - 添加分享人标签显示("来自 XXX") - 考试记录列表显示所有参与者信息 ## 技术细节 - 使用 GORM 外键关联和 Preload 优化查询 - 添加唯一索引防止重复分享 - 事务保护数据一致性 - 软删除支持数据恢复 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
31 lines
985 B
Go
31 lines
985 B
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ExamShare 试卷分享关联表
|
|
type ExamShare struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
ExamID uint `gorm:"not null;uniqueIndex:uk_exam_shared_to" json:"exam_id"`
|
|
SharedByID int64 `gorm:"not null;index" json:"shared_by_id"`
|
|
SharedToID int64 `gorm:"not null;uniqueIndex:uk_exam_shared_to" json:"shared_to_id"`
|
|
SharedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP" json:"shared_at"`
|
|
|
|
// 关联关系
|
|
Exam *Exam `gorm:"foreignKey:ExamID" json:"exam,omitempty"`
|
|
SharedBy *User `gorm:"foreignKey:SharedByID;references:ID" json:"shared_by,omitempty"`
|
|
SharedTo *User `gorm:"foreignKey:SharedToID;references:ID" json:"shared_to,omitempty"`
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (ExamShare) TableName() string {
|
|
return "exam_shares"
|
|
}
|