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

207 lines
6.9 KiB
Go

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/gin-gonic/gin"
"go.uber.org/zap"
)
type QuestionApi struct{}
// CreateQuestion 创建题目
// @Tags Question
// @Summary 创建题目
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.QuestionCreate true "创建题目"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /question/createQuestion [post]
func (questionApi *QuestionApi) CreateQuestion(c *gin.Context) {
var req learningReq.QuestionCreate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
question := learning.Question{
KnowledgePointId: req.KnowledgePointId,
Title: req.Title,
Content: req.Content,
Type: req.Type,
Options: req.Options,
Answer: req.Answer,
Explanation: req.Explanation,
Difficulty: req.Difficulty,
Score: req.Score,
}
err = service.ServiceGroupApp.LearningServiceGroup.QuestionService.CreateQuestion(c.Request.Context(), &question)
if err != nil {
global.GVA_LOG.Error("创建失败!", zap.Error(err))
response.FailWithMessage("创建失败", c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteQuestion 删除题目
// @Tags Question
// @Summary 删除题目
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除题目"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /question/deleteQuestion [delete]
func (questionApi *QuestionApi) DeleteQuestion(c *gin.Context) {
ID := c.Query("ID")
err := service.ServiceGroupApp.LearningServiceGroup.QuestionService.DeleteQuestion(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("删除失败!", zap.Error(err))
response.FailWithMessage("删除失败", c)
return
}
response.OkWithMessage("删除成功", c)
}
// DeleteQuestionByIds 批量删除题目
// @Tags Question
// @Summary 批量删除题目
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /question/deleteQuestionByIds [delete]
func (questionApi *QuestionApi) DeleteQuestionByIds(c *gin.Context) {
IDs := c.QueryArray("IDs[]")
err := service.ServiceGroupApp.LearningServiceGroup.QuestionService.DeleteQuestionByIds(c.Request.Context(), IDs)
if err != nil {
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
response.FailWithMessage("批量删除失败", c)
return
}
response.OkWithMessage("批量删除成功", c)
}
// UpdateQuestion 更新题目
// @Tags Question
// @Summary 更新题目
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.QuestionUpdate true "更新题目"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /question/updateQuestion [put]
func (questionApi *QuestionApi) UpdateQuestion(c *gin.Context) {
var req learningReq.QuestionUpdate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
question := learning.Question{
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
KnowledgePointId: req.KnowledgePointId,
Title: req.Title,
Content: req.Content,
Type: req.Type,
Options: req.Options,
Answer: req.Answer,
Explanation: req.Explanation,
Difficulty: req.Difficulty,
Score: req.Score,
}
err = service.ServiceGroupApp.LearningServiceGroup.QuestionService.UpdateQuestion(c.Request.Context(), question)
if err != nil {
global.GVA_LOG.Error("更新失败!", zap.Error(err))
response.FailWithMessage("更新失败", c)
return
}
response.OkWithMessage("更新成功", c)
}
// FindQuestion 用id查询题目
// @Tags Question
// @Summary 用id查询题目
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learning.Question true "用id查询题目"
// @Success 200 {object} response.Response{data=learning.Question,msg=string} "查询成功"
// @Router /question/findQuestion [get]
func (questionApi *QuestionApi) FindQuestion(c *gin.Context) {
ID := c.Query("ID")
requestion, err := service.ServiceGroupApp.LearningServiceGroup.QuestionService.GetQuestion(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败", c)
return
}
response.OkWithData(requestion, c)
}
// GetQuestionList 分页获取题目列表
// @Tags Question
// @Summary 分页获取题目列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learningReq.QuestionSearch true "分页获取题目列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /question/getQuestionList [get]
func (questionApi *QuestionApi) GetQuestionList(c *gin.Context) {
var pageInfo learningReq.QuestionSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
list, total, err := service.ServiceGroupApp.LearningServiceGroup.QuestionService.GetQuestionInfoList(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)
}
// GetQuestionsByKnowledgePoint 根据知识点获取题目列表
// @Tags Question
// @Summary 根据知识点获取题目列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param knowledgePointId query uint true "知识点ID"
// @Success 200 {object} response.Response{data=[]learning.Question,msg=string} "获取成功"
// @Router /question/getQuestionsByKnowledgePoint [get]
func (questionApi *QuestionApi) GetQuestionsByKnowledgePoint(c *gin.Context) {
knowledgePointIdStr := c.Query("knowledgePointId")
knowledgePointId, err := strconv.ParseUint(knowledgePointIdStr, 10, 32)
if err != nil {
response.FailWithMessage("知识点ID格式错误", c)
return
}
list, err := service.ServiceGroupApp.LearningServiceGroup.QuestionService.GetQuestionsByKnowledgePoint(c.Request.Context(), uint(knowledgePointId))
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(list, c)
}