Study/server/api/v1/learning/wrong_question.go

279 lines
9.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package learning
import (
"strconv"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/flipped-aurora/gin-vue-admin/server/model/learning"
learningReq "github.com/flipped-aurora/gin-vue-admin/server/model/learning/request"
"github.com/flipped-aurora/gin-vue-admin/server/service"
"github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type WrongQuestionApi struct{}
// CreateWrongQuestion 创建错题记录
// @Tags WrongQuestion
// @Summary 创建错题记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.WrongQuestionCreate true "创建错题记录"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /wrongQuestion/createWrongQuestion [post]
func (wrongQuestionApi *WrongQuestionApi) CreateWrongQuestion(c *gin.Context) {
var req learningReq.WrongQuestionCreate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
wrongQuestion := learning.WrongQuestion{
UserId: req.UserId,
QuestionId: req.QuestionId,
WrongAnswer: req.WrongAnswer,
WrongCount: req.WrongCount,
LastWrongAt: req.LastWrongAt,
IsMastered: req.IsMastered,
MasteredAt: req.MasteredAt,
Notes: req.Notes,
}
err = service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.CreateWrongQuestion(c.Request.Context(), &wrongQuestion)
if err != nil {
global.GVA_LOG.Error("创建失败!", zap.Error(err))
response.FailWithMessage("创建失败", c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteWrongQuestion 删除错题记录
// @Tags WrongQuestion
// @Summary 删除错题记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除错题记录"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /wrongQuestion/deleteWrongQuestion [delete]
func (wrongQuestionApi *WrongQuestionApi) DeleteWrongQuestion(c *gin.Context) {
ID := c.Query("ID")
err := service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.DeleteWrongQuestion(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("删除失败!", zap.Error(err))
response.FailWithMessage("删除失败", c)
return
}
response.OkWithMessage("删除成功", c)
}
// DeleteWrongQuestionByIds 批量删除错题记录
// @Tags WrongQuestion
// @Summary 批量删除错题记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /wrongQuestion/deleteWrongQuestionByIds [delete]
func (wrongQuestionApi *WrongQuestionApi) DeleteWrongQuestionByIds(c *gin.Context) {
IDs := c.QueryArray("IDs[]")
err := service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.DeleteWrongQuestionByIds(c.Request.Context(), IDs)
if err != nil {
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
response.FailWithMessage("批量删除失败", c)
return
}
response.OkWithMessage("批量删除成功", c)
}
// UpdateWrongQuestion 更新错题记录
// @Tags WrongQuestion
// @Summary 更新错题记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.WrongQuestionUpdate true "更新错题记录"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /wrongQuestion/updateWrongQuestion [put]
func (wrongQuestionApi *WrongQuestionApi) UpdateWrongQuestion(c *gin.Context) {
var req learningReq.WrongQuestionUpdate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
wrongQuestion := learning.WrongQuestion{
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
UserId: req.UserId,
QuestionId: req.QuestionId,
WrongAnswer: req.WrongAnswer,
WrongCount: req.WrongCount,
LastWrongAt: req.LastWrongAt,
IsMastered: req.IsMastered,
MasteredAt: req.MasteredAt,
Notes: req.Notes,
}
err = service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.UpdateWrongQuestion(c.Request.Context(), wrongQuestion)
if err != nil {
global.GVA_LOG.Error("更新失败!", zap.Error(err))
response.FailWithMessage("更新失败", c)
return
}
response.OkWithMessage("更新成功", c)
}
// FindWrongQuestion 用id查询错题记录
// @Tags WrongQuestion
// @Summary 用id查询错题记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learning.WrongQuestion true "用id查询错题记录"
// @Success 200 {object} response.Response{data=learning.WrongQuestion,msg=string} "查询成功"
// @Router /wrongQuestion/findWrongQuestion [get]
func (wrongQuestionApi *WrongQuestionApi) FindWrongQuestion(c *gin.Context) {
ID := c.Query("ID")
rewrongQuestion, err := service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.GetWrongQuestion(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败", c)
return
}
response.OkWithData(rewrongQuestion, c)
}
// GetWrongQuestionList 分页获取错题记录列表
// @Tags WrongQuestion
// @Summary 分页获取错题记录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learningReq.WrongQuestionSearch true "分页获取错题记录列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /wrongQuestion/getWrongQuestionList [get]
func (wrongQuestionApi *WrongQuestionApi) GetWrongQuestionList(c *gin.Context) {
var pageInfo learningReq.WrongQuestionSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
list, total, err := service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.GetWrongQuestionInfoList(c.Request.Context(), pageInfo)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithDetailed(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
}, "获取成功", c)
}
// GetUserWrongQuestions 获取用户错题列表
// @Tags WrongQuestion
// @Summary 获取用户错题列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param userId query uint false "用户ID"
// @Param onlyUnmastered query bool false "只获取未掌握的题目"
// @Success 200 {object} response.Response{data=[]learning.WrongQuestion,msg=string} "获取成功"
// @Router /wrongQuestion/getUserWrongQuestions [get]
func (wrongQuestionApi *WrongQuestionApi) GetUserWrongQuestions(c *gin.Context) {
userIdStr := c.Query("userId")
onlyUnmasteredStr := c.Query("onlyUnmastered")
var userId uint
if userIdStr != "" {
userIdUint, err := strconv.ParseUint(userIdStr, 10, 32)
if err != nil {
response.FailWithMessage("用户ID格式错误", c)
return
}
userId = uint(userIdUint)
} else {
// 如果没有提供userId从JWT中获取当前用户ID
userId = uint(utils.GetUserID(c))
}
onlyUnmastered := onlyUnmasteredStr == "true"
list, err := service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.GetUserWrongQuestions(c.Request.Context(), userId, onlyUnmastered)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(list, c)
}
// MarkAsMastered 标记题目为已掌握
// @Tags WrongQuestion
// @Summary 标记题目为已掌握
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.MarkMasteredRequest true "标记掌握请求"
// @Success 200 {object} response.Response{msg=string} "标记成功"
// @Router /wrongQuestion/markAsMastered [put]
func (wrongQuestionApi *WrongQuestionApi) MarkAsMastered(c *gin.Context) {
var req learningReq.MarkMasteredRequest
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err = service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.MarkAsMastered(c.Request.Context(), req.UserId, req.ID)
if err != nil {
global.GVA_LOG.Error("标记失败!", zap.Error(err))
response.FailWithMessage("标记失败", c)
return
}
response.OkWithMessage("标记成功", c)
}
// GetWrongQuestionStatistics 获取错题统计
// @Tags WrongQuestion
// @Summary 获取错题统计
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param userId query uint false "用户ID"
// @Success 200 {object} response.Response{data=learningReq.WrongQuestionStatistics,msg=string} "获取成功"
// @Router /wrongQuestion/getWrongQuestionStatistics [get]
func (wrongQuestionApi *WrongQuestionApi) GetWrongQuestionStatistics(c *gin.Context) {
userIdStr := c.Query("userId")
var userId uint
if userIdStr != "" {
userIdUint, err := strconv.ParseUint(userIdStr, 10, 32)
if err != nil {
response.FailWithMessage("用户ID格式错误", c)
return
}
userId = uint(userIdUint)
} else {
// 如果没有提供userId从JWT中获取当前用户ID
userId = uint(utils.GetUserID(c))
}
stats, err := service.ServiceGroupApp.LearningServiceGroup.WrongQuestionService.GetWrongQuestionStatistics(c.Request.Context(), userId)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(stats, c)
}