feat: add study session APIs and per-user review isolation
This commit is contained in:
@@ -16,6 +16,18 @@ type WordHandler struct {
|
||||
wordService *service.WordService
|
||||
}
|
||||
|
||||
func userIDFromContext(c *gin.Context) int64 {
|
||||
v, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
uid, ok := v.(int64)
|
||||
if !ok || uid <= 0 {
|
||||
return 1
|
||||
}
|
||||
return uid
|
||||
}
|
||||
|
||||
func NewWordHandler(wordService *service.WordService) *WordHandler {
|
||||
return &WordHandler{wordService: wordService}
|
||||
}
|
||||
@@ -41,7 +53,7 @@ func (h *WordHandler) AddWord(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 保存到数据库
|
||||
word, err := h.wordService.SaveWord(req.Word, youdaoResp)
|
||||
word, err := h.wordService.SaveWord(userIDFromContext(c), req.Word, youdaoResp)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存失败: " + err.Error()})
|
||||
return
|
||||
@@ -58,7 +70,7 @@ func (h *WordHandler) GetReviewWords(c *gin.Context) {
|
||||
mode := c.DefaultQuery("mode", "spelling")
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
||||
|
||||
words, err := h.wordService.GetReviewWords(mode, limit)
|
||||
words, err := h.wordService.GetReviewWords(userIDFromContext(c), mode, limit)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -75,7 +87,7 @@ func (h *WordHandler) SubmitReview(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.wordService.SubmitReviewAnswer(req)
|
||||
result, err := h.wordService.SubmitReviewAnswer(userIDFromContext(c), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -103,7 +115,7 @@ func (h *WordHandler) GetWords(c *gin.Context) {
|
||||
|
||||
// 获取统计信息
|
||||
func (h *WordHandler) GetStatistics(c *gin.Context) {
|
||||
stats, err := h.wordService.GetStatistics()
|
||||
stats, err := h.wordService.GetStatistics(userIDFromContext(c))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -138,3 +150,32 @@ func (h *WordHandler) GetAudio(c *gin.Context) {
|
||||
path := "./audio/" + filename
|
||||
c.File(path)
|
||||
}
|
||||
|
||||
func (h *WordHandler) CreateStudySession(c *gin.Context) {
|
||||
var req request.CreateStudySessionRequest
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
|
||||
words, err := h.wordService.CreateStudySession(userIDFromContext(c), req.Limit)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": words})
|
||||
}
|
||||
|
||||
func (h *WordHandler) SubmitStudyAnswer(c *gin.Context) {
|
||||
var req request.SubmitStudyAnswerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.wordService.SubmitStudyAnswer(userIDFromContext(c), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": result})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user