package router

import (
	"exam_system/dao"
	"exam_system/entity"
	"exam_system/result"
	"exam_system/utils"
	"github.com/gin-gonic/gin"
	"strings"
)

func QuestionBank(router *RouterPlus) {

	r := router.Group("/admin")
	{

		// 添加题库
		r.POST("/questionBank", AdminAddQuestionBank)

		// 获取题库
		r.GET("/questionBank/:id", AdminGetQuestionBank)

		// 获取题库列表
		r.GET("/questionBank/list", AdminQuestionBankList)

		// 修改题库
		r.PUT("/questionBank", AdminUpdateQuestionBank)

		// 删除题库
		r.DELETE("/questionBank/:ids", AdminDeleteQuestionBank)
	}

}

func AdminAddQuestionBank(c *gin.Context) *result.Result {
	var questionBank entity.QuestionBank
	err := c.ShouldBindJSON(&questionBank)
	if err != nil || questionBank.Name == "" {
		return result.PARAM_ERROR
	}

	return dao.AddQuestionBank(&questionBank)
}

func AdminGetQuestionBank(c *gin.Context) *result.Result {
	id := c.Param("id")
	if id == "" {
		return result.UNKNOW_ERROR
	}
	return dao.QuestionBankDetail(id)
}

func AdminQuestionBankList(c *gin.Context) *result.Result {
	page, size, sort, query, err := utils.Page(c)
	if err != nil {
		return result.PARAM_ERROR
	}
	return dao.QuestionBankList(page, size, sort, query)
}

func AdminUpdateQuestionBank(c *gin.Context) *result.Result {
	var questionBank entity.QuestionBank
	err := c.ShouldBindJSON(&questionBank)
	if err != nil || questionBank.Id == 0 || questionBank.Name == "" {
		return result.PARAM_ERROR
	}
	return dao.UpdateQuestionBank(&questionBank)
}

func AdminDeleteQuestionBank(c *gin.Context) *result.Result {
	idStr := c.Param("ids")
	if idStr == "" {
		return result.PARAM_ERROR
	}

	ids := strings.Split(idStr, ",")

	return dao.DeleteQuestionBanks(ids)
}
