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

277 lines
8.7 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 ExamApi struct{}
// CreateExam 创建考试
// @Tags Exam
// @Summary 创建考试
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.ExamCreate true "创建考试"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /exam/createExam [post]
func (examApi *ExamApi) CreateExam(c *gin.Context) {
var req learningReq.ExamCreate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
exam := learning.Exam{
Title: req.Title,
Description: req.Description,
CourseId: req.CourseId,
Duration: req.Duration,
TotalScore: req.TotalScore,
PassScore: req.PassScore,
QuestionCount: req.QuestionCount,
StartTime: req.StartTime,
EndTime: req.EndTime,
Status: req.Status,
AllowRetake: req.AllowRetake,
MaxAttempts: req.MaxAttempts,
}
err = service.ServiceGroupApp.LearningServiceGroup.ExamService.CreateExam(c.Request.Context(), &exam)
if err != nil {
global.GVA_LOG.Error("创建失败!", zap.Error(err))
response.FailWithMessage("创建失败", c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteExam 删除考试
// @Tags Exam
// @Summary 删除考试
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除考试"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /exam/deleteExam [delete]
func (examApi *ExamApi) DeleteExam(c *gin.Context) {
ID := c.Query("ID")
err := service.ServiceGroupApp.LearningServiceGroup.ExamService.DeleteExam(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("删除失败!", zap.Error(err))
response.FailWithMessage("删除失败", c)
return
}
response.OkWithMessage("删除成功", c)
}
// DeleteExamByIds 批量删除考试
// @Tags Exam
// @Summary 批量删除考试
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /exam/deleteExamByIds [delete]
func (examApi *ExamApi) DeleteExamByIds(c *gin.Context) {
IDs := c.QueryArray("IDs[]")
err := service.ServiceGroupApp.LearningServiceGroup.ExamService.DeleteExamByIds(c.Request.Context(), IDs)
if err != nil {
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
response.FailWithMessage("批量删除失败", c)
return
}
response.OkWithMessage("批量删除成功", c)
}
// UpdateExam 更新考试
// @Tags Exam
// @Summary 更新考试
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.ExamUpdate true "更新考试"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /exam/updateExam [put]
func (examApi *ExamApi) UpdateExam(c *gin.Context) {
var req learningReq.ExamUpdate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
exam := learning.Exam{
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
Title: req.Title,
Description: req.Description,
CourseId: req.CourseId,
Duration: req.Duration,
TotalScore: req.TotalScore,
PassScore: req.PassScore,
QuestionCount: req.QuestionCount,
StartTime: req.StartTime,
EndTime: req.EndTime,
Status: req.Status,
AllowRetake: req.AllowRetake,
MaxAttempts: req.MaxAttempts,
}
err = service.ServiceGroupApp.LearningServiceGroup.ExamService.UpdateExam(c.Request.Context(), exam)
if err != nil {
global.GVA_LOG.Error("更新失败!", zap.Error(err))
response.FailWithMessage("更新失败", c)
return
}
response.OkWithMessage("更新成功", c)
}
// FindExam 用id查询考试
// @Tags Exam
// @Summary 用id查询考试
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learning.Exam true "用id查询考试"
// @Success 200 {object} response.Response{data=learning.Exam,msg=string} "查询成功"
// @Router /exam/findExam [get]
func (examApi *ExamApi) FindExam(c *gin.Context) {
ID := c.Query("ID")
reexam, err := service.ServiceGroupApp.LearningServiceGroup.ExamService.GetExam(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败", c)
return
}
response.OkWithData(reexam, c)
}
// GetExamList 分页获取考试列表
// @Tags Exam
// @Summary 分页获取考试列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learningReq.ExamSearch true "分页获取考试列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /exam/getExamList [get]
func (examApi *ExamApi) GetExamList(c *gin.Context) {
var pageInfo learningReq.ExamSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
list, total, err := service.ServiceGroupApp.LearningServiceGroup.ExamService.GetExamInfoList(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)
}
// GetActiveExams 获取进行中的考试列表
// @Tags Exam
// @Summary 获取进行中的考试列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=[]learning.Exam,msg=string} "获取成功"
// @Router /exam/getActiveExams [get]
func (examApi *ExamApi) GetActiveExams(c *gin.Context) {
list, err := service.ServiceGroupApp.LearningServiceGroup.ExamService.GetActiveExams(c.Request.Context())
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(list, c)
}
// GetExamsByCourse 根据课程获取考试列表
// @Tags Exam
// @Summary 根据课程获取考试列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param courseId query uint true "课程ID"
// @Success 200 {object} response.Response{data=[]learning.Exam,msg=string} "获取成功"
// @Router /exam/getExamsByCourse [get]
func (examApi *ExamApi) GetExamsByCourse(c *gin.Context) {
courseIdStr := c.Query("courseId")
courseId := uint(0)
if courseIdStr != "" {
if id, err := strconv.ParseUint(courseIdStr, 10, 32); err == nil {
courseId = uint(id)
}
}
list, err := service.ServiceGroupApp.LearningServiceGroup.ExamService.GetExamsByCourse(c.Request.Context(), courseId)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(list, c)
}
// PublishExam 发布考试
// @Tags Exam
// @Summary 发布考试
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.PublishExamRequest true "发布考试请求"
// @Success 200 {object} response.Response{msg=string} "发布成功"
// @Router /exam/publishExam [put]
func (examApi *ExamApi) PublishExam(c *gin.Context) {
var req learningReq.PublishExamRequest
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err = service.ServiceGroupApp.LearningServiceGroup.ExamService.PublishExam(c.Request.Context(), req.ID)
if err != nil {
global.GVA_LOG.Error("发布失败!", zap.Error(err))
response.FailWithMessage("发布失败", c)
return
}
response.OkWithMessage("发布成功", c)
}
// GetExamStatistics 获取考试统计
// @Tags Exam
// @Summary 获取考试统计
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=learningReq.ExamStatistics,msg=string} "获取成功"
// @Router /exam/getExamStatistics [get]
func (examApi *ExamApi) GetExamStatistics(c *gin.Context) {
stats, err := service.ServiceGroupApp.LearningServiceGroup.ExamService.GetExamStatistics(c.Request.Context())
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(stats, c)
}