diff --git a/internal/handlers/wrong_question_handler.go b/internal/handlers/wrong_question_handler.go index ac46d06..a54ac23 100644 --- a/internal/handlers/wrong_question_handler.go +++ b/internal/handlers/wrong_question_handler.go @@ -263,3 +263,41 @@ func GetRandomWrongQuestion(c *gin.Context) { "data": dto, }) } + +// DeleteWrongQuestion 删除单个错题 +func DeleteWrongQuestion(c *gin.Context) { + userID, exists := c.Get("user_id") + if !exists { + c.JSON(http.StatusUnauthorized, gin.H{ + "success": false, + "message": "未登录", + }) + return + } + + wrongQuestionID := c.Param("id") + db := database.GetDB() + + // 删除错题(确保只能删除自己的错题) + result := db.Where("id = ? AND user_id = ?", wrongQuestionID, userID).Delete(&models.WrongQuestion{}) + if result.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "删除失败", + }) + return + } + + if result.RowsAffected == 0 { + c.JSON(http.StatusNotFound, gin.H{ + "success": false, + "message": "错题不存在", + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "已删除", + }) +} diff --git a/main.go b/main.go index 30baccb..4691927 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,7 @@ func main() { auth.GET("/wrong-questions", handlers.GetWrongQuestions) // 获取错题列表 auth.GET("/wrong-questions/stats", handlers.GetWrongQuestionStats) // 获取错题统计 auth.GET("/wrong-questions/random", handlers.GetRandomWrongQuestion) // 获取随机错题 + auth.DELETE("/wrong-questions/:id", handlers.DeleteWrongQuestion) // 删除单个错题 auth.PUT("/wrong-questions/:id/mastered", handlers.MarkWrongQuestionMastered) // 标记已掌握 auth.DELETE("/wrong-questions", handlers.ClearWrongQuestions) // 清空错题本 } diff --git a/web/src/api/question.ts b/web/src/api/question.ts index 18a9ce1..0051c45 100644 --- a/web/src/api/question.ts +++ b/web/src/api/question.ts @@ -57,6 +57,11 @@ export const markWrongQuestionMastered = (id: number) => { return request.put>(`/wrong-questions/${id}/mastered`) } +// 删除单个错题 +export const deleteWrongQuestion = (id: number) => { + return request.delete>(`/wrong-questions/${id}`) +} + // 清空错题本 export const clearWrongQuestions = () => { return request.delete>('/wrong-questions') diff --git a/web/src/pages/WrongQuestions.tsx b/web/src/pages/WrongQuestions.tsx index 82c76a9..76b8209 100644 --- a/web/src/pages/WrongQuestions.tsx +++ b/web/src/pages/WrongQuestions.tsx @@ -67,6 +67,27 @@ const WrongQuestions: React.FC = () => { }) } + // 删除单个错题 + const handleDelete = (id: number) => { + Modal.confirm({ + title: '确定要删除这道错题吗?', + content: '删除后将无法恢复', + okText: '确定', + cancelText: '取消', + onOk: async () => { + try { + const res = await questionApi.deleteWrongQuestion(id) + if (res.success) { + message.success('已删除') + loadWrongQuestions() + } + } catch (error) { + message.error('删除失败') + } + }, + }) + } + // 开始错题练习 const handlePractice = () => { // 跳转到答题页面,错题练习模式 @@ -170,7 +191,21 @@ const WrongQuestions: React.FC = () => { loading={loading} dataSource={wrongQuestions} renderItem={(item) => ( - + } + onClick={() => handleDelete(item.id)} + > + 删除 + + ]} + >