|
@@ -5,6 +5,7 @@ import (
|
|
|
"fmt"
|
|
|
"oilseal-train/db/model"
|
|
|
"oilseal-train/db/repo"
|
|
|
+ "strconv"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
@@ -17,6 +18,7 @@ func Test(r *GinRouter) {
|
|
|
r.GETJWT("/test/list", TestList)
|
|
|
r.POSTJWT("/test/update", TestEdit)
|
|
|
r.POSTJWT("/test/delete/:id", TestDelete)
|
|
|
+ r.GETJWT("/test/randList", TestRandList)
|
|
|
}
|
|
|
|
|
|
// 新增
|
|
@@ -89,6 +91,53 @@ func TestList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
return result, err
|
|
|
}
|
|
|
|
|
|
+// 获取随机试题
|
|
|
+func TestRandList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ // bankId number type map
|
|
|
+ _bankId := c.Query("bankId")
|
|
|
+ _number := c.Query("number")
|
|
|
+ _type := c.Query("type")
|
|
|
+
|
|
|
+ bankId, err := primitive.ObjectIDFromHex(_bankId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("bankId错误")
|
|
|
+ }
|
|
|
+ number, err := strconv.Atoi(_number)
|
|
|
+ // number 不存在时默认为10
|
|
|
+ if err != nil {
|
|
|
+ number = 10
|
|
|
+ }
|
|
|
+ fmt.Println(number)
|
|
|
+
|
|
|
+ tsType, err := strconv.Atoi(_type)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("题型错误")
|
|
|
+ }
|
|
|
+ tests := []*model.Test{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionTest,
|
|
|
+ Query: repo.Map{"bankId": bankId, "type": tsType, "state": 1},
|
|
|
+ }, &tests)
|
|
|
+ if len(tests) <= number {
|
|
|
+ return tests, nil
|
|
|
+ }
|
|
|
+ // 放入map中,利用map特性随机得到试题
|
|
|
+ sets := make(map[primitive.ObjectID]*model.Test)
|
|
|
+ for _, ts := range tests {
|
|
|
+ sets[ts.Id] = ts
|
|
|
+ }
|
|
|
+ list := []*model.Test{}
|
|
|
+ for _, ts := range sets {
|
|
|
+ if len(list) < number {
|
|
|
+ list = append(list, ts)
|
|
|
+ } else {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return list, nil
|
|
|
+}
|
|
|
+
|
|
|
// 更新
|
|
|
func TestEdit(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
// 操作用户为admin
|