init
This commit is contained in:
145
serve/internal/handler/learning_session_handler.go
Normal file
145
serve/internal/handler/learning_session_handler.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/common"
|
||||
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type LearningSessionHandler struct {
|
||||
sessionService *services.LearningSessionService
|
||||
}
|
||||
|
||||
func NewLearningSessionHandler(sessionService *services.LearningSessionService) *LearningSessionHandler {
|
||||
return &LearningSessionHandler{sessionService: sessionService}
|
||||
}
|
||||
|
||||
// StartLearning 开始学习
|
||||
// POST /api/v1/vocabulary/books/:id/learn
|
||||
func (h *LearningSessionHandler) StartLearning(c *gin.Context) {
|
||||
userID := c.GetInt64("user_id")
|
||||
bookID := c.Param("id")
|
||||
|
||||
var req struct {
|
||||
DailyGoal int `json:"dailyGoal" binding:"required,min=1,max=100"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.ErrorResponse(c, 400, "参数错误:"+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 创建学习会话
|
||||
session, err := h.sessionService.StartLearningSession(userID, bookID, req.DailyGoal)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "创建学习会话失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取今日学习任务(新词数 = 用户选择,复习词 = 所有到期)
|
||||
tasks, err := h.sessionService.GetTodayLearningTasks(userID, bookID, req.DailyGoal)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "获取学习任务失败")
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessResponse(c, gin.H{
|
||||
"session": session,
|
||||
"tasks": tasks,
|
||||
})
|
||||
}
|
||||
|
||||
// GetTodayTasks 获取今日学习任务
|
||||
// GET /api/v1/vocabulary/books/:id/tasks
|
||||
func (h *LearningSessionHandler) GetTodayTasks(c *gin.Context) {
|
||||
userID := c.GetInt64("user_id")
|
||||
bookID := c.Param("id")
|
||||
|
||||
newWordsLimit, _ := strconv.Atoi(c.DefaultQuery("newWords", "20"))
|
||||
|
||||
// 获取学习任务(复习词不限数量)
|
||||
tasks, err := h.sessionService.GetTodayLearningTasks(userID, bookID, newWordsLimit)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "获取学习任务失败")
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessResponse(c, tasks)
|
||||
}
|
||||
|
||||
// SubmitWordStudy 提交单词学习结果
|
||||
// POST /api/v1/vocabulary/words/:id/study
|
||||
func (h *LearningSessionHandler) SubmitWordStudy(c *gin.Context) {
|
||||
userID := c.GetInt64("user_id")
|
||||
wordIDStr := c.Param("id")
|
||||
|
||||
wordID, err := strconv.ParseInt(wordIDStr, 10, 64)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 400, "无效的单词ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Difficulty string `json:"difficulty" binding:"required,oneof=forgot hard good easy perfect"`
|
||||
SessionID int64 `json:"sessionId"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.ErrorResponse(c, 400, "参数错误:"+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 记录学习结果
|
||||
progress, err := h.sessionService.RecordWordStudy(userID, wordID, req.Difficulty)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "记录学习结果失败")
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessResponse(c, progress)
|
||||
}
|
||||
|
||||
// GetLearningStatistics 获取学习统计
|
||||
// GET /api/v1/vocabulary/books/:id/statistics
|
||||
func (h *LearningSessionHandler) GetLearningStatistics(c *gin.Context) {
|
||||
userID := c.GetInt64("user_id")
|
||||
bookID := c.Param("id")
|
||||
|
||||
stats, err := h.sessionService.GetLearningStatistics(userID, bookID)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "获取学习统计失败")
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessResponse(c, stats)
|
||||
}
|
||||
|
||||
// GetTodayOverallStatistics 获取今日总体学习统计
|
||||
// GET /api/v1/learning/today/statistics
|
||||
func (h *LearningSessionHandler) GetTodayOverallStatistics(c *gin.Context) {
|
||||
userID := c.GetInt64("user_id")
|
||||
|
||||
stats, err := h.sessionService.GetTodayOverallStatistics(userID)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "获取今日学习统计失败")
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessResponse(c, stats)
|
||||
}
|
||||
|
||||
// GetTodayReviewWords 获取今日需要复习的单词
|
||||
// GET /api/v1/learning/today/review-words
|
||||
func (h *LearningSessionHandler) GetTodayReviewWords(c *gin.Context) {
|
||||
userID := c.GetInt64("user_id")
|
||||
|
||||
words, err := h.sessionService.GetTodayReviewWords(userID)
|
||||
if err != nil {
|
||||
common.ErrorResponse(c, 500, "获取今日复习单词失败")
|
||||
return
|
||||
}
|
||||
|
||||
common.SuccessResponse(c, words)
|
||||
}
|
||||
Reference in New Issue
Block a user