package api

import (
	"errors"
	"fmt"
	"nrhe-train/db/model"
	"nrhe-train/db/repo"
	"time"

	"github.com/gin-gonic/gin"
)

func ExamHistoryCreate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	var form model.ExamHistory
	err := c.ShouldBindJSON(&form)
	fmt.Println(form)
	if err != nil {
		fmt.Println(err)
		return nil, errors.New("参数错误")
	}
	form.UserId = apictx.User.ID
	form.CreateTime = time.Now()
	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionExamHistory, &form)
}

func ExamHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	page, size, query := UtilQueryPageSize(c)
	// 没有userId时默认自己的
	if _, ok := query["userId"]; !ok {
		query["userId"] = apictx.User.ID
	}

	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionExamHistory,
		Page:        page,
		Size:        size,
		Query:       query,
	})
}
