From 0f544c169d1cbcaeea29d9a6cc0a1778105414ed Mon Sep 17 00:00:00 2001 From: yanlongqi Date: Thu, 13 Nov 2025 03:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BB=9F=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=8D=E5=AF=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - internal/handlers/practice_handler.go | 87 +-------------------- internal/handlers/wrong_question_handler.go | 10 +-- internal/services/wrong_question_service.go | 6 +- main.go | 3 - 5 files changed, 9 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index 73339bc..c6bdd1b 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,6 @@ yarn dev - `GET /api/practice/questions` - 获取练习题目列表 (支持分页和类型过滤) - `GET /api/practice/questions/:id` - 获取指定练习题目 - `POST /api/practice/submit` - 提交练习答案 (简答题自动AI评分) -- `GET /api/practice/types` - 获取题型列表 #### 其他 - `GET /api/health` - 健康检查端点 diff --git a/internal/handlers/practice_handler.go b/internal/handlers/practice_handler.go index c755200..e4beff5 100644 --- a/internal/handlers/practice_handler.go +++ b/internal/handlers/practice_handler.go @@ -172,52 +172,6 @@ func GetPracticeQuestionByID(c *gin.Context) { }) } -// GetRandomPracticeQuestion 获取随机练习题目 -func GetRandomPracticeQuestion(c *gin.Context) { - typeParam := c.Query("type") - - log.Printf("[GetRandomPracticeQuestion] 收到请求 (type: '%s')", typeParam) - - db := database.GetDB() - var question models.PracticeQuestion - - query := db.Model(&models.PracticeQuestion{}) - if typeParam != "" { - // 如果请求论述题,检查权限 - if strings.HasSuffix(typeParam, "-essay") && !checkEssayPermission(c, typeParam) { - // 权限不足,返回暂无题目 - log.Printf("[GetRandomPracticeQuestion] 权限检查失败,返回暂无题目 (type: '%s')", typeParam) - c.JSON(http.StatusNotFound, gin.H{ - "success": false, - "message": "暂无题目", - }) - return - } - query = query.Where("type = ?", typeParam) - log.Printf("[GetRandomPracticeQuestion] 添加题型过滤 (type: '%s')", typeParam) - } - - // 使用PostgreSQL的随机排序 - if err := query.Order("RANDOM()").First(&question).Error; err != nil { - log.Printf("[GetRandomPracticeQuestion] 未找到题目 (type: '%s', error: %v)", typeParam, err) - c.JSON(http.StatusNotFound, gin.H{ - "success": false, - "message": "暂无题目", - }) - return - } - - log.Printf("[GetRandomPracticeQuestion] 找到题目 (id: %d, type: '%s')", question.ID, question.Type) - - // 转换为DTO - dto := convertToDTO(question) - - c.JSON(http.StatusOK, gin.H{ - "success": true, - "data": dto, - }) -} - // SubmitPracticeAnswer 提交练习答案 func SubmitPracticeAnswer(c *gin.Context) { var submit models.PracticeAnswerSubmit @@ -452,45 +406,6 @@ func SubmitPracticeAnswer(c *gin.Context) { }) } -// GetPracticeQuestionTypes 获取题型列表 -func GetPracticeQuestionTypes(c *gin.Context) { - types := []gin.H{ - { - "type": "fill-in-blank", - "type_name": "填空题", - }, - { - "type": "true-false", - "type_name": "判断题", - }, - { - "type": "multiple-choice", - "type_name": "选择题", - }, - { - "type": "multiple-selection", - "type_name": "多选题", - }, - { - "type": "short-answer", - "type_name": "简答题", - }, - { - "type": "ordinary-essay", - "type_name": "普通涉密人员论述题", - }, - { - "type": "management-essay", - "type_name": "保密管理人员论述题", - }, - } - - c.JSON(http.StatusOK, gin.H{ - "success": true, - "data": types, - }) -} - type CheckAnswer interface { check(userAnswer, correctAnswer interface{}) bool } @@ -705,7 +620,7 @@ func GetStatistics(c *gin.Context) { return } - uid, ok := userID.(uint) + uid, ok := userID.(int64) if !ok { c.JSON(http.StatusInternalServerError, gin.H{ "success": false, diff --git a/internal/handlers/wrong_question_handler.go b/internal/handlers/wrong_question_handler.go index 2a3f915..c74f5c4 100644 --- a/internal/handlers/wrong_question_handler.go +++ b/internal/handlers/wrong_question_handler.go @@ -130,7 +130,7 @@ func GetWrongQuestionStats(c *gin.Context) { return } - stats, err := services.GetWrongQuestionStats(userID.(uint)) + stats, err := services.GetWrongQuestionStats(userID.(int64)) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "获取统计失败"}) return @@ -159,14 +159,14 @@ func GetRecommendedWrongQuestions(c *gin.Context) { } // 获取要排除的题目ID(前端传递当前题目ID,避免重复推荐) - excludeQuestionID := uint(0) + excludeQuestionID := int64(0) if e := c.Query("exclude"); e != "" { - if parsed, err := strconv.ParseUint(e, 10, 32); err == nil { - excludeQuestionID = uint(parsed) + if parsed, err := strconv.ParseUint(e, 10, 64); err == nil { + excludeQuestionID = int64(parsed) } } - questions, err := services.GetRecommendedWrongQuestions(userID.(uint), limit, excludeQuestionID) + questions, err := services.GetRecommendedWrongQuestions(userID.(int64), limit, excludeQuestionID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "获取推荐错题失败"}) return diff --git a/internal/services/wrong_question_service.go b/internal/services/wrong_question_service.go index 775c7dc..71b1090 100644 --- a/internal/services/wrong_question_service.go +++ b/internal/services/wrong_question_service.go @@ -111,7 +111,7 @@ func RecordCorrectAnswer(userID, questionID int64, userAnswer, correctAnswer int } // GetWrongQuestionStats 获取错题统计 -func GetWrongQuestionStats(userID uint) (*models.WrongQuestionStats, error) { +func GetWrongQuestionStats(userID int64) (*models.WrongQuestionStats, error) { db := database.GetDB() stats := &models.WrongQuestionStats{ @@ -193,7 +193,7 @@ func GetWrongQuestionStats(userID uint) (*models.WrongQuestionStats, error) { } // calculateTrendData 计算错题趋势数据 -func calculateTrendData(db *gorm.DB, userID uint, days int) []models.TrendPoint { +func calculateTrendData(db *gorm.DB, userID int64, days int) []models.TrendPoint { trendData := make([]models.TrendPoint, days) now := time.Now() @@ -223,7 +223,7 @@ func calculateTrendData(db *gorm.DB, userID uint, days int) []models.TrendPoint // 1. 最优先推荐掌握度为0的题目(从未答对过) // 2. 其次推荐掌握度低的题目(mastery_level 从低到高) // 3. 最后推荐最近答错的题目 -func GetRecommendedWrongQuestions(userID uint, limit int, excludeQuestionID uint) ([]models.WrongQuestion, error) { +func GetRecommendedWrongQuestions(userID int64, limit int, excludeQuestionID int64) ([]models.WrongQuestion, error) { db := database.GetDB() var questions []models.WrongQuestion diff --git a/main.go b/main.go index f946b27..690f0f0 100644 --- a/main.go +++ b/main.go @@ -33,9 +33,6 @@ func main() { api.POST("/login", handlers.Login) // 用户登录 api.POST("/register", handlers.Register) // 用户注册 - // 公开的练习题相关API - api.GET("/practice/types", handlers.GetPracticeQuestionTypes) // 获取题型列表 - // 需要认证的路由 auth := api.Group("", middleware.Auth()) {