diff --git a/api/controller/question.go b/api/controller/question.go index f1c6334..4eb5259 100644 --- a/api/controller/question.go +++ b/api/controller/question.go @@ -172,6 +172,69 @@ func (r *Question) PutQuestion(c *gin.Context) { responses.Ok(c) } +// PutQuestionStatus 修改题目状态 +func (b *Question) PutQuestionStatus(c *gin.Context) { + questionRequest := requests.QuestionRequest{} + req := questionRequest.PutQuestionStatus + if err := c.ShouldBind(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + id := c.Param("question_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + // 将 id 转换为 int64 类型 + questionId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + // 获取基础分类数据 + questionDao := dao.QuestionDao{} + question, err := questionDao.GetQuestionById(questionId) + if err != nil { + responses.FailWithMessage("题目异常", c) + return + } + + // 检测状态 + if question.QuestionStatus == req.QuestionStatus { + responses.Ok(c) + return + } + + // 开始事务 + tx := global.Db.Begin() + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + questionData := make(map[string]interface{}) + questionData["question_status"] = req.QuestionStatus + err = questionDao.EditQuestionById(tx, questionId, questionData) + if err != nil { + tx.Rollback() + responses.FailWithMessage("操作失败", c) + return + } + + tx.Commit() + responses.Ok(c) +} + // PutQuestionTest 修改题目 func (r *Question) PutQuestionTest(c *gin.Context) { couponRequest := requests.QuestionRequest{} diff --git a/api/requests/Question.go b/api/requests/Question.go index 807875e..bfcee4e 100644 --- a/api/requests/Question.go +++ b/api/requests/Question.go @@ -1,13 +1,14 @@ package requests type QuestionRequest struct { - GetQuestionPage // 获取题目列表-分页 - AddQuestion // 新增题目 - AddQuestionTest // 新增题目 - PutQuestion // 修改题目 - PutQuestionTest // 修改题目 - DeleteQuestion // 修改题目 - GetQuestionCount // 获取题目数量 + GetQuestionPage // 获取题目列表-分页 + AddQuestion // 新增题目 + AddQuestionTest // 新增题目 + PutQuestion // 修改题目 + PutQuestionStatus // 操作问题发布状态 + PutQuestionTest // 修改题目 + DeleteQuestion // 修改题目 + GetQuestionCount // 获取题目数量 } // GetQuestionPage 获取题目列表-分页 @@ -81,6 +82,11 @@ type PutQuestion struct { QuestionOption []string `json:"question_option" form:"question_option" label:"选项"` } +// PutQuestionStatus 修改题目状态 +type PutQuestionStatus struct { + QuestionStatus int `json:"question_status" form:"question_status" label:"状态" validate:"required,oneof=1 2"` // 状态(1:正常 2:禁用) +} + // PutQuestionTest 修改题目 type PutQuestionTest struct { QuestionName string `json:"question_name" form:"question_name" validate:"required" label:"题目名称"` diff --git a/api/router/router.go b/api/router/router.go index 993a6e3..4111804 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -146,6 +146,9 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 修改题目 questionGroup.PUT("/:question_id", api.Question.PutQuestion) + // 修改题目状态 + questionGroup.PUT("/status/:question_id", api.Question.PutQuestionStatus) + // 删除题目 questionGroup.DELETE("", api.Question.DeleteQuestion)